2

I'm trying to adapt to Tony Scott's tidy trigger pattern. I'm trying to prevent users from uploading more than certain size of attachments. I have written the Handler class for Attachment object and wrote a helper class in the bulkBefore method.

Interestingly, I'm facing

AttachmentTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.Attachment_TriggerHandler.bulkBefore: line 24, column 1

even though I have initialized the handler class variable.

private set<id> itemids = new set<id>();

Handler Class -

public with sharing class Attachment_TriggerHandler implements ITrigger
{   
    // Member variables
     private set<id> itemids = new set<id>();

    // Constructor
    public Attachment_TriggerHandler()
    {
    }

    public void bulkBefore()
    {
        if(Trigger.isInsert){
         itemids = ExpenseAttachmentSize_Helper.findLimitIds(Trigger.newMap.KeySet());

        }
    }

    public void bulkAfter()
    {
    }

    public void beforeInsert(SObject so)
    {
        Attachment att = (Attachment)so;
        if(itemIds.contains(att.Id)) 
        { 
          att.addError('File size cannot be greater than 1 MB');
        }
    }

    public void beforeUpdate(SObject oldSo, SObject so)
    {
    }

    public void beforeDelete(SObject so)
    {   
    }    

    public void afterInsert(SObject so)
    {
    }

    public void afterUpdate(SObject oldSo, SObject so)
    {
    }

    public void afterDelete(SObject so)
    {
    }

    public void andFinally()
    {
    }
}

Here is the Helper Class -

   Public class ExpenseAttachmentSize_Helper{

 public static set<id> findLimitIds(Set<Id> trigIds){

set<Id> attIdSet = new set<Id>();
Integer sizeLimit = Integer.ValueOf(Label.Expense_Attachment_Size_Limit);

   for(attachment att : [select body, parentId from attachment where id in :trigIds])
 {
  if (att.body.size() > sizeLimit && att.parentId.getsObjectType() ==   expense_item__c.sObjectType)
 {
    attIdSet.add(att.id);
   }

   }         
  return attIdSet;
  }
}
2
  • 2
    Record id generates after insert context not in before context.. You need to modify your code.
    – Ratan Paul
    Commented Jan 31, 2016 at 13:08
  • @Ratan facepalm
    – Jarvis
    Commented Jan 31, 2016 at 13:23

1 Answer 1

4

In before insert trigger, trigger.newMap is null as there is no record created in the database. So your line:

itemids = ExpenseAttachmentSize_Helper.findLimitIds(Trigger.New);

Will throw null pointer exception. Use trigger.new instead of a map.

And also the for loop on attachment in your method will not run as there is no Record Id created of the attachment in case of before trigger. Id is only created in after insert triggers:

public static set<id> findLimitIds(list<attachment> attNew){

         set<Id> attIdSet = new set<Id>();
         Integer sizeLimit = Integer.ValueOf(Label.Expense_Attachment_Size_Limit); //sizelimit is defined in the custom label

          for(attachment att : attNew)
              {
                if (att.body.size() > sizeLimit && att.parentId.getsObjectType() == expense_item__c.sObjectType)
                  {
                    attIdSet.add(att.id);
                  }

              }         
                return attIdSet;
     }
1
  • facepalm -_Q, edited your post
    – Jarvis
    Commented Jan 31, 2016 at 13:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .