Default Value for Standard Fields in Salesforce

 In this blog you will learn a workaround to set Default value for Standard Fields in Salesforce.

In below example I am having a Custom Object Zoom_Data__c and I will auto-populate Name field in it.

To implement the workaround please follow below steps : 

Step 1 : 

Create a Visualforce page as shown below : 

1
2
3
4
5
<apex:page standardController="Zoom_Data__c" >
 <script>
      window.top.location.href = '/lightning/o/Zoom_Data__c/new?nooverride=1&defaultFieldValues=Name=Test';
  </script>
</apex:page>

In above VF page I am using a URL hack to pass the default value in the standard field. You can defaultFieldValue parameter in URL to pass the default value as shown in above example.

Step 2:

Override the new button of the Object with this Visualforce page : 



This will do the trick, now try to create a new record of the Object and you will be having default value there.


How to Pass default parameters ?

To pass a single value you can use below URL : 
1
/lightning/o/Contact/new?defaultFieldValues=OwnerId={!Value}

You can bind variables in the URL or pass the String directly.

To pass multiple value you can use below URL : 
1
/lightning/o/Contact/new?defaultFieldValues=OwnerId={!Value},AccountId={!Value}


Checkout complete tutorial below  :

 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Keep Coding 

Post a Comment

8 Comments

  1. How to pass something like this - defaultFieldValues=FieldName=id1,id2 ?
    Multiple values for same field.

    ReplyDelete
    Replies
    1. Hi Simran,

      What is the field type ?

      Delete
    2. Hey,

      I'm trying to populate WhoId with multiple values. 'Shared Activities' is enabled for my org. Apparently we cant do it with apex, can we?

      Delete
    3. Event - WhoId field - to populate with multiple contact ids selected in a list view.

      Delete
    4. Simran, have you tried it by passing the list as the value ?

      Delete
    5. Yes, I tried passing as list and using other separators as well but no luck so far. I reckon we can't pass more than one id in whoid field via apex and can only be done via UI.

      Delete
    6. &defaultFieldValues=WhoId='+selectedContactsIDString;
      String of ids selected in list view separated by comma. I tried other separators and also a list instead of string.

      Delete
    7. Seems like a limitation to set the default values ! I will do some more research on it and let you know.

      Delete