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
Rahul SharmaRahul Sharma 

Update Rich text area field with image through code

Greetings board,

 

Is there a way by which we can update RTA field with image through code.

I'm aware about image can be inserted from local machine or by giving the web address but the problem is i want to update the RTA field through code.

 

I tried to debug the value stored in the RIA field after inserting the image in it.

system.debug([Select Image__c from account where id= '00190000007KFZr']);

(Account:{Image__c=<img src="http://www.w3schools.com/images/compatible_firefox.gif" alt="Firefox"></img>})

It can be seen that RTA field contains image as HTML but if we update same field with HTML value from debug, then that html is displayed as text instead of image.


One solution is to have a text field to store URL and display image using IMAGE() function in another formula field, But as the RIch text area field supports Image and HTML there must be a way by which we could achieve this.

 

Please share your thoughts on this and thanks for your time.

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

Found a solution,

If we update the RTA field with HTML value of the image then it works fine.

Code snippet for future reference:

Account objAccount = [Select Image__c from Account where Id = '00190000007KFZr'];
objAccount.Image__c = '<img alt="User-added image" src="http://www.diseno-art.com/images_2/for_gt_nav_ffffff-short.gif"></img>';
update objAccount;

 

All Answers

Rahul SharmaRahul Sharma

Found a solution,

If we update the RTA field with HTML value of the image then it works fine.

Code snippet for future reference:

Account objAccount = [Select Image__c from Account where Id = '00190000007KFZr'];
objAccount.Image__c = '<img alt="User-added image" src="http://www.diseno-art.com/images_2/for_gt_nav_ffffff-short.gif"></img>';
update objAccount;

 

This was selected as the best answer
J.J.E. van OsJ.J.E. van Os

Hello Rahul,


I was looking on google for some explaination about making an image through code. I did not refer to salesforce while it actually is intended for salesforce. Then i stumbled on this link.
It looks really promising but it somewhat differs from what i want to achieve (i think).

 

I want to make 3 custom fields in tab opportunity and place full image url in there so that i can use the api-names to relate them in my emailsjabloon.

The text fields are called Reclame 1, Reclame 2, Reclame 3:


Then i go to the fields and fill them with full URL's of images. EXAMPLE:  hxxp://ww<dot>something.jpg

 

and in my emailtemplate ii want to call them like this:

<img src="{!relatedTo.reclame_2__c}" height="100%" width="100%" /> (problem here is that because of the " " it will read as text and therefor not show any image as it not interper the code. I don't know a way to do it different yet. IT shows an img  field with icon but no actual image.

 

Second different approach:

I made 3 rich text fields after reading your post and gave them the same name Reclame 1, 2, 3.

This time copy the img code in there with full image url and then only placed the code --> {!relatedTo.reclame_2__c} in my emailtemplate.
This also did not work. I don't know why not, it did not place pure text in my emailtemplate. In fact it didn't show anything now.

 

Third approach:

 

I uploaded an image to documents within salesforce and made the image for external use.

From there i can use the ID code to call the image though i have to find out how i can do this exactly.

Can i use the answer you placed here for my third approach? it looks like it comes close and will try it though it is not exactly what i want.

If yes where do i use this code? in the formula editor field? how can i relate this to my emailtemplate.


3 custom fields that i can alter on user level for users so they don't have to change stuff in templates (wich they can't get to anyway).


Please if you know a way can you help and explain this to me?

Thanks!


Regards,

Stef Hofman