You need to sign in to do that
Don't have an account?

Need Help Tuning code
I have an object called Order Request which can have multiple IDs associated with it (through a junction object). When the Order Request install status is changed to Completed, I have a trigger which loops through the IDs and updates values. (ie change anythign with Add-Realtime to Enabled-Realtime). This code is working fine unless there are a large number of IDs on the order request. I was originally running into a heap error but figured that out. Now i am running into a "too many script statements" error. I know whey I am getting the error. There are approximately 190 picklists on each ID which this code is processing, and the code about executes at least 2 lines for each picklist value, so at a very basic level, for 25 IDs I have 190*25*5=9750. Add some overhead to that and I have hit my limit of 10200 scripts. I was hoping that somebody might have a good way to avoid looping through each one of these picklist values or have another inventive way of getting around this. Here is the code:
public static void processIDs (Order_Request__c[] orReq){
List<Townsend_User_Login_ID__c> orderRequestIDs = new List<Townsend_User_Login_ID__c>();
List<Townsend_User_Login_ID__c> orderRequestDirectIDs = new List<Townsend_User_Login_ID__c>();
List<Order_Request_Townsend_User_Login_IDs__c> orderIDlinks = new List<Order_Request_Townsend_User_Login_IDs__c>();
List<String> exchanges = new List<String>();
Map<String, Schema.SObjectField> IDfields = Schema.SObjectType.Townsend_User_Login_ID__c.fields.getMap();
String qString = '';
for (String f: IDfields.keyset()){
if(f.substring(0,1) == 'X' ||
f == 'SCB_SWX_SCOACH_BASIC__c' ||
f == 'SCA_SWX_SCOACH_ADVANCED__c' ||
f == 'LSQ_LSE_INTL_L1_NEWS__c' ||
f == 'LSP_LSE_DOM_L1_NEWS__c' ){
qString = qString + ', ' + f + ' ';
exchanges.add(f);
}
}
ID recTypeId = Schema.SObjectType.Order_Request__c.getRecordTypeInfosByName().get('Townsend Submitted').getRecordTypeId();
for (Order_Request__c o : orReq){
if (o.RecordTypeId == recTypeId ){
if(o.Install_Status__c <> 'Completed'){
continue;
}else{
String thisQString1 = 'select id ' + qString + 'from Townsend_User_Login_ID__c where id IN (select Townsend_User_Login_ID__c from Order_Request_Townsend_User_Login_IDs__c where Order_Request__c = \''+o.id+'\')';
List<Townsend_User_Login_ID__c> townList = new List<Townsend_User_Login_ID__c>();
for (Townsend_User_Login_ID__c t : Database.query(thisQString1)){
for(String field: exchanges){
String value = String.valueOf(t.get(field));
if(value <> 'Not Enabled' && value <>'Enabled-Realtime' && value <> 'Enabled-Delayed'){
if (value == 'Add-Realtime'){t.put(field, 'Enabled-Realtime');
}else if(value == 'Add-Delayed'){t.put(field, 'Enabled-Delayed');
}else if(value == 'Delete-Realtime' || value == 'Delete-Delayed'){
t.put(field, 'Not Enabled');
}//end if-else
}
}
townList.add(t);
System.debug('*** Heap Size is '+Limits.getHeapSize());
if (Limits.getLimitHeapSize() - Limits.getHeapSize() < 50000 ){
update(townList);
townList.clear();
}
}//end for loop
update(townList);
townList.clear();
}//end if-else
}
}
}