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
Rizwan Ali 12Rizwan Ali 12 

Trigger: On Oppotunity to create Job object Records

Hi,
Criteria:
On create Opportunity record , the Job Object records will create with according to Opportunity 'Quntity' Field ex: if quntity is 5 then create 5 job records and opportunity 'Amount' set in Job Object divided by 5 ex: If quntity is 5 and Amount is 1000 , set 200 to each Job records
I did this but didn't work
Please answer it.
Thanks in Advance.
If(Trigger.IsAfter && Trigger.isInsert)
   {
       for(Opportunity opp : Trigger.New)
       {
           Decimal Amount = opp.Amount / opp.TotalOpportunityQuantity;
           
           List<job__c> listofjob = new List<job__c>();
           
           for(integer i = 1; i<=opp.TotalOpportunityQuantity; i++)
           {
               job__c ajob = new job__c(Name = 'Job'+i, Amount__c =Amount );
               listofjob.add(ajob);
           }
           
           if(listofjob.size()>0)
           {
               insert listofjob;
           }
       }
   }

 
Best Answer chosen by Rizwan Ali 12
Sunil RathoreSunil Rathore
Hi Rizwan,

Please refer the below code. The trigger will execute only when the new Opportunity will create. It won't execute on the update of the opportunity.
If(Trigger.IsAfter && Trigger.isInsert){
	List<job__c> listofjob = new List<job__c>();
	for(Opportunity opp : Trigger.New){
		Decimal Amount = opp.Amount / opp.TotalOpportunityQuantity;
		for(integer i = 1; i<=opp.TotalOpportunityQuantity; i++){
		   job__c ajob = new job__c(Name = 'Job'+i, Amount__c = Amount, listofjobs__c =  opp.id);
		   listofjob.add(ajob);
		}
	}
	if(listofjob.size()>0){
		insert listofjob;
	}
}

Hope this will help you. If does than mark it as the best answer so it can also help others in the future.

Many Thanks,
Sunil Rathore

All Answers

Sunil RathoreSunil Rathore
Hi Rizwan,

Your code looks perfect. You just need to avoid performing DML operation inside the for loop. I have removed the Insert Operation from the for loop. 

While insertion of the Job record make sure that value should be assigned to all the required field of the Job and hoping that your trigger is Active.

Refer to the below-updated code. 
If(Trigger.IsAfter && Trigger.isInsert){
	List<job__c> listofjob = new List<job__c>();
	for(Opportunity opp : Trigger.New){
		Decimal Amount = opp.Amount / opp.TotalOpportunityQuantity;
		for(integer i = 1; i<=opp.TotalOpportunityQuantity; i++){
		   job__c ajob = new job__c(Name = 'Job'+i, Amount__c = Amount );
		   listofjob.add(ajob);
		}
	}
	if(listofjob.size()>0){
		insert listofjob;
	}
}


Hope this will help you. If does than mark it as the best answer so it can also help others in the future.

Many Thanks,
Sunil Rathore
Rizwan Ali 12Rizwan Ali 12
Please can you tell me the reason why we put it outside
Rizwan Ali 12Rizwan Ali 12
I tryed but it didn't work.
I created master detailed field related to Opportunity in Job Object.
Trigger is also active.
and call all requried field.
If(Trigger.IsAfter && Trigger.isInsert)
   {
       List<job__c> listofjob = new List<job__c>();
       for(Opportunity opp : Trigger.New)
       {
           Decimal Amount = opp.Amount / opp.TotalOpportunityQuantity;          
           String Opportunity;
           for(integer i = 1; i<=opp.TotalOpportunityQuantity; i++)
           {
               job__c ajob = new job__c(Name = 'Job'+i, Amount__c = Amount, listofjobs__c = Opportunity );
               listofjob.add(ajob);
           }
           
           if(listofjob.size()>0)
           {
               insert listofjob;
           }
       }
   }
User-added image
Sunil RathoreSunil Rathore
Hi Rizwan,

There is a governor limit for DML statements. To avoid hitting the governor limit it is a best practice to avoid writing DML statements inside the for loop. It won't affect the signal record but when you perform an operation on bulk record it will throw an error.

Please refer the below link for all salesforce governor limits:
https://developer.salesforce.com/docs/atlas.en-us.salesforce_app_limits_cheatsheet.meta/salesforce_app_limits_cheatsheet/salesforce_app_limits_platform_apexgov.htm

Hope this will help you.

Many Thanks,
Sunil Rathore
Sunil RathoreSunil Rathore
Hi Rizwan,

Please refer the below code. The trigger will execute only when the new Opportunity will create. It won't execute on the update of the opportunity.
If(Trigger.IsAfter && Trigger.isInsert){
	List<job__c> listofjob = new List<job__c>();
	for(Opportunity opp : Trigger.New){
		Decimal Amount = opp.Amount / opp.TotalOpportunityQuantity;
		for(integer i = 1; i<=opp.TotalOpportunityQuantity; i++){
		   job__c ajob = new job__c(Name = 'Job'+i, Amount__c = Amount, listofjobs__c =  opp.id);
		   listofjob.add(ajob);
		}
	}
	if(listofjob.size()>0){
		insert listofjob;
	}
}

Hope this will help you. If does than mark it as the best answer so it can also help others in the future.

Many Thanks,
Sunil Rathore
This was selected as the best answer
Rizwan Ali 12Rizwan Ali 12
Yes i create new OPP and
If(Trigger.IsAfter && Trigger.isInsert)
   {
	List<job__c> listofjob = new List<job__c>();
	for(Opportunity opp : Trigger.New){
		Decimal Amount = opp.Amount / opp.TotalOpportunityQuantity;
		for(integer i = 1; i<=opp.TotalOpportunityQuantity; i++){
		   job__c ajob = new job__c(Name = 'Job'+i, Amount__c = Amount, listofjobs__c = opp.Id );
		   listofjob.add(ajob);
		}
	}
	if(listofjob.size()>0){
		insert listofjob;
	}
}

i use this code but it didn't work