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
Jina ChetiaJina Chetia 

Too many script statements: 50001 in a trigger

Hi,

 

I am getting 'Too many script statements: 50001' exception in my trigger when I do a mass insert. I have not put any DML statements inside any for loop but still I am getting the error.

 

I have a object named Goal and the trigger is on insert. I need to insert all junction object records associated with the Goal object whenever any Goal record is inserted. There are 3 junction objects associated with the Goal - Insurance/Asset/PortfolioReport.

 

Here is the piece of code

 

trigger CreateGoalAssociations on Goal__c (after insert) {
	List<GoalAssetAssociation__c> assetList = new List<GoalAssetAssociation__c>();
	List<GoalInsuranceAssociation__c> insuranceList = new List<GoalInsuranceAssociation__c>();
	List<GoalIAULIPAssociation__c> pReport = new List<GoalIAULIPAssociation__c>();
	Map<Id, Id> entityIds = new Map<Id, Id>();
	Double d= 0;
	Map<Id, Id> goalAccountMap = new Map<Id, Id>();
	List<Goal__c> goalList = new List<Goal__c>();
	
	for(Goal__c g:Trigger.new){
		goalAccountMap.put(g.Id, g.Entity__c);
		goalList.add(g);
	}
    for(Account a: [select Id,Parent_Entity__c from Account where Parent_Entity__c IN :goalAccountMap.values()]){
		entityIds.put(a.Id, a.Id);
	}
    for(Asset__c a: [select Id, Asset_Type__c from Asset__c where Entity__c IN :goalAccountMap.values()
    					OR Entity__c IN :entityIds.keySet()]){
    	
    	for(Goal__c g: goalList){
    		GoalAssetAssociation__c gA = new GoalAssetAssociation__c(Goal__c = g.Id, Asset__c = a.Id, Allocation__c = d);
    		assetList.add(gA);
    	}
    }
    
    for(Insurance__c i: [select Id, Policy_Number_or_Name__c from Insurance__c where Entity__c IN :goalAccountMap.values()
    						OR Entity__c IN :entityIds.keySet()]){
    	for(Goal__c g:goalList){
    		GoalInsuranceAssociation__c gI = new GoalInsuranceAssociation__c(Goal__c = g.Id, Insurance__c = i.Id, Allocation__c = d);
    		insuranceList.add(gI);
    	}
    	
    }
    
    for(Portfolio_Report__c p: [select Id, Portfolio_Type__c  from Portfolio_Report__c where Entity__c IN :goalAccountMap.values()
    							OR Entity__c IN :entityIds.keySet()]){
    	for(Goal__c g:goalList){
    		GoalIAULIPAssociation__c gP = new GoalIAULIPAssociation__c(Goal__c = g.Id, Portfolio_Report__c = p.Id, Allocation__c = d);
    		pReport.add(gP);
    	}
    }
     insert assetList;
     insert insuranceList;
     insert pReport;
     
}

 

Is there any way I can modify the code to avoid the errors. I have highligted the places where I am getting the error.

 

Any help on this will be very much appreciated.

 

Many Thanks,

Jina

_Prasu__Prasu_
 for(Insurance__c i: [select Id, Policy_Number_or_Name__c, Portfolio_Report__c,Allocation__c from Insurance__c where Entity__c IN :goalAccountMap.values() 	OR Entity__c IN :entityIds.keySet()]){
    	for(Goal__c g:goalList){
    		GoalInsuranceAssociation__c gI = new GoalInsuranceAssociation__c(Goal__c = g.Id, Insurance__c = i.Id, Allocation__c = d);
    		insuranceList.add(gI);
GoalIAULIPAssociation__c gP = new GoalIAULIPAssociation__c(Goal__c = g.Id, Portfolio_Report__c = p.Id, Allocation__c = d);
    		pReport.add(gP);
} }
Jina ChetiaJina Chetia

Sorry Prasanna, I did not understand the solution.

 

I saw that you have combined both the loop but that is not possible as the values are being retrived from two different object

 

 

     

GoalInsuranceAssociation__c gI = new GoalInsuranceAssociation__c(Goal__c = g.Id, Insurance__c = i.Id, Allocation__c = d);
    		insuranceList.add(gI);
GoalIAULIPAssociation__c gP = new GoalIAULIPAssociation__c(Goal__c = g.Id, Portfolio_Report__c = p.Id, Allocation__c = d);
    		pReport.add(gP);

 

 As you can see that Portfolio Report data are being pulled from Portfolio_Report__c object and Insurance data are being pulled from Insurance__c object. So iterating thru Insurance object will not give Portfolio Report data, so I one in red below is not correct.

 

 for(Insurance__c i: [select Id, Policy_Number_or_Name__c, Portfolio_Report__c,Allocation__c from Insurance__c where Entity__c IN :goalAccountMap.values() 	OR Entity__c IN :entityIds.keySet()]){

 

AcMEGXAcMEGX

Hello Jina,

 

Nested FOR loops are usually cause "Too many script statements" governor limit.

 

Here is a suggestion to reduce the number of statements...

 

 

List<Insurance__c> listInsurances = [select Id, Policy_Number_or_Name__c from Insurance__c where Entity__c IN :goalAccountMap.values()OR Entity__c IN :entityIds.keySet()]];
List<Portfolio_Report__c> listPorfolioReports = [select Id, Portfolio_Type__c  from Portfolio_Report__c where Entity__c IN :goalAccountMap.values() OR Entity__c IN :entityIds.keySet()];
for(Goal__c g:goalList)
{
	for(Insurance__c insurance:listInsurances)
	{
		// script statements
	}
	for(List<Portfolio_Report__c>  portfolioReport:listPorfolioReports)
	{
		// script statements
	}
}

 

regards,

AcMEGX