You need to sign in to do that
Don't have an account?
System.LimitException: Apex CPU time limit exceeded
Hello,
Requirement: To update four picklist field values with dependencies. As the numbers of picklist values are high it’s difficult to manage the Field Dependencies manually.
Product Family
Product Line
Product Module
Product Version
Design Approach: So we planned to go for customization. We used four custom settings to store the picklist values. Then update the picklist fields values using Metadata API. Below are the design of four custom settings.
Product Family
Family1
Family2
Family3
Product Line
Family1 - Line1
Family2 - Line2
Family3 - Line3
Product Module
Line1 - Module1
Line1 - Module2
Line2 - Module2
Product Version
Module1 - Version1
Module2 - Version1
Module2 - Version2
Problem: Though the above design works for less volume of data, I am getting “System.LimitException: Apex CPU time limit exceeded” error when the number of picklist values is high (i.e. more than 1000).
I have generated a Metadata Service class from the MetadataAPI WSDL.
I need a map of following format to pass into "UpdateMetadata" method(in Metadata Service class).
Map<String, List<String>> OR Map<DependantValue, List<ControllingValues>>
To get the data in above format from custom setting, I am using below code.
Set<String> setDependantValue = new Set<String>();
for(Product_Line__c objPicklistValues : lstProduct_Line){
setDependantValue.add(objPicklistValues.Product_Line_Name__c);
}
Map<String, List<String>> mapDependantControllingValue = new Map<String, List<String>>();
for(String objDepVal : setDependantValue){
List<String> lstControllingValues = new List<String>();
for(Product_Line__c objPicklistValues : lstProduct_Line){
if(objDepVal == objPicklistValues.Product_Line_Name__c){
lstControllingValues.add(objPicklistValues.Product_Family_Name__c);
}
}
mapDependantControllingValue.put(objDepVal, lstControllingValues);
}
I think the error is coming due to FOR inside FOR loop.
Is there any other way to optimize the code or any solution?
Thank you
Swagat
Hi,
My issue is resolved by following optimised code.
Map<String, List<String>> mapDepValContVal = new Map<String, List<String>>();
for(Product_Line__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);
}
All Answers
Hi,
My issue is resolved by following optimised code.
Map<String, List<String>> mapDepValContVal = new Map<String, List<String>>();
for(Product_Line__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);
}
{
public static void XmlRequest()
{
String response='<TRX>'
+'<XID>1NXGL0KPRKIBM</XID>'
+'<MPY>VISA</MPY>'
+'<CRD>'
+'<CLV>1</CLV>'
+'<EXP>1210</EXP>'
+'<SCN>****0002</SCN>'
+'</CRD>'
+'<RES>'
+'<RCD>0</RCD>'
+'<MSG>Service succeeded</MSG>'
+'<STM>2010-10-04T13:30:22.019628Z</STM>'
+'<ETM>2010-10-04T13:30:22.028491Z</ETM>'
+'</RES>'
+'<AUT>'
+'<MID>3000786</MID>'
+'<AUC>637039</AUC>'
+'</AUT>'
+'</TRX>';
//Creating instace of XmlStreamReader
XmlStreamReader xsr = new XmlStreamReader(response);
system.debug('response........'+ xsr);
while(xsr.hasnext())
{
/*
if(xsr.getEventType()==XmlTag.START_ELEMENT)
{
if('STM'==xsr.getLocalName())
{
if(xsr.getEventType()==XmlTag.END_ELEMENT)
{
break;
}//if2
else if(xsr.getEventType()==XmlTag.CHARACTERS)
{
system.debug('----GetEventType----'+xsr.getEventType());
}//IF
}//if
xsr.next();
*/}//while
}//method
}//class
I am having an error message Line: 57, Column: 1
System.LimitException: Apex CPU time limit exceeded
How to resolve this.
Trigger
trigger UpdateRelIntConIds5 on Contract_Group_Association__c (after delete, after insert, after update) {
Set<Id> accounts = new Set<Id>();
Set<Id> cons = new Set<Id>();
system.debug('old'+Trigger.old );
if (Trigger.old != null) {
system.debug('inside update');
for (Contract_Group_Association__c c : Trigger.old) {
if (c.Internal_Contract_Group__c != null)
cons.add(c.Internal_Contract_Group__c);
}
}
if (Trigger.new != null) {
system.debug('inside insert');
for (Contract_Group_Association__c c : Trigger.new) {
if (c.Internal_Contract_Group__c != null)
cons.add(c.Internal_Contract_Group__c);
}
}
if (cons.size()> 0){
List<Customer_Groups_Association__c> conGroup = [select Internal_Contact_Group__c from Customer_Groups_Association__c where Internal_Contract_Group__c in :cons];
system.debug('conGroup'+conGroup.size());
for (Customer_Groups_Association__c cx : conGroup) {
accounts.add(cx.Internal_Contact_Group__c);
system.debug('cx.Internal_Contact_Group__c: '+cx.Internal_Contact_Group__c);
}
}
system.debug('Groups'+accounts);
UpdateRelatedInternalContractIds ur = new UpdateRelatedInternalContractIds(accounts);
ur.go();
}
apex class:
global class UpdateRelatedInternalContractIds {
public Set<Id> accounts {get; set;}
public UpdateRelatedInternalContractIds(Set<Id> accounts) {
this.accounts = accounts;
}
public void go() {
Set<Id> contractGroups = new Set<Id>();
Map<String, Set<String>> customers = new Map<String, Set<String>>();
if (accounts == null)
return;
List<Customer_Groups_Association__c> clist = [select id, Internal_Contract_Group__c, Internal_Contact_Group__c from Customer_Groups_Association__c where Internal_Contact_Group__c in :accounts];
if (clist != null) {
for (Customer_Groups_Association__c c : clist) {
contractGroups.add(c.Internal_Contract_Group__c);
Set<String> t = customers.get(c.Internal_Contact_Group__c);
if (t == null) {
t = new Set<String>();
customers.put(c.Internal_Contact_Group__c, t);
}
t.add(c.Internal_Contract_Group__c);
}
}
Map<String, Set<String>> contractIds = new Map<String, Set<String>>();
List<Contract_Group_Association__c> dlist = [select id, Internal_Contract__r.Internal_Contract_ID__c, Internal_Contract_Group__c from Contract_Group_Association__c where Internal_Contract_Group__c in :contractGroups];
if (dlist != null) {
for (Contract_Group_Association__c d : dlist) {
Set<String> t = contractIds.get(d.Internal_Contract_Group__c);
if (t == null) {
t = new Set<String>();
contractIds.put(d.Internal_Contract_Group__c, t);
}
t.add(d.Internal_Contract__r.Internal_Contract_ID__c);
}
}
List<Account> customerList = [select id, Related_Internal_Contract_IDs__c from Account where id in :accounts];
for (Account c : customerList) {
String result = '';
if (customers.containsKey(c.id)) {
system.debug('customer@@' + c);
for (String s : customers.get(c.id)) {
if (!contractIds.containsKey(s))
continue;
for (String x : contractIds.get(s)) {
if (result != '')
result += ',';
result += x;
}
}
}
c.Related_Internal_Contract_IDs__c = result;
}
update customerList;
}
}