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

13 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
  2. Hi Kapil, this is fantastic demonstration, thank you.
    My use case is a bit more complex - I need to override the new opportunity button, and the default value for the Opportunity currency need to be that of the Account.
    So I need to query the account object from which I'm creating the new opportunity. Is there a line of code I can add to your example above that will provide such solution?
    Thanks,
    Tal

    ReplyDelete
    Replies
    1. Yes, as it's a vf page which is sending the default parameters maybe you can add a apex class there and get the selected account based on the Id !

      Delete
  3. this URL is not support in salesforce mobile appp

    ReplyDelete
  4. Hi. The default value is working fine but when we click cancel button, it takes back to Home page rather than taking it back to Object home page. Is there any way to handle it?

    ReplyDelete
    Replies
    1. Hi Rohit, might be some standard functionality working behind it. One thing I can suggest here, check how you can handle retUrl parameter while creating the url that might help!

      Delete