The Apex CPU time limit exceeded error is a standard Salesforce error which occurs when the salesforce governor limit to run the Apex code has exceeded.The maximum CPU time to run the Apex code on the Salesforce servers is:
Limits.getCPUTime() - Returns the CPU time(in milliseconds) accumulated on the salesforce.com server in the current transaction.
Limits.getLimitCPUTime() - Returns the total CPU time (in milliseconds) accumulated on the salesforce.com servers in the current transaction.
Example :
Below code error is coming due to FOR inside FOR loop.
Set<String> setDependantValue = new Set<String>();
for(Payment_Process__c objPicklistValues : lstProduct_Line){
setDependantValue.add(objPicklistValues.Payment_Name__c);
}
Map<String, List<String>> mapDependantControllingValue = new Map<String, List<String>>();
for(String objDepVal : setDependantValue){
List<String> lstControllingValues = new List<String>();
for(Payment_Process__c objPicklistValues : lstProduct_Line){
if(objDepVal == objPicklistValues.Payment_Name__c){
lstControllingValues.add(objPicklistValues.Product_Family_Name__c);
}
}
mapDependantControllingValue.put(objDepVal, lstControllingValues);
}
Solution :
Issue is resolved by following optimized code.
Map<String, List<String>> mapDepValContVal = new Map<String, List<String>>();
for(Payment_Process__c prdLine : lstProduct_Line){
List<String> lstControllingValues = mapDepValContVal.get(prdLine.Line__c);
if(lstControllingValues == null)
lstControllingValues = new list<String>();
lstControllingValues.add(prdLine.Family__c);
mapDepValContVal.put(prdLine.Line__c, lstControllingValues);
}
- 10,000 milliseconds (Synchronous limit)
- 60,000 milliseconds (Asynchronous limit)
Limits.getCPUTime() - Returns the CPU time(in milliseconds) accumulated on the salesforce.com server in the current transaction.
Limits.getLimitCPUTime() - Returns the total CPU time (in milliseconds) accumulated on the salesforce.com servers in the current transaction.
Example :
Below code error is coming due to FOR inside FOR loop.
Set<String> setDependantValue = new Set<String>();
for(Payment_Process__c objPicklistValues : lstProduct_Line){
setDependantValue.add(objPicklistValues.Payment_Name__c);
}
Map<String, List<String>> mapDependantControllingValue = new Map<String, List<String>>();
for(String objDepVal : setDependantValue){
List<String> lstControllingValues = new List<String>();
for(Payment_Process__c objPicklistValues : lstProduct_Line){
if(objDepVal == objPicklistValues.Payment_Name__c){
lstControllingValues.add(objPicklistValues.Product_Family_Name__c);
}
}
mapDependantControllingValue.put(objDepVal, lstControllingValues);
}
Solution :
Issue is resolved by following optimized code.
Map<String, List<String>> mapDepValContVal = new Map<String, List<String>>();
for(Payment_Process__c prdLine : lstProduct_Line){
List<String> lstControllingValues = mapDepValContVal.get(prdLine.Line__c);
if(lstControllingValues == null)
lstControllingValues = new list<String>();
lstControllingValues.add(prdLine.Family__c);
mapDepValContVal.put(prdLine.Line__c, lstControllingValues);
}
No comments:
Post a Comment