+ Start a Discussion
Krishna1317Krishna1317 

Attachment Body null error

Hi All,

 

I have created a trigger on attachment. When I run it, it is giving an errror tht attachment body cannot be null.

 

trigger CTC_AttachmentDelete on Attachment (after delete) {

public list<Attachment> errorAttachments = new list<Attachment>();
public list<Attachment> updateAttachments = new list<Attachment>();
public list<CaseHistory> caseHistoryList = new list<CaseHistory>();

// Get the current user's profile name
Profile prof = [select Name from Profile where Id = :UserInfo.getProfileId() ];

// If current user is not a System Administrator, do not allow Attachments to be deleted
if (!'System Administrator'.equalsIgnoreCase(prof.Name)) {
for (Attachment a : Trigger.old) {
a.addError('You do not have permission to delete attachments.');
//page.addmesage();
}
}
else{

for(Attachment a : Trigger.old){

try{
//CaseHistory ch = new CaseHistory();
//ch.CaseId = a.ParentId;
//caseHistoryList.add(ch);

Deleted_Attachments__c dAttach = new Deleted_Attachments__c();
dAttach.Case__c = a.ParentId;

system.debug('case history list before insert is: '+dAttach);
insert dAttach;

Attachment attach=new Attachment();
Blob bodyString = a.Body;
attach.Body=bodyString;
attach.Name=a.Name;
attach.ContentType=a.contentType;
attach.ParentID=dAttach.Id;

system.debug('attachment before insert: '+attach+' old attachment is: '+a);
insert attach;

delete a;
}

catch(DmlException dex){
system.debug('exception while deleting attachment'+ dex);
}
}

/**if(caseHistoryList.size()>0){

try{
system.debug('case history list before insert is: '+caseHistoryList);
insert caseHistoryList;
}
catch(exception ex){
system.debug('error that occured while inserting caseHistoryList: '+ex);
}

}**/

}
}

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

I wonder if its something weird like the body isn't populated to protect the stack?

 

Have you tried querying back the attachment from the database to see if you can get at the body that way?

All Answers

bob_buzzardbob_buzzard

Where in the code is the exception being thrown?

 

I'm also a bit confused about the purpose of this trigger - it looks like you create a replica attachment and insert that, and then delete the contents of trigger.old just to make sure its really gone?

Krishna1317Krishna1317

Bob,

 

Thanks for your reply.

 

The purpose of the trigger is, when an attachment is deleted, it has to be saved in another custom object(Deleted_Attachments__c).

bob_buzzardbob_buzzard

I figured that bit out.  However, you have a bonus delete towards the end that attempts to delete the attachments passed to the trigger.

 

The key piece of information I'm missing though, is which line is throwing the error.

Krishna1317Krishna1317

The exception is as below. Looking at the exception, Looks like it is happening at 'attach.Body=bodyString;'

 

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Body]: [Body]
Krishna1317Krishna1317

Bob,

 

One more thing I observed from debug is as below. It shows that body is null in Old attachment but the BodyLength is 559184

 

 

 

15:47:30.191 (191592000)|USER_DEBUG|[40]|DEBUG|attachment before insert: Attachment:{Name=BRD v1_0-1-USBFSB2deploy.doc, Body=null, ParentId=a1kW0000000519eIAA, ContentType=application/msword} old attachment is: Attachment:{ParentId=500W0000001AaidIAC, OwnerId=005W0000000N6mrIAC, LastModifiedDate=2012-10-05 22:47:08, IsPrivate=false, SystemModstamp=2012-10-05 22:47:08, Body=null, Description=null, LastModifiedById=005W0000000N6mrIAC, Name=BRD v1_0-1-USBFSB2deploy.doc, CreatedById=005W0000000N6mrIAC, CreatedDate=2012-10-05 22:47:08, IsDeleted=false, Id=00PW0000000TwKqMAK, BodyLength=669184, ContentType=application/msword}

 

 

 

bob_buzzardbob_buzzard

I wonder if its something weird like the body isn't populated to protect the stack?

 

Have you tried querying back the attachment from the database to see if you can get at the body that way?

This was selected as the best answer
Krishna1317Krishna1317

Spot on. Your suggestion worked. I had to change the trigger as 'before delete' and do the query like you suggested.