2

I want to compare a text with a attachment that is added to a custom object. For example, check if the word "Test" is placed in the attachment body.

1 Answer 1

5

You just need to convert the Body data (Blob Type) to text (String type).

Attachment record; // defined in your context somewhere
Blob body = record.Body; // Blob data type
String content = body.toString(); // converted to text
system.debug(content.contains('Test')); // string manipulation as needed
2
  • 1
    Hi Adrian,Thanks for your response but when I run this code getting an error that says "Blob is not a valid UTF-8 string", below is the code which I checked, attachment tpye might be a word or a PDF, "Attachment record= [Select Id,ParentId, Name,body,ContentType From Attachment where ParentId='a0328000008srNO']; Blob body = record.Body; // Blob data type String content = body.toString(); // converted to text system.debug(content.contains('Test')); // string manipulation as needed
    – Karti K
    May 3, 2016 at 4:22
  • 1
    Where are you getting the files from? You can't really work around the error in Apex. salesforce.stackexchange.com/questions/11805/…
    – Adrian Larson
    May 8, 2016 at 16:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .