How to Prepopulate Text Box Field on VisualForce Page on Page Load

by - 11:46 AM

How to pre-populate a textbox with some values in it on page load ?

Consider a situation where a business organization want to track the reason behind closing opportunity on loss of customer. Organization want custom field on a page, where user is supposed to answer bunch of questions, this field could be text field or rich-text field and on submitting answer, code automatically save the record and change opportunity stage from Open to Closed Lost

To meet this requirement, first things comes to mind is that, all I need is to design a visual-force page and add rich-text or text field  but how can I pre-populate this field with text (questions) provided by organization 

Does Salesforce offer to set default values to 'Text Field' or 'Rich-Text Field' ?
No, unfortunately not

Then here are two workarounds to meet this need

Approach 1 : Through controller extension

Assumption : Visualforce page with field lost_notes__c

VisualForce Page Code :

Controller Extension :  Controller extension are easy functionality supported by Salesforce where you can design custom method and call them to perform additional operation as you like

Controller code

Approach 2 : Use Java-Script on Page Load

Install jQuery (which I am big fan of) upload Jquery to your organization as static resource and then add reference to jQuery in page and develop custom method to pre-populate, find the Id of the lost notes box use DOM-Inspector in your native browser as shown in the code and and invoke jScript method on page load

That should resolve the problem

You May Also Like