Pre populate field values while creating new record in salesforce through UI

Sometimes while creating new records we need to have feature that allows us to have default values pre-populated in some fields. Salesforce does gives us an option to have default values pre-populated but it is not available for all the data type, like you cannot have default value for lookup field.

We can play a trick to pre-populate the values for whichever data type we want while creating a new record. For this we need to tweak the url that is getting used for creating the new record (URL that gets displayed when you click on new record and the edit page opens ). We need to make sure the modified url gets called whenever the new button is clicked. The the final url will be in addition to the original url that we get. We need to add id of the field that should be pre populated along with the value that we need, this needs to be done for all the fields you need pre-populated. Each id+value should be seperated by '&' character. For example, in the below url two fields are getting prepopulated with values 'e technologies' and 'cold'.

/001/e?retURL=/001/o'+'&acc2=e-Technologies&acc9=cold'

How do we get the id of the field?

You can get the id of the field by navigating to the field through set up or through source code/html of the edit page(Just open source code through browser and search for the label of the field)
Also as of now you cannot get the field id through apex hence you cannot make such a functionality dynamic and it should be pre-saved in your code,url.

Below is an example that pre-populates some fields while a new account record is created. Since you cannot write java script for a standard button visualforce page is used in-order to modify the original url. Just override the account new button with this visuflriec page.

 





Visualforce Page
Apex Class


 






 









For all the data type one can simple specify the field id followed by the value but for pre-populating lookup fields there are 2 ids that should be used. One for record id and one for name of the record. Id to be used for record id will have '_lkid' in the end and for name just use id without '_lkid'

Ex,
acc3_lkid=0019000000RP2f4'+'&acc3=University of arizona

here acc3_lkid with record id is for lookup id and acc3 is for populating name. The name that you specify could be anything and it does not make a difference if you give wrong name. Name of the record could be anything and need not match the real name of the record but has to be always specified without which look field will not be populated.

1 comment:

  1. Hi Yoganand ,
    I have tried same solution but it is not working .Even i edited New button with VF page also but it is not working.
    Controller
    Public class FieldValue_Prepopulator{

    Public FieldValue_Prepopulator(ApexPages.StandardController controller) {

    }

    Public pagereference Prepopulatemethod(){
    /*Build URL String to prepopulate field values*/
    String prePopulateURL;
    prePopulateURL = '/001/e?retURL=/001/o'+'&acc23=e Technologies';
    pagereference retPg= new pagereference(prePopulateURL);
    //this is important to set else it will go in infinite loop and keep opening the same page
    retPg.getParameters().put('nooverride', '1');
    retPg.setredirect(TRUE);
    return retPg;
    }
    }

    VF Page

    ReplyDelete