You need to sign in to do that
Don't have an account?
Trigger to update multi-select picklist
I'm writing a trigger to update a multi-select picklist. However, I'm having trouble adding more than one value to the field.
Here's the scenario - we have a number of fields on the record that may or may not be null. They are also all associated with different reasons, which make up the values of the ChangeTag__c multi-select picklist field. If the fields are NOT null, then the corresponding reason needs to be selected in ChangeTag__c.
One record can have any given number of reasons. Below is a portion of the code that I wrote:
//(4) update Change Tag field based on new field values
for(integer i=0; i<ChangeRequestUpdateList.size();i++){
if(dd.RFP_Template__c!=null){dd.ChangeTag__c='Terms & Conditions';}
if(dd.Legal_Terms__c!=null){dd.ChangeTag__c='Terms & Conditions';}
if(dd.Ad_Server__c!=null){dd.ChangeTag__c='Ad Server Change';}
if(dd.ReTargeting__c!=null){dd.ChangeTag__c='ReTargeting Change';}
}
update ChangeRequestUpdateList;
}
}
The issue is that if there are multiple fields that aren't null, only one reason shows up in ChangeTag__c. For example, if RFP_Templace__c!=null and Ad_Server__c!=null, the only value that gets updated to ChangeTag__c is 'Ad Server Change.' Any ideas on how to write the code so that both 'Terms & 'Conditions' and 'Ad Server Change' are selected? There are way more fields and reasons than what is included above, so it would be nearly impossible to write out every combination.
Thank you so much!
I'm stunned your post has 19 views and no one replied yet. The start of your trigger is excellent. But there are some things you need to stay focused on when programming (not specific to apex really).
.
All Answers
Update - I was able to set it up so that it adds the values to the picklist, but no matter what it also always adds "null". For example, it will say "Terms & Conditions, Ad Server Change, ReTargeting Change, null." Any ideas way? Any help is greatly appreciated!
Updated Code:
Just a guess, I assume that when no value is picked, the default string value of the multipicklist may be null. You're using +=, which may add to that null, instead of replace the default null value.
You could use = or += depneding on conditionally checking the ChangeTag__c value.
Mind posting some more of your code ? I don't understand why you're doing an iteration but operating on objects outside your iteration scope.
Yes, you're exactly right - it's because the default string value of hte multipicklist is null. Below is the rest of the code. It's now working, but I'm new to apex so there may be a better strategy for achieving the same result? All of the updates are happening on the same object though.
I'm stunned your post has 19 views and no one replied yet. The start of your trigger is excellent. But there are some things you need to stay focused on when programming (not specific to apex really).
.
Hi Sdry - Thank you SO much for your comments. As someone is just learning Apex (with no prior coding experience), I really appreciate the guidance.
I tried following your advice below, but I'm getting an error message. When I try to save, it says "Compile Error: unexpected toekn: 'List' at line 10 column 0." Line 10 is where my second list (1st DDF) begins - I'm assuming I entered it correctly, but do you see where I went wrong?
Makst 1stDDF firstDDF. Can't start a variable/method/class/property with a numeric caracter.
Hi Sdry - Thank you so much for your help. I had to add in a few brackets to make it save properly, but I believe I still kept the dml statement outside of the iteration. The bracket after the update command closes the first bracket in the trigger, not the list iteration.
Below is the final code - if you could let me know if there are still any issues, it would be much appreciated!! I'm really trying to become more efficient and learn to write solid code, and this has been very helpful.
It looks good to me. However, to garantee the code runs as expected, and will continue to do so in the future (or that we are alerted if not) the force.com platform demands us to write test code for our triggers. You will need 75%of your code covered by test codes in production to add anything else.
If you haven't done so before, these are worth reading, and a good start off point.
http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_testing_intro.htm|StartTopic=Content%2Fapex_testing_intro.htm|SkinName=webhelp
Thank you so much!