function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SGulSGul 

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.

Best Answer chosen by Admin (Salesforce Developers) 
SGulSGul

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

kiranmutturukiranmutturu

what is the type of the field Body__c in the customobject?

kiranmutturukiranmutturu

i worked with your code .. but i am getting the body in the after insert block......

SGulSGul

Agreed Kiran thats working in After Insert.

My question related to After Update and Before Delete...I am getting null there.

SGulSGul

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

}

 

This was selected as the best answer
communitycommunity

I have a similar requirement. I need to display body content of attachment in VF page.

 

My controller

Public string getBody(){
        List<Attachment> attach = [Select Id,body from Attachment where ParentId=:crf.Id limit 1];
        if(attach.size()>0){
          String sBody = Encodingutil.base64Encode(attach[0].body);
            return sBody; 
        }
        return sBody;    
    }

 My VF page

 

<apex:outputText value="{!Body}"/>

 

But the output I am getting is some weird alphabets.  Something must be wrong with decoding-encoding. 

 

UEsDBBQABgAIAAAAIQBxDjkrcAEAAKAFAAATANsBW0NvbnRlbnRfVHlwZXNdLnhtbCCi1wEooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
/CNiNB1PFAvx7HKl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s/3cJ5maTr23/pw1gJ6e6XxLTlshOK6z2maYdjHc5fgRdKH1UZwVyNVNrnO3kBsa5fTSZzEnj26oQ+R4pnwEhX8fviYUWSYLdAS1mYqsDALnniGX+j0Bo31BZ3+iR7eoLG+oIdBXSBHSSWTJYbnnyAizUbJY0Cc/5/iGwAA//8DAFBLAwQUAAYACAAAACEA6aYluIIGAABTGwAAEwAAAHhsL3RoZW1lL3RoZW1lMS54bWzsWU9v2zYUvw/YdyB0b20nthsHdYrYsZutTRvEboceaZmWWFOiQNJJfRva44ABw7phlwG77TBsK9ACu3SfJluHrQP6FfZISrIYy0vSBhvW1YdEIn98

 

Please Help! 

 

Thanks

 

kiranmutturukiranmutturu

try with escape="true"

communitycommunity

no luck :(

RatanRatan
Just to solve above issue..
Public string getBody(){

        List<Attachment> attach = [Select Id,body from Attachment where ParentId=:crf.Id limit
        String sBody = '';
        if(attach.size()>0){

          String sBody = Encodingutil.base64Encode(attach[0].body);

            return sBody;

        }
        return sBody;
    }


In VF page use

create a image 

VF page
 
<img alt="Image" src="data:image/jpeg;base64,{!Body}  " />

This will work...