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.

4

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
  • 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 '16 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 '16 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.