You need to sign in to do that
Don't have an account?
dselat
trigger to add value to a custom multi-select pick list field
Hello, I need to have a trigger to update or add new values to existing multi-select custom picklist field. I've below trigger but it overwrites existing value. How can I add to multi-select field? or Can you direct me to a forum that have a solution? Appreciate your help.
trigger AccountUpdate_Saas on OpportunityLineItem (after update) {
//Get Triggered Opps
List<Account> Accts = new List<Account>();
for (OpportunityLineItem ol : [Select O.Opportunity.AccountId, O.Contract_Type__c from OpportunityLineItem O where O.id in :Trigger.newMap.keySet()]) {
if(ol.Contract_Type__c == 'SaaS'){
Account eachAccount = new Account(Id=ol.Opportunity.AccountId);
eachAccount.Client_Product_Line_s__c='SaaS (Not Specific)';
Accts.add(eachAccount);
}
}
if(!Accts.isEmpty()){
update(Accts);
}
}
You can add multi-select picklist values to a field through the user interface by going to Setup | Customize | Opportunity | Fields and selecting your multi-select picklist.
The Values in a multi select picklist are stored as semi colon (;) separated values in the database.
eg London;Paris;Frankfurt
So if you'd like to append values, just delimit using semicolons and set the field value
Thanks Ritesh. But then I should not overwrite the values that already exists in multi-select field. How can I do this with triggers? Should I first query and get all the values in that field and re-add them along with the new value? If so how to do it? I am new to SFDC triggers.
I am not talking about adding through gui.
Yes, include them in your SOQL query and then just append the new values to them, like you would to a string
eg
for (Account acc : [Select Id, MultiSelectField__c from Account where ....]){
....
acc.MultiSelectField__c + = ''Paris;London' ;
}
Use this trigger
This should solve your problem.
Here is the updated code but it still won't append the new value to the multi-select field. Not sure what is wrong:
You're missing the semi-colon delimiter between values. Also in an after trigger, you need an explicit update - so you will need to aggregate your Account record in a list and issue an Update at the end
So,
Hi Shashikant, Thanks...It works! Need to add another condition is it should not add 'SaaS (Not Specific) if it already exists..How to achieve this?.
if (acc.Client_Product_Line_s__c.indexOf('SaaS (Not Specific)') < 0)
acc.Client_Product_Line_s__c += ';SaaS (Not Specific)';
You could also use CONTAINS, i.e. str1.contains(str2)
Another alternative would be to only select values which dont already have Saas (Not Specific) in the field by using a SOQL Where Clause, so that condition never arises.
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_querying_multiselect_picklists.htm
Hi dselat,
I will your solution has two problems in my view
1)It uses SOQL in loop , not a best practice bcoz trigger will fail for bulk data
2)acc.Client_Product_Line_s__c +='SaaS (Not Specific)';
I think it should be
acc.Client_Product_Line_s__c +=';SaaS (Not Specific)';
Use this in your condition before update
Ritesh and Shashikant...thanks to both of you. Here is the final code that is working fine: