You need to sign in to do that
Don't have an account?
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);
}
}**/
}
}
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
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?
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).
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.
The exception is as below. Looking at the exception, Looks like it is happening at 'attach.Body=bodyString;'
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
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?
Spot on. Your suggestion worked. I had to change the trigger as 'before delete' and do the query like you suggested.