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
A GunaleA Gunale 

trigger to create two Opportunities when Account MultiPicklist field has two value

I have two multipicklist fields. One is in Account object and Another in Opportunity. If Account Multipicklist field has Two values (X,Y) then create two opportunities and update dat value into opportunity multipicklist field. If Account related opportunities has X value then create only one opporunity nd update field with Y.

Thank you.
Best Answer chosen by A Gunale
Abhishek BansalAbhishek Bansal
Hi Gunale,

As per your requirement I have written the trigger logic which will handle both the insert and update case as well as the bulk data:
trigger createOpportunities on Account(after insert, after Update) {
	Set<Id> accountIds = new Set<Id>();
	List<Opportunity> oppListToInsert = new List<Opportunity>();
	Opportunity newOpp;
	for(Account acc : trigger.New) {
		if((trigger.isInsert && acc.Multi__c != null) {
			for(String programValue : acc.Multi__c .split(';')) {
				newOpp = new Opportunity();
				op.name = acc.Name;
                op.AccountId = acc.Id;
                op.CloseDate = Date.Today();
                op.StageName = 'Closed Won';
                op.Sale_Type__c = programValue;
                oppListToInsert.add(newOpp);
			}
		}
		if(trigger.isUpdate && trigger.oldMap.get(acc.Id).Multi__c != acc.Multi__c && acc.Multi__c != null) {
			accountIds.add(acc.Id);
		}
	}
	
	if(accountIds.size() > 0) {
		Set<String> oppProgramValues;
		for(Account acc : [Select Id, Name, Multi__c, (Select Sale_Type__c from Opportunities) from Account where Id IN: accountIds]) {
			oppProgramValues = new Set<String>();
			for(Opportunity accOpp : acc.Opportunities) {
				oppProgramValues.add(accOpp.Sale_Type__c);
			}
			for(String programValue : acc.Multi__c .split(';')) {
				if(!oppProgramValues.contains(programValue)) {
					newOpp = new Opportunity();
					op.name = acc.Name;
					op.AccountId = acc.Id;
					op.CloseDate = Date.Today();
					op.StageName = 'Closed Won';
					op.Sale_Type__c = programValue;
					oppListToInsert.add(newOpp);
				}
			}
		}
	}
	
	if(oppListToInsert.size() > 0) {
		insert oppListToInsert;
	}
}

Please take care of any syntax error and API names of the field. Let me know if you need any further help on this.

Thanks,
Abhishek Bansal. 

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi A,

As mentioned in the below link that has a similar use case you can try the below method to implement your scenario:

>> https://salesforce.stackexchange.com/questions/269718/trigger-to-create-multiple-records-based-on-multiselect-picklist-value

Let me know in case if you need any help in implementing your scenario.

Regards,
Anutej 

 
Abhishek BansalAbhishek Bansal
Hi Gunale,

You can achieve this with the help of the trigger. Please follow the below steps:
  1. Create a trigger on the Account object. Context would be after insert, after update.
  2. Check the value in the multipicklist field and based on that value create the opportunity as you want.
Let me know if you need any further help on this.

Thanks,
Abhishek Bansal.
A GunaleA Gunale
Thank you ANUTEJ  & Abhishek Bansal for your quick response.

My requirement is , While updating Account record , It should check Related Opportunities has same multipicklist value of Account. If so then don't create opportunity else create an opportunity.
 
ANUTEJANUTEJ (Salesforce Developers) 
So, is it possible to give an example like?
A GunaleA Gunale
Created Account with name 'Person' and multipicklist field 'Multi__c'  has value X;Y.   
Two Opportunities are created i.e 'op1'  its picklist value is updated with X  and  'op2'  its picklist value is updated with Y.

Requirement : If I update 'Person' Account by adding  Z in 'Multi__c' Then it will be X;Y;Z. 
During updation It should check whether the related Opportunities picklist field has value Z . If not Create an Opportunity with picklist value Z.
Abhishek BansalAbhishek Bansal
Hi Gunale,

As per your requirement I have written the trigger logic which will handle both the insert and update case as well as the bulk data:
trigger createOpportunities on Account(after insert, after Update) {
	Set<Id> accountIds = new Set<Id>();
	List<Opportunity> oppListToInsert = new List<Opportunity>();
	Opportunity newOpp;
	for(Account acc : trigger.New) {
		if((trigger.isInsert && acc.Multi__c != null) {
			for(String programValue : acc.Multi__c .split(';')) {
				newOpp = new Opportunity();
				op.name = acc.Name;
                op.AccountId = acc.Id;
                op.CloseDate = Date.Today();
                op.StageName = 'Closed Won';
                op.Sale_Type__c = programValue;
                oppListToInsert.add(newOpp);
			}
		}
		if(trigger.isUpdate && trigger.oldMap.get(acc.Id).Multi__c != acc.Multi__c && acc.Multi__c != null) {
			accountIds.add(acc.Id);
		}
	}
	
	if(accountIds.size() > 0) {
		Set<String> oppProgramValues;
		for(Account acc : [Select Id, Name, Multi__c, (Select Sale_Type__c from Opportunities) from Account where Id IN: accountIds]) {
			oppProgramValues = new Set<String>();
			for(Opportunity accOpp : acc.Opportunities) {
				oppProgramValues.add(accOpp.Sale_Type__c);
			}
			for(String programValue : acc.Multi__c .split(';')) {
				if(!oppProgramValues.contains(programValue)) {
					newOpp = new Opportunity();
					op.name = acc.Name;
					op.AccountId = acc.Id;
					op.CloseDate = Date.Today();
					op.StageName = 'Closed Won';
					op.Sale_Type__c = programValue;
					oppListToInsert.add(newOpp);
				}
			}
		}
	}
	
	if(oppListToInsert.size() > 0) {
		insert oppListToInsert;
	}
}

Please take care of any syntax error and API names of the field. Let me know if you need any further help on this.

Thanks,
Abhishek Bansal. 
This was selected as the best answer
A GunaleA Gunale
Thank you Abhishek