You need to sign in to do that
Don't have an account?
Eric Blaxton 11
How to skip trigger code based on record type
Hi and thanks in advance for any help.
Situation:
I wrote a web service to grab new cases from an external source. That part works fine, BUT an after update trigger fires when uploading new cases and i get a ''Apex CPU time limit exceeded". I thought if I skipped the Record Type it would skip the trigger code.
This line causes the error is the for loop, which occurs before the RT check:
Eric
Situation:
I wrote a web service to grab new cases from an external source. That part works fine, BUT an after update trigger fires when uploading new cases and i get a ''Apex CPU time limit exceeded". I thought if I skipped the Record Type it would skip the trigger code.
This line causes the error is the for loop, which occurs before the RT check:
for (Case cse : Trigger.new){
trigger CompleteResolutionTimeMilestone on Case (after update) { for (Case cse : Trigger.new){ system.debug('cse.RecordTypeId ' + cse.RecordTypeId); if (cse.RecordTypeId != Schema.Sobjecttype.Case.getRecordTypeInfosByDeveloperName().get('Maintenance').getRecordTypeId()){ if (UserInfo.getUserType() == 'Standard'){ DateTime completionDate = System.now(); List<Id> updateCases = new List<Id>(); for (Case c : Trigger.new){ if (((c.isClosed == true)||(c.Status == 'Closed'))&&((c.SlaStartDate <= completionDate)&&(c.SlaExitDate == null))) updateCases.add(c.Id); } if (updateCases.isEmpty() == false) milestoneUtils.completeMilestone(updateCases, 'Support Resolution Time', completionDate); } }// end if (cse.RecordTypeId != } }Regards,
Eric
A few reasons i can see for the possible time out. See Notes in your code example a better solution where we just fix the setting of variables and remove the inner loop
now, arguably, we could speed up the trigger further by testing the running user first before we get into the loop - example below
I think that should put you on the right track
Regards
Andrew
p.s. all code supplied as-is and uncompiled.
All Answers
A few reasons i can see for the possible time out. See Notes in your code example a better solution where we just fix the setting of variables and remove the inner loop
now, arguably, we could speed up the trigger further by testing the running user first before we get into the loop - example below
I think that should put you on the right track
Regards
Andrew
p.s. all code supplied as-is and uncompiled.
>> https://salesforce.stackexchange.com/questions/48736/how-to-run-a-trigger-only-for-specific-record-types
>> https://developer.salesforce.com/forums/?id=906F0000000AprlIAC
I have found the above example that has scenarios that run a trigger for a specific record type you can check and change this to match this as per your use case.
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks.