Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to bulkify the trigger and error handler.Can someone point me if my trigger is bukified or not and handled error ?

 trigger KeyGenerator on Account (before insert,before update) {

       if(UpdatedAcc.Key__c == null)

            String KeyStartWith = UpdatedAcc.Name.replace(' ','');
            KeyStartWith = KeyStartWith.toLowerCase();
            //Get the first 5 letter of Account Name
            //Get the number generated randomly
            Double randomDouble=Math.random();
            //Crop the last five digits of this generated number
            String KeyEndWith=String.valueOf(randomDouble);

           catch(Exception e){
            UpdatedAcc.addError('Failed to update Key');
share|improve this question
possible duplicate of General trigger bulkification - best practices – Christian Deckert Aug 21 '14 at 18:20
up vote 4 down vote accepted

Yes, your trigger is bulkified in the sense that it handles SOQL queries and DML statements in a bulkified way (you don't have DML or SOQL). If the assignment of Key__c failed you will have working error handling - however If your other code fails you are not handling exceptions well.

What if Name is null for example - not sure it can happen - but if it can you would end up with an unhandled exception due to a null-pointer-exception on the replace call.

share|improve this answer
Name is standard field for all kind of objects and it is always required, but it can happend that it is an auto-number. From my point I can't see here any issue, try{} catch(){} block is most useful for DML operations, but in before triggers as this I don't see a point of using it. – Artur Kępczyński Feb 11 '14 at 5:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.