From 9c9103b1644f0eb4dc470d2cf0e9775f9ab8805e Mon Sep 17 00:00:00 2001 From: Evan Kennedy Date: Thu, 7 Aug 2014 08:51:22 -0500 Subject: [PATCH] Removed insert inside of loop, and also added functionality to purge list before inserting, in order to pick up picklist value deletions. --- .../classes/ManagePicklistsForFlow.cls | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/FlowPicklistSync/classes/ManagePicklistsForFlow.cls b/FlowPicklistSync/classes/ManagePicklistsForFlow.cls index 6abdec5..e1ed2fe 100644 --- a/FlowPicklistSync/classes/ManagePicklistsForFlow.cls +++ b/FlowPicklistSync/classes/ManagePicklistsForFlow.cls @@ -13,6 +13,16 @@ global Class ManagePicklistsForFlow implements Process.Plugin { return new Process.PluginResult(result); } + + private void deleteValuesFromTable(String objectName, String fieldName) { + List plList = + [select Id + from PicklistForFlow__c + where ObjectName__c = :objectName + and PicklistFieldName__c = :fieldName]; + + delete plList; + } private void ManagePicklist(String ObjectAPIName, String FieldAPIName) { @@ -21,15 +31,32 @@ global Class ManagePicklistsForFlow implements Process.Plugin { Schema.DescribeSOBjectResult sotDesc = sot.getDescribe(); Schema.DescribeFieldResult F = sotDesc.fields.getMap().get(FieldAPIName).getDescribe(); List P = F.getPicklistValues(); - + Set fieldValues = new Set(); + Map flowMap; + List toInsert = new List(); + + for (PicklistEntry aPickListval : P) { + fieldValues.add(aPickListval.getValue()); + } + + deleteValuesFromTable(ObjectAPIName, FieldAPIName); for (PicklistEntry aPickListval: P) { - PicklistForFlow__c[] fpv = [select Name from PicklistForFlow__c where ObjectName__c = :ObjectAPIName and PicklistFieldName__c = :FieldAPIName and PickListValue__c = :aPickListval.getValue()]; + /*PicklistForFlow__c[] fpv = [select Name from PicklistForFlow__c where ObjectName__c = :ObjectAPIName and PicklistFieldName__c = :FieldAPIName and PickListValue__c = :aPickListval.getValue()]; if (fpv.size() == 0 ) { PicklistForFlow__c newFpv = new PicklistForFlow__c(ObjectName__c = ObjectAPIName, PickListValue__c = aPickListval.getValue(), PicklistFieldName__c =FieldAPIName, PickListLabel__c =aPickListval.getLabel() ); Database.insert(newFpv); - } + }*/ + toInsert.add(new PicklistForFlow__c( + ObjectName__c = ObjectAPIName, + PickListValue__c = aPickListval.getValue(), + PicklistFieldName__c = FieldAPIName, + PickListLabel__c = aPickListval.getLabel())); + } + + if (!toInsert.isEmpty()) { + insert toInsert; } }