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
13 Comments
How to pass something like this - defaultFieldValues=FieldName=id1,id2 ?
ReplyDeleteMultiple values for same field.
Hi Simran,
DeleteWhat is the field type ?
Hey,
DeleteI'm trying to populate WhoId with multiple values. 'Shared Activities' is enabled for my org. Apparently we cant do it with apex, can we?
Event - WhoId field - to populate with multiple contact ids selected in a list view.
DeleteSimran, have you tried it by passing the list as the value ?
DeleteYes, 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&defaultFieldValues=WhoId='+selectedContactsIDString;
DeleteString of ids selected in list view separated by comma. I tried other separators and also a list instead of string.
Seems like a limitation to set the default values ! I will do some more research on it and let you know.
DeleteHi Kapil, this is fantastic demonstration, thank you.
ReplyDeleteMy 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
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 !
Deletethis URL is not support in salesforce mobile appp
ReplyDeleteHi. 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?
ReplyDeleteHi 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