function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RavitejaRaviteja 

multi select picklist

i have multi select picklist feild in account. the no of opportunities should be created that are equals to values selected in multi select picklist feild

 

i wrote below code . by using this iam able to create one opportunty at a time

 

trigger:

=========================

  trigger OppIns on Account (after insert) {

list<Opportunity> oppts =new list<Opportunity>();
 for(account acc:trigger.new)
 {
 oppts =new list<Opportunity>();
oppts.add(new Opportunity(name=acc.products__c,accountid=acc.id,StageName='Prospecting',Closedate=date.today()));

  }

   insert oppts;

}

 

 

for example i select  two values in picklist two opportunity should be created

Best Answer chosen by Admin (Salesforce Developers) 
Gunners_23Gunners_23

In this case inside the for loop you have to first get the value of the multi-select picklist value to string

 

String multi = Acccount.SomeField__c;

 

Then split the string based on ';' using split function. So you got the list of values which were selected in the

 

multi-select picklist and then iterate through the same and create opportunity records for each value selected 

All Answers

Gunners_23Gunners_23

In this case inside the for loop you have to first get the value of the multi-select picklist value to string

 

String multi = Acccount.SomeField__c;

 

Then split the string based on ';' using split function. So you got the list of values which were selected in the

 

multi-select picklist and then iterate through the same and create opportunity records for each value selected 

This was selected as the best answer
RavitejaRaviteja

thanks for your reply

 

how to iterate splited values, can u post me  the code snippet

Gunners_23Gunners_23

 

List<Opportunity> oppts = new List<Opportunity>();
String selected = 'A;B;C;D'; // THe value selected from multi-select picklist will look something like this in background
List<String> selectedValues = Selected.split(';'); // Split to get the individual values
for(String selectedValuesInstance : selectedValues)
{
oppts.add(new Opportunity(name=acc.products__c,accountid=acc.id,StageName='Prospecting',Closedate=date.today(),
PicklistField__c = selectedValuesInstance)); 
}