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
Priyadarshini Manoharan 3Priyadarshini Manoharan 3 

How to pre-populate a long text or rich text field real time with values picked from a picklist field

To explain the scenario will make the example simple say if I have a picklist field Issue Type on Case which has values Access, Performance, Request etc.
There is another field Description which is a long text/rich text which needs to be pre-defaulted based on the value selected from the Issue Type field.
Issue Type and Description are fields on VF page with standard controller case
If Issue Type = Access, Description should be Please include full name and email address
If Issue Type = Performance, Description should be Detail when the problem started, specific error messages, performance now vs. before (benchmarks), application name (and environment if applicable) and browser
If Issue Type =  Request, Description should be Include application name, desired feature, details on use case
 
NagaNaga (Salesforce Developers) 
Hi Priyadarshini,

Everyone has the solution how to pre-populate fields after clicking on a custom ‘New’ button but no one has addressed the issue of hard coded IDs  in such a way that making new buttons easy and re-usable!

It’s a great way to populate fields on a Salesforce page however the problem with this method is that we are hardcoding the ID’s of the various labels on the record. If you are used to working on a sandbox before deploying on a production environment you will soon realize that having to change all these ID’s because you hard coded these, it is not only time-consuming, it is bad practice.

Attached to this post is a little script that I wrote that allows you create buttons without hard-coding anything, you can deploy the code between org’s and all will work !

Examples how to pre-populate different fields using the script (just add this into a Visualforce button), ofcourse you can add more fields to the URL if you need to prepopulate more than one:

1. Pre-populate a text field having API label: ‘Text__c’ on a custom object called: ‘Custom__c':

/apex/RedirectWithVariables?object=Custom__c&Text__c=Text Value

2. Pre-populate a picklist having API label: ‘Picklist__c’ on a custom object called ‘Custom__c':

/apex/RedirectWithVariables?object=Custom__c&Picklist__c=Picklist Value

3. Create a new record of a given record type (use the label of the record type instead of the name):

/apex/RedirectWithVariables?object=Custom__c&RecordType=Custom Record Type

4. Pre-populate an example lookup field having API label ‘Lookup__c’ on a custom object called: ‘Custom__c. Important to note that a lookup field requires two parameters, the ID of the record that it references and the text value of the field as seen by the user. To set the text value use the API name of the lookup and to set the ID value of the lookup append ‘ID_’ to the API name of the lookup as seen in the following example:

/apex/RedirectWithVariables?object=Custom__c&Lookup__c=Text Value&ID_Lookup__v=ID_Value

Hope this will anyone out there !!

Classed used in this post:

RedirectWithVariables.class

StaticFunctions.class

Page used in this post:

RedirectWithVariables.page

Best Regards
Naga kiran
Priyadarshini Manoharan 3Priyadarshini Manoharan 3

Hi Naga,

Thanks for the clear and detailed explanation. Yes I dont want to go with hardcoding the Id.

I think the above code would help only on a click of a button

However the real issue is here after I click on New Case button, and I pick the Issue Type field as say Performance then Description field should be auto-populated with "Detail when the problem started, specific error messages, performance now vs. before (benchmarks), application name (and environment if applicable) and browser" and then I should be able to add some details for the question in the description field like should mention when the problem started, error messages etc say I entered problem started at 3pm, getting error "cannot connect to server" and then click on the save button....

So once the record is saved
 the  Issue Type field should have value Performance

Description field should have value

Detail when the problem started, specific error messages, performance now vs. before (benchmarks), application name (and environment if applicable) and browser
 

started at 3pm, getting error "cannot connect to server"