Applies to: Lightning and Classic |
Ever need to update Salesforce records that have multi-picklists without fear of overriding the existing data in those pick lists? Here’s a simple solution for you.
In my example below we’ll assume we have a Salesforce multi-picklist called Product_Interest__c on the Contact object.
Create a new custom text field on the Contact object to capture the data to be imported. I recommend labeling the field the same as the multi-picklist field name plus “import”. So in our example, that would be Product_Interest_Import__c. This puts the fields alphabetically next to each other when using your import tool making it easy to remember NOT to import data into the multi-picklist field.
Next create a Contact Before Update trigger and place the following code in the trigger (modifying the field names to match your fields):
<br />for (Contact c: trigger.new) {<br />// update Product_Interest__c multi-picklist if a new value is being imported<br />string pl = c.Product_Interest__c;</p> <p>if (c.Product_Interest_Import__c != null) {<br />string pli = c.Product_Interest_Import__c.trim();<br />if (pl == null) {<br />pl = pli;<br />} else {<br />if (pl.contains(pli)) { // do nothing<br />} else {<br />pl = pl + ';' + pli;<br />}<br />}</p> <p>// reset the import field to empty so it doesn't attempt reprocess the import data<br />c.Product_Interest_Import__c = null;<br />}</p> <p>// set the multi-picklist to their new value<br />c.Product_Interest__c = pl;<br />}<br />
When you import your data, use your new Import (product_Interest__c) field. The trigger reads then import field, checks the multi-picklist field to see if it exist, if it doesn’t then in appends the import field into the Salesforce multi-picklist field.
Pretty simple and makes the import so much easier.