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
case commentscase comments 

Update Trigger best practices

Hello,

 

I'm fairly new to writing apex. I've written the following code to update the status of the case to match that of another dropdown(JIRA_Status2__c) on the case.  This trigger should only fire when JIRA_Status2__c changes and if the recordtype matches.

 

The code works, but I would like to know if I've written too much to achieve such a basic update.  Thanks for the input in advance. It wil help me to learn how to code better in the future.

trigger UpdateJiraOnlyRT on Case (after update) {

    //create list of case IDs
    List<ID>cid = new List<ID>();
   
    
    
    //add cases to list for only cases that has JIRA Status2 dropdown changed AND recordtype=012A00000017NFl

    for (Case c: Trigger.new){
        Case oldCase = Trigger.oldMap.get(c.Id);
        if((oldCase.JIRA_Status2__c != c.JIRA_Status2__c) && (c.RecordTypeID=='012A00000017NFl')){
            cid.add(c.ID);}
    }
  
   //update the list  
    List<Case>updatecase =[SELECT Id, Status, JIRA_Status2__c from Case WHERE Id IN:cid];
    for(Case c2:updatecase){
        c2.Status = c2.JIRA_Status2__c;
    }
    
    update updatecase;
        
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Alex.AcostaAlex.Acosta

It really depends on what you want your end goal to be...

 

My best practice in general is to try and avoid using any static variable when possible, so I would first get rid of your static RecordTypeId.

 

Secondly I believe you can shorten this trigger... try this out and let me know if it helps... remember to properly ajust the RecordType query in my sample code.

 

trigger UpdateJiraOnlyRT on Case (before update) {
    
//use DeveloperName than standard name just because this tends to never change RecordType rt = [SELECT Id from RecordType WHERE SobjectType = 'Case' AND DeveloperName = 'DeveloperName' limit 1]; //create list of case IDs List<ID>cid = new List<ID>(); //add cases to list for only cases that has JIRA Status2 dropdown changed AND recordtype=012A00000017NFl for (Case c: Trigger.new){ Case oldCase = Trigger.oldMap.get(c.Id); if((oldCase.JIRA_Status2__c != c.JIRA_Status2__c) && (c.RecordTypeID == rt.Id)){ c.Status = c.JIRA_Status2__c; } }

 

 

All Answers

Alex.AcostaAlex.Acosta

It really depends on what you want your end goal to be...

 

My best practice in general is to try and avoid using any static variable when possible, so I would first get rid of your static RecordTypeId.

 

Secondly I believe you can shorten this trigger... try this out and let me know if it helps... remember to properly ajust the RecordType query in my sample code.

 

trigger UpdateJiraOnlyRT on Case (before update) {
    
//use DeveloperName than standard name just because this tends to never change RecordType rt = [SELECT Id from RecordType WHERE SobjectType = 'Case' AND DeveloperName = 'DeveloperName' limit 1]; //create list of case IDs List<ID>cid = new List<ID>(); //add cases to list for only cases that has JIRA Status2 dropdown changed AND recordtype=012A00000017NFl for (Case c: Trigger.new){ Case oldCase = Trigger.oldMap.get(c.Id); if((oldCase.JIRA_Status2__c != c.JIRA_Status2__c) && (c.RecordTypeID == rt.Id)){ c.Status = c.JIRA_Status2__c; } }

 

 

This was selected as the best answer
case commentscase comments

Thanks Alex for the quick reply.  This has been very helpful.  One last question if you don't mind.  In my example, I put all the updates into a list and then updated the list.  Whereas yours did the updates in the for loop.  Wouldn't governor limits come into play?

Alex.AcostaAlex.Acosta

Short answer No.

Long answer, I'm doing it slightly different from you. Your trigger event was after updates. I chose to do a before update trigger. Due to this, you are able to modify the data prior to it being soft inserted/updated into the database. The query you see at the top for recordtype will only be called once per transaction. Governor limits only live within the transaction phase which usually only last a couple of microseconds. You can do a mass update with my trigger, and it would only use 1 query every 200 records as records are processed in batches of 200.

Please let me know if you have any other questions. I'm not sure what your comfort level is yet with coding, but the cookbooks is always a good way to learn.

http://developer.force.com/cookbook/category/applogic/top

case commentscase comments

Thanks Alex!