+ Start a Discussion
shweta raghav 13shweta raghav 13 

create records on the basis of multiselect values

Trying below code but getting error :
Apex trigger oppsplicreate caused an unexpected exception, contact your administrator: oppsplicreate: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, oppsplicreate: maximum trigger depth exceeded Opportunity_Splits trigger event AfterInsert
  1.  Whenever an opportunity is created an opportunity split record should be created with the practice same as that of opportunity and percentage as 100. (If the practice is one)
  2. If there are two practices then whenever an opportunity is created two opportunity split records should be created with the practices same as that of opportunity and each with 50% share

Sum of the two should always be equal to 100%

trigger oppsplicreate on Opportunity_Splits__c(after delete, after insert, after update){

    set<Id> OpportunityIds = new set<Id>();
    List<Opportunity_Splits__c> opportunitySplitList = new List<Opportunity_Splits__c>();
    
    
    if(trigger.isInsert || trigger.isUpdate)
    {
        for(Opportunity_Splits__c p : trigger.new)
        {
            OpportunityIds.add(p.Opportunity__c);    
            opportunitySplitList.add(p);
            system.debug('-----opportunitySplitList--------'+opportunitySplitList);
        }
    }     
    List<Opportunity_Splits__c> lstCon = new List<Opportunity_Splits__c>();
    Opportunity oppt= [Select Id, Practiceu__c FROM Opportunity WHERE  id IN :OpportunityIds];
//    Opportunity oppt=[SELECT Name from Opportunity where Practiceu__cIN :multselect.split(';')
    system.debug('-----oppt--------'+oppt);
    String str = oppt.Practiceu__c ;
    system.debug('-----str --------'+str );
    if(str !=null)
    {
        List<String> lstValues = str.split(';');
        for(String pkValue: lstValues)
        {
            Opportunity_Splits__c con = new Opportunity_Splits__c(Opportunity__c = oppt.Id, Practice__c =pkValue);
            lstCon.add(con);    
        }
        insert lstCon;
}
}

Ahmad J. KoubeissyAhmad J. Koubeissy
try this code :
trigger oppsplicreate on Opportunity_Splits__c(after delete, after insert, after update){

    set<Id> OpportunityIds = new set<Id>();
    List<Opportunity_Splits__c> opportunitySplitList = new List<Opportunity_Splits__c>();
    
    
    if(trigger.isInsert || trigger.isUpdate)
    {
        for(Opportunity_Splits__c p : trigger.new)
        {
            OpportunityIds.add(p.Opportunity__c);    
            opportunitySplitList.add(p);
            system.debug('-----opportunitySplitList--------'+opportunitySplitList);
        }
    }     
    List<Opportunity_Splits__c> lstCon = new List<Opportunity_Splits__c>();
    
	for(Opportunity opp:[Select Id, Practiceu__c FROM Opportunity WHERE  id IN :OpportunityIds]){
		system.debug('-----oppt--------'+oppt);
		String str = oppt.Practiceu__c ;
		system.debug('-----str --------'+str );
		if(str !=null)
		{
			List<String> lstValues = str.split(';');
			for(String pkValue: lstValues)
			{
				Opportunity_Splits__c con = new Opportunity_Splits__c(Opportunity__c = oppt.Id, Practice__c =pkValue);
				lstCon.add(con);    
			}
		}
	}	
	insert lstCon;
}

tell if you need to add the percentage
Kindly mark as best answer if this solves your problem
shweta raghav 13shweta raghav 13
@ahmad, i try above code,its gives same error.
GauravGargGauravGarg
Hi Shweta,

Update this code in batch job, and then try. 

Thanks,
Gaurav
Skype: gaurav2990
Ahmad J. KoubeissyAhmad J. Koubeissy
the Opportunity_Splits__c records are created manually with opportunity creation, or they should be automaticaly created when creating or updating the practice field on the opportunity ??
shweta raghav 13shweta raghav 13
records will be create automatically.
Ahmad J. KoubeissyAhmad J. Koubeissy
so you should create the trigger on the opportunity not on the opportunity_split, because the creation of a new opportunity will cause the trigger launch.
Ahmad J. KoubeissyAhmad J. Koubeissy
what is the type of Practiceu__c  field? is it a multi-picklist ?
and the idea is if the practice has two values like (value1; value 2), two opportunities splits will be created ? if it has 3 values,, so 3 opps split will be created?