+ Start a Discussion
harika78harika78 

Read the content of an attachment from an apex class

Hi

I am trying to read the content of an attachment from an apex class. 

 Attachment att=   [Select Id,a.ParentId, a.Name,a.body,a.ContentType From Attachment a where ParentId=:contactId limit 1];
           System.debug('Attachment body : '+ att.body);
        System.debug('Attachment body : '+ att.ContentType);

 the attachment body  content is displayed as :Blob[176680].
the body of the attachment is base64. I want to see the content of the body in plain english.

 

Thanks

APathakAPathak
Hi,
Have you tried doing this :

System.debug(att.body.toString());
Yoganand GadekarYoganand Gadekar

You can try with following code to read your attachment. Each input value in system debug should give you one line of the attachment.

 

Attachment att=   [Select Id,ParentId, Name,body,ContentType From Attachment  limit 1]; 

String[] filelines = new String[]{};
public string nameFile{get;set;}
nameFile=att.body.toString();       
filelines = nameFile.split('\n');       
 

for(Integer i=1;i<filelines.size();i++)        {
  String[] inputvalues = new String[]{};           
   inputvalues = filelines[i].split(',');
   system.debug('%%%%%%% each Line::::: '+inputvalues );
}

 

 

Mark this as answer if it helps you, so that others can benifit from this too.

harika78harika78

Hi

 

Tried the code snippet . I am getting the follwoing error :

Blob is not a valid UTF-8 String.

 

The following line of code is causing the error:

myFile=att.body.toString();

 

I am still struck with this .

 

Please help.

Yoganand GadekarYoganand Gadekar

Please post your full code so that it becomes easy to understand what is causing the error. 

 

 

harika78harika78

I am able to resolve that error by  using EncodingUtil.base64Encode(att.body).The origin of error was att.body.toString();

 

 

Attachment att=   [Select Id,ParentId, Name,body,ContentType From Attachment where ParentId=:contactId ];
     System.debug('Attachement  Body:'+ att.body);
       myFile=EncodingUtil.base64Encode(att.body);
        System.debug('Attachement  Body myFile:'+ myFile);

 

But with the above code I am able to get a long utf-8 encoded string instead of the text in the attachment. Could you please help me read the content of the attachmnet in plain english

 

 

Thanks
        
       

 

Yoganand GadekarYoganand Gadekar

Try following and see if it helps:

decode the encoded string inot its normal form of blob and then apply the initial logic of converting it into string.

 

Attachment att=   [Select Id,ParentId, Name,body,ContentType From Attachment where ParentId=:contactId ];

String[] filelines = new String[]{};
public string MyFile{get;set;}

MyFile=EncodingUtil.base64Encode(att.body);

Blob MyBlob=EncodingUtil.base64Decode(att.body);

 

MyFile=MyBlob.toString();       
filelines = MyFile.split('\n');       
 

for(Integer i=1;i<filelines.size();i++)        {
  String[] inputvalues = new String[]{};           
   inputvalues = filelines[i].split(',');
   system.debug('%%%%%%% each Line::::: '+inputvalues );
}

 

 

Mark this as answer if it helps you, so that others can benifit from this too. Hit kudos if this helps.

APathakAPathak
Hi,
What is the type of attachement you are trying to read?
BittusBittus
Hi All, 
I need to create/generate records from an inbound mail attachment(Email service). I created an email handler class and an email service class in order to receive an email with attachment. I am able to read the attachment,  but am not able to create records from the data present in attachment.
 Can anyone edit this inorder to meet the requirement.
Here is my code:-

Email Handler:-
-------------------------

global class myHandler implements Messaging.InboundEmailHandler {
      global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
          Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
          return result;
      }
  }

Email service:-
-----------------------------------

global class ProcessJobApplicantEmail implements Messaging.InboundEmailHandler {

  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {

    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

    Contact contact = new Contact();
    contact.FirstName = email.fromname.substring(0,email.fromname.indexOf(' '));
    contact.LastName = email.fromname.substring(email.fromname.indexOf(' '));
    contact.Email = envelope.fromAddress;
    insert contact;

    System.debug('====> Created contact '+contact.Id);

    if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
      for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
        Attachment attachment = new Attachment();
        // attach to the newly created contact record
        attachment.ParentId = contact.Id;
        attachment.Name = email.binaryAttachments[i].filename;
        attachment.Body = email.binaryAttachments[i].body;
        insert attachment;
      }
    }

    return result;

  }

}
Mitesh SuraMitesh Sura
Any luck with this? @Yoganand Gadekar, the snippet you provided is throwing error on line
Blob MyBlob=EncodingUtil.base64Decode(att.body);
Also, what is the use of below line, because it gets overwritten couple of lines later
MyFile=EncodingUtil.base64Encode(att.body);


 
Anirudh MadhusudanAnirudh Madhusudan
Anyone got the solution for this issue? Stuck with the same thing here