You need to sign in to do that
Don't have an account?

Attachment Body - NULL in before insert /After Update Trigger of Attachment
Hi,
I am writing following code in Attachment Trigger.
I want to get the Body content of Attachment after update and before delete, but its returning null in both the cases.
Only in case of insert i am able to fetch the body content of attachment. Here is the code:
trigger attachmentHistoryManager on Attachment (after insert, after update,before delete)
{
List<AttachmentHistory__c> lstHistoryToBeIns = new List<AttachmentHistory__c>();
if(trigger.isBefore && trigger.isDelete)
{
for(Attachment objAttach : trigger.old)
{
AttachmentHistory__c objHistory = new AttachmentHistory__c();
system.debug('*****Printing Old Attachment Body data'+objAttach.body);
objHistory.Body__c = Encodingutil.base64Encode(objAttach.body);
lstHistoryToBeIns.add(objHistory);
}
}
if(trigger.isAfter)
{
for(Attachment objAttach : trigger.new)
{
AttachmentHistory__c objHistory = new AttachmentHistory__c();
system.debug(Encodingutil.base64Encode(objAttach.Body));
objHistory.Body__c = Encodingutil.base64Encode(objAttach.body);
lstHistoryToBeIns.add(objHistory);
}
}
if(lstHistoryToBeIns!=null && lstHistoryToBeIns.size()>0)
{
Database.SaveResult[] lsr = Database.insert(lstHistoryToBeIns);
}
}
Can anyone there please help me out.
thanks.
I had to fire a SOQL to retirve it ....Its working fine after writing following code.
for(Attachment objAttach : [Select ID,Body, BodyLength, ContentType,CreatedById,CreatedDate,Description,IsPrivate,LastModifiedById,LastModifiedDate,Name,OwnerId,ParentId, Parent.Name,SystemModstamp from attachment where id in: trigger.newMap.keySet()])
{
system.debug(objAttach.body);
}
for(Attachment objAttach : [Select ID,Body, BodyLength, ContentType,CreatedById,CreatedDate,Description,IsPrivate,LastModifiedById,LastModifiedDate,Name,OwnerId,ParentId, Parent.Name,SystemModstamp from attachment where id in: trigger.oldMap.keySet()])
{
// objAttach.body is accessible here
}
All Answers
what is the type of the field Body__c in the customobject?
i worked with your code .. but i am getting the body in the after insert block......
Agreed Kiran thats working in After Insert.
My question related to After Update and Before Delete...I am getting null there.
I had to fire a SOQL to retirve it ....Its working fine after writing following code.
for(Attachment objAttach : [Select ID,Body, BodyLength, ContentType,CreatedById,CreatedDate,Description,IsPrivate,LastModifiedById,LastModifiedDate,Name,OwnerId,ParentId, Parent.Name,SystemModstamp from attachment where id in: trigger.newMap.keySet()])
{
system.debug(objAttach.body);
}
for(Attachment objAttach : [Select ID,Body, BodyLength, ContentType,CreatedById,CreatedDate,Description,IsPrivate,LastModifiedById,LastModifiedDate,Name,OwnerId,ParentId, Parent.Name,SystemModstamp from attachment where id in: trigger.oldMap.keySet()])
{
// objAttach.body is accessible here
}
I have a similar requirement. I need to display body content of attachment in VF page.
My controller
My VF page
But the output I am getting is some weird alphabets. Something must be wrong with decoding-encoding.
Please Help!
Thanks
try with escape="true"
no luck :(
In VF page use
create a image
VF page
This will work...