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
ab84ab84 

Extension to add default text/images to rich text

I have a controller extension that adds default value to a rich text field.  The line that adds the default value is:

nws.NewsBody__c = 'Test default info';

How would I update this to include an image as the default?
PrasanntaPrasannta (Salesforce Developers) 
Hi,

Try adding it as below-

nws.NewsBody__c = '/servlet/servlet.FileDownload?file=';
    List< document > documentList=[select name from attachment where
                                    Name='SamplePic'];
                                                                       
Hope this helps.
ab84ab84
Hi Prasannta, that looks like it could do the job, however is it pulling from an attachment list?  How would I change it to include a static resource?
PrasanntaPrasannta (Salesforce Developers) 
Hi,

Here is a code wherein any user can upload any image file and provides the document name where it will be stored and get dynamically inserted onto the Rich Text Area field.

Kindly refer the code below & modify it according to your requirement and test it before deploying it in your Org.

***************Apex Class***************

public class ImagInsertionIntoRichText
{

public String naam{get;set;}
public ID folderid{get;set;}
public Blob file{get;set;}

public void insrt()
{

Document d= new Document();

d.name = naam;
d.body=file; // body field in document object which holds the file.
d.folderid='00l90000000inFEAAY'; //folderid where the document will be stored insert d;
insert d;

Case cs = new Case();
cs.FirstExample__Comments__c = '<img src="https://rustagiankit-developer-edition--c.ap1.content.force.com/servlet/servlet.FileDownload?file='+d.id+'" width="500" height="281"></img>'; //FirstExample is namespace & Comments__c is Rich Text Area field
cs.Status = 'New';
cs.Origin = 'Web';
insert cs;
}
}
*************************************
***************VF page***************

<apex:page controller="ImagInsertionIntoRichText">
<apex:form>
  <apex:outputLabel value="Document Name"></apex:outputLabel>
  <apex:inputText id="name" value="{!naam}"/>

  <apex:outputLabel value="Upload Document"></apex:outputLabel>
  <apex:inputfile value="{!file}"></apex:inputfile>

  <apex:commandButton value="Save" action="{!insrt}" id="save"/>
</apex:form>
</apex:page>