2

I am writing a trigger to Insert a Image in a record which will be posted via feed

string img;

trigger will save if I define "img" has a String on line 3 & "f.ContentData.tostring();" on line 7

If I tried to convert string to Blob it will say

Blob is not defined

    trigger Update_image on FeedItem (after insert) {
        id Travelid ;
        string img; 
     List<Object__c> lst = new List<Object__c>();

             For(FeedItem  f : Trigger.new){
                Travelid = f.parentid;
                 img = f.ContentData.tostring();
              }

    for(Object__c i :[select id,image__c from Object__c where id=: travelid]){
       i.image__c =img;
       lst.add(i);
    }
       update lst ;
}

In above trigger I am getting Error like this

System.StringException: BLOB is not a valid UTF-8 string
  • 1
    What's the data type of image__c? You can't just stuff random binary data into a string and expect Good Things to happen. You're going to have to work harder than that. – sfdcfox Apr 29 '15 at 17:58
  • It's a rich text area field – user40138 Apr 29 '15 at 18:00
6

You have somehow embed the binary data into the field. Here's one way:

trigger Update_image on FeedItem (after insert) {
    id Travelid ;
    blob img; // Just binary data, thanks!
 List<Object__c> lst = new List<Object__c>();

         For(FeedItem  f : Trigger.new){
            Travelid = f.parentid;
             img = f.ContentData;
          }

for(Object__c i :[select id,image__c from Object__c where id=: travelid]){
   i.image__c = '<img src="data:image/jpeg;base64,'+EncodingUtil.base64Encode(img)+'"></img>';
   lst.add(i);
}
   update lst ;
}

Note that I've only copied/pasted your code (you may still have other issues here), but the idea is that you have to build an img tag yourself.

Edit:

So I just tried it, and salesforce automatically converts the image into a rtaImage file after saving. You can query the data back to see the changed image source. It'd be nice if this was clearly documented somewhere.

| improve this answer | |
0

I was not able to convert Blob to String.. So I just Uploaded the Image to Document and Giving the Image URL in Rich Text Area Field

trigger Update_image on FeedItem (after insert) {

               id Travelid ;
               string Names;
               Blob body;  
               List<Object__c> lst = new List<Object__c>();

                     For(FeedItem  f : Trigger.new){
                            Travelid = f.parentid;
                          body=  f.contentdata;
                            Names= f.parentid;       
                      }

               Document d = new Document();
               d.name = Names;
               d.Body =  decodedbody;
               d.folderId= '00l90000000jkYA';
               insert d;  

            for(itravel__c i :[select id,image__c from Object__c where id=: travelid]){

                i.image__c ='<img src="https://c.ap1.content.force.com/servlet/servlet.FileDownload?file='+d.id+'" width="500" height="500"></img>';
               lst.add(i);
            }
               update lst ;

    }
| improve this answer | |

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.