7

I'm required to change the title of each attachment on an upload to the name of the expense item. I have written a bulkified code

trigger ExpenseProof on Attachment (before insert) {

    set<id> itemids = new set<id>();
    for(attachment att:trigger.new){
        if(att.parentid.getSobjectType() == Expense_Item__c.sObjectType){
        itemids.add(att.ParentId);
     }
    }

   Map<Id,expense_item__c> itemMap = new Map<Id, expense_item__c>([select name from expense_item__c where id in :itemids]);

    for(attachment att:trigger.new)
    {
     if((att.parentid.getSobjectType() == Expense_Item__c.sObjectType) && itemMap.containsKey(att.ParentId))
            att.Name= itemmap.get(att.ParentId).name;
    }
}

and a non-bulkified code

    trigger ExpenseProof on Attachment (before insert) {

    for(attachment att:Trigger.new)
    {
        if(att.parentid.getSobjectType() == Expense_Item__c.sObjectType)
        {
            att.Name= [select name from expense_item__c where id =:att.ParentId].name;
        }
    }
}

Shall I ignore the bulkified one as I don't foresee a scenario where more than 200 attachments are going to be uploaded at once?

13

Even if you are 100% sure that there is no possibility you will not have 200 attachments to modify, you also have to think that other code might execute in the same execution context and if the code you are creating is not efficient, it might impact the code that would have succeeded if the resources used by your code were lower.

So always optimise your code to run with the lowest resource possible.

  • Why the trivial edit? – Adrian Larson Jan 26 '16 at 17:44
  • It didn't like how I was using "that" word twice in such a short space. – Mihai Neagoe Jan 26 '16 at 17:46
6

There is no Harm in adding this bulkification. NEVER say never....you could find the need to do a mass upload of attachments in the future, or another admin could do it as well.

ESPECIALLY since you already have the code written, go with the bulkified. In my opinion, I'd rather have something in place that I might never need, but be all set in the off chance that I do need it.....No risk in being overprepared for all scenarios.

If you think about it, what does NOT putting in the bulkified code save you?

  • Merry Christmas, didn't understand "If you think about it, what does NOT putting in the bulkified code save you?" – Jarvis Dec 25 '15 at 12:51
  • I just mean that what do you gain by putting in the non bulkified code. Just that it's not like there is any gain or benefit to not adding the bulkified code. – Chris Duncombe Dec 25 '15 at 13:20
  • Ok, I get it, thought there's some mistake with my bulkified code which I should correct – Jarvis Dec 25 '15 at 13:25
  • 4
    @Bennie Always prefer bulkified code. Just because you can't foresee a situation where more than a handful of records could exist in a trigger doesn't mean you won't later have a situation where you will such a feature. In fact, it's almost guaranteed to happen at some point. You have to consider all possibilities, and one such possibility is that someone will need this trigger to work with bulk uploads. – sfdcfox Dec 25 '15 at 15:11
  • 4
    @Bennie. The 1st project I ever worked on everything was supposed to be entered via the UI. The next thing we knew, the client was doing bulk uploads and things began to break because it didn't follow the formatting the UI provided. I had to re-write my trigger to accommodate that. Always be prepared for those types of scenarios. – crmprogdev Dec 25 '15 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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