+ Start a Discussion
KeithlarsKeithlars 

Merging values from 2 multiselect picklist in a trigger

I have two multiselect picklist for area of interest, one is being updated from outside of salesforce which I call my area_of_interest_landing__c field, the other is area_of_interest__c. 

The area_of_interest_landing__c field may or may not have all the value selected that the area_of_interest__c field has.  I need to keep a complete set of values being selected over time in the area_of_interest__c field.  My thought was to create a trigger that would update the area_of_interest__c field whenever the area_of_interest_landing__c field is updated.  This is my first trigger and I haven't seen any examples of updating multiselect picklist so I'm kind of struggling here so I would like to know if I'm on the right track with the code below.  Currently, I get a complie error on invalid type of "String".

trigger area_of_interest on Lead (after insert, after update){
    Set<String> interests = new Set<String>();
   
    for (area_of_interest_landing__c aoiLanding : trigger.new)
        interests.add(aoiLanding.area_of_interest_landing__c);
   
    for (area_of_interest__c aoi : trigger.new)
        interests.add(aoi.area_of_interest__c);

    for (area_of_interest__c aoi : trigger.new)
        aoi.area_of_interest__c = entries.get(aoi.area_of_interest__c);
}
Best Answer chosen by Admin (Salesforce Developers) 
narsavagepnarsavagep
I based this off of some multiselect-picklist code that I'm currently using.  I didn't actually run this code through the system, so forgive me if there are any syntax errors, but the logic should be correct.  I think the "split" and "addAll" methods will help you do what you need to do.  Hope this helps.
 
Code:
Set<String> setInterests;
Set<String> setInterestsLanding;
String strNewInterests;

for (Lead L : trigger.new) {

 //Merge two aoi fields
 if ( L.area_of_interest_landing__c != null) setInterestsLanding = new Set<String> ( L.area_of_interest_landing__c.split(';') );
 if ( L.area_of_interest__c != null) setInterests = new Set<String> ( L.area_of_interest__c.split(';') );
 setInterests.addAll(setInterestsLanding);

 //Create "New" combined aoi field 
 if ( setInterest.size() > 0 ) {
  for (String strInterest : setInterests) {
   if (strNewInterests == null) {
    strNewInterests = strInterest;
   } else {
    strNewInterests += ';' + strInterest;
   }
  }
 }

 //Set field
 L.area_of_interest__c = strNewInterests;

} 

 


Message Edited by narsavagep on 11-19-2008 09:53 AM

All Answers

mikefmikef
The error is coming from our first for loop, area_of_interest_landing__c is a field and you want to loop through Trigger.new.

Change your for loops to look like this for(Lead variableName : Trigger.new)...
KeithlarsKeithlars
Okay, that gets the Set updated with all the values, now how to I update the multiselect picklist lead.area_of_interest__c with those values?

trigger area_of_interest on Lead (after insert, after update){
    Set<String> interests = new Set<String>();
   
    for (Lead aoiLanding:trigger.new)
        interests.add(aoiLanding.area_of_interest_landing__c);
   
    for (Lead aoi:trigger.new)
        interests.add(aoi.area_of_interest__c);

    //Update area_of_interest__c multiselect picklist field with values in Set named "interests"
???
}
mikefmikef
well what you are really doing is creating a Set with all the string values, but you have no reference to what record any of the values belongs to.

You need to create a Map of Id and string then reference the map to update the other field.
narsavagepnarsavagep
I based this off of some multiselect-picklist code that I'm currently using.  I didn't actually run this code through the system, so forgive me if there are any syntax errors, but the logic should be correct.  I think the "split" and "addAll" methods will help you do what you need to do.  Hope this helps.
 
Code:
Set<String> setInterests;
Set<String> setInterestsLanding;
String strNewInterests;

for (Lead L : trigger.new) {

 //Merge two aoi fields
 if ( L.area_of_interest_landing__c != null) setInterestsLanding = new Set<String> ( L.area_of_interest_landing__c.split(';') );
 if ( L.area_of_interest__c != null) setInterests = new Set<String> ( L.area_of_interest__c.split(';') );
 setInterests.addAll(setInterestsLanding);

 //Create "New" combined aoi field 
 if ( setInterest.size() > 0 ) {
  for (String strInterest : setInterests) {
   if (strNewInterests == null) {
    strNewInterests = strInterest;
   } else {
    strNewInterests += ';' + strInterest;
   }
  }
 }

 //Set field
 L.area_of_interest__c = strNewInterests;

} 

 


Message Edited by narsavagep on 11-19-2008 09:53 AM
This was selected as the best answer