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;
}
}