+ Start a Discussion
jonathanbernddf20131.387825590468351E12jonathanbernddf20131.387825590468351E12 

Help to bulkify working trigger better

Hi folks
Still new but getting there bit by bit. I have this trigger, It is supposed to update certain specific fields based on different picklist values. I'm thinking it's not bulkified like it should be. I'm thinking (but could be wrong) I need to make a set/list and update the values of the set before letting it go ahead and do it's stuff, so I can do everything with one update.  Here's the trigger.

trigger updateAppNum on CustomObject__c (before insert, before update) {

   //Get set of Incomplete ids
    List<Integer> appUps = new List<Integer>();
   
    for(CustomObject__c ap :trigger.new) {
        if (ap.Status__c == 'Incomplete') {
          ap.AppInc__c = 1;
        }
        if (ap.Status__c == 'Complete - Pending Review') {
          ap.AppCompP__c = 1;
        }
        if (ap.Status__c == 'Complete - Decision Made') {
          ap.AppCompD__c = 1;
        }
        if (ap.Status__c == 'Deferred') {
          ap.AppDef__c = 1;
        }
        if (ap.Status__c == 'Withdrawn') {
          ap.AppW__c = 1;
        }
    }

}
Best Answer chosen by jonathanbernddf20131.387825590468351E12
sfdcfoxsfdcfox
Your code is perfectly fine for now. Bulkification refers to collapsing DML operations and queries into single statements to avoid running into limits. You have no queries nor any DML statements, so your code works as expected. Since the trigger is a "before" event, your records will automatically have the correct values when the trigger completes. You do not need to perform any DML statement to make the values "stick."

All Answers

sfdcfoxsfdcfox
Your code is perfectly fine for now. Bulkification refers to collapsing DML operations and queries into single statements to avoid running into limits. You have no queries nor any DML statements, so your code works as expected. Since the trigger is a "before" event, your records will automatically have the correct values when the trigger completes. You do not need to perform any DML statement to make the values "stick."
This was selected as the best answer
jonathanbernddf20131.387825590468351E12jonathanbernddf20131.387825590468351E12
Thanks. That's great to know.