This post was inspired by an article found here: http://raydehler.com/cloud/clod/salesforce-url-hacking-to-prepopulate-fields-on-a-standard-page-layout.html
And also:
http://blog.deliveredinnovation.com/2012/09/17/create-a-custom-new-button-on-a-related-list-to-return-to-the-parent-record-after-clicking-save/
With credits to Wes on:
http://force201.wordpress.com/2012/01/22/hack-to-find-field-ids-allows-a-default-ui-new-page-to-be-pre-populated/
Everyone has the solution how to pre-populate fields after clicking on a custom ‘New’ button but no one has addressed the issue of hard coded IDs in such a way that making new buttons easy and re-usable!
It’s a great way to populate fields on a Salesforce page however the problem with this method is that we are hardcoding the ID’s of the various labels on the record. If you are used to working on a sandbox before deploying on a production environment you will soon realize that having to change all these ID’s because you hard coded these, it is not only time-consuming, it is bad practice.
Attached to this post is a little script that I wrote that allows you create buttons without hard-coding anything, you can deploy the code between org’s and all will work !
Examples how to pre-populate different fields using the script (just add this into a Visualforce button), ofcourse you can add more fields to the URL if you need to prepopulate more than one:
1. Pre-populate a text field having API label: ‘Text__c’ on a custom object called: ‘Custom__c’:
- /apex/RedirectWithVariables?object=Custom__c&Text__c=Text Value
2. Pre-populate a picklist having API label: ‘Picklist__c’ on a custom object called ‘Custom__c’:
- /apex/RedirectWithVariables?object=Custom__c&Picklist__c=Picklist Value
3. Create a new record of a given record type (use the label of the record type instead of the name):
- /apex/RedirectWithVariables?object=Custom__c&RecordType=Custom Record Type
4. Pre-populate an example lookup field having API label ‘Lookup__c’ on a custom object called: ‘Custom__c. Important to note that a lookup field requires two parameters, the ID of the record that it references and the text value of the field as seen by the user. To set the text value use the API name of the lookup and to set the ID value of the lookup append ‘ID_’ to the API name of the lookup as seen in the following example:
- /apex/RedirectWithVariables?object=Custom__c&Lookup__c=Text Value&ID_Lookup__v=ID_Value
Hope this will anyone out there !!
Classed used in this post:
RedirectWithVariables.class
StaticFunctions.class
Page used in this post:
RedirectWithVariables.page
Pretty awesome solution! Thanks a lot! I changed your code a bit to get it to work with the ‘new’ override anywhere instead of just the buttons on the page layout.
I did find that you should be careful when you are matching text from the HTML blob to populate your URL because this means you cannot add values to any field. For instance, on the account object Salesforce will pre-populate the Id section with ‘acc2’ and the parent account with ‘acc3’.
Reblogged this on Sutoprise Avenue, A SutoCom Source.
I’m using this blog as a learning exercise –
I’ve created the two Apex Classes attached within your blog:
RedirectWithVariables.class
StaticFunctions.class
And I’ve created the VF page attached within your blog:
RedirectWithVariables.page
When I create a custom button, I see where I can make the content source a VisualForce page, and then subsequently select the VF I just created.
My question is, where exactly do I create the URL? I would assume I create it within the VF page, but I don’t know.
Your page shows this script:
So where does the below go?:
/apex/RedirectWithVariables?object=Quote&Phase__c=Phase I
Thanks for any help!!!
Hi JT, it should not be a VF page , but either do URL and that URL as the content, or if you need some Javascript preprocessing, use OnClick Javascript and use window.parent.location.href = ‘URL’ to redirect to the page.
Let me know if this works. Furthermore I am continuing this blog on my personal site (www.frankneezen.com) so feel free to visit!
Very nice. A very reusable solution for a common problem. Thanks for sharing.
This is my VF page
This does not do anything. After Record Type choice, I get a blank screen.
This VF page is to overide the “New” button in Opportunities.
My objective is to create the opportuntiy with a custom picklist field named Service Country to be filled in by the contents of the User’s country.
So it would give something like /apex/RedirectWithVariables?object=Opportunity&Service_Country__c=User.Country if I understand what you wrote. But I don’t know where to put this. Since I am overiding the standard Opportunity New button, I will not creat a custom button…
The VF page:
apex:page standardController=”Opportunity” extensions=”RedirectWithVariablesController” action=”{!redirectUser}”>
</apex:page
I am having problems getting my VF page code to appear here…
Hi Alain,
Did you get it working? Please check my new blog: http://www.frankneezen.com/2013/04/salesforce-url-hacking-prepopulating-fields/
Drop me a note there if you still have questions. Thanks!
This is my VF page
This does not do anything. After Record Type choice, I get a blank screen.
This VF page is to overide the “New” button in Opportunities.
My objective is to create the opportuntiy with a custom picklist field named Service Country to be filled in by the contents of the User’s country.
So it would give something like /apex/RedirectWithVariables?object=Opportunity&Service_Country__c=User.Country if I understand what you wrote. But I don’t know where to put this. Since I am overiding the standard Opportunity New button, I will not creat a custom button…
Hi, I tried to use the classes and here is my testing URL that is not working properly:
https://c.cs9.visual.force.com/apex/RedirectWithVariables?object=Case
&Contract__c=00000440
&ID_Contract__c=800K000000019chIAA
&RecordType=Contract
&Contract_End_Date__c=2014-06-17
&Contract_Start_Date__c=2013-06-17
&Opportunity_Type__c=Sysomos
&Opportunity_Name__c=Smalls+Jr.-Jun%2F2013%2FNick
&ID_Opportunity_Name__c=006K0000005vVFqIAM
&Contact=Samy+Saied
&ID_Contact=003K000000YZAQnIAP
&Contract__c=00000440
&ID_Contract__c=800K000000019chIAA
&Account=Smalls+Jr.
&ID_Account=001K000000ZHVssIAH
&Primary_Quote__c=Q%2FSmalls+Jr.
&ID_Primary_Quote__c=a0bK0000000BlMsIAK
For some reason none of those are working, does it have to be a custom object only ?
Hi Samy,
Please check: http://www.frankneezen.com/2013/04/salesforce-url-hacking-prepopulating-fields/
If it still not working drop me a note on my new blog. Thanks!
I think this is a great solution, and I’d really like to try it out. I’ve created both classes and the apex page. I updated my URL, but when I click on it I get the following message:
Attempt to de-reference a null object
Error is in expression ‘{!redirectUser}’ in component in page redirectwithvariables
Any ideas? I know it is probably something simple that I just can’t wrap my head around because it’s the end of the day ha ha
I figured it out! Yes, it was something simple. I was populating a lookup field and had copied and pasted your URL exactly without realizing that on the ID you have a __v on the end instead of a __c. All is good and this is working perfectly! Such a great solution, thank you so much for sharing. You have no idea how much this is going to help us!!
Hi Emily,
Great I could help! Please check my new blog as well as I will not be commenting on this one (keeping things central). -> http://www.frankneezen.com/2013/04/salesforce-url-hacking-prepopulating-fields/
Hi,
Thank you for this valuable information .
I have a record (Opp)from which the picklist value and the dependent values of the pick list should be pre-populated on clicking a Button to the next Record(QR). I have executed java script for the remaining fields of Opp record.
Is is any way so that I can pre-populate the Picklist values to the next record using the same java script.
Thanks in Advance,
Anil .
Hi Anil -> Please see the post on my new blog http://www.frankneezen.com/2013/04/salesforce-url-hacking-prepopulating-fields/
I will answer any questions there 🙂 Thanks
Pingback: Set default value on children records when `New` is clicked on related list | DL-UAT
So, the link to the new blog doesn’t work for me at this time, so I’m adding this here.
Salesforce Summer ’15 has broken this functionality for required fields. I have a fix. The problem is that the script in StaticFunctions is scraping the standard edit page for the target object and Salesforce has changed the CSS class name on that generated page from “requiredMark” to “assistiveText”. I fixed this by making the scraper just ignore any HTML tags inside the LABEL tag rather than looking for a specific span tag and class.
I had to change line 44 from:
Matcher m = Pattern.compile(‘(\\*)?(.*?)’).matcher(html);
to
Matcher m = Pattern.compile(‘(.*?)’).matcher(html);
and then change line 50 from:
String label = m.group(3);
to
String label = m.group(2).replaceAll(‘]+)[^>]*>.*?’, ”); // Strip all HTML tags from label content
is there a test class available?
You new blog is redirecting to unsafe sites.
Hi,
May I ask for assistance? We have a custom button on case object and we hope to create a hyperlink (formula field) on the account object so when a user clicks on that field, it will populate the same as when clicking the button. Thanks for your help. This is the button :
{!REQUIRESCRIPT(“/soap/ajax/13.0/connection.js”)}
var caseObj = new sforce.SObject(“Case”);
caseObj.AccountId = ‘{!Account.Id}’;
caseObj.RecordTypeId = ‘0123E0000008WLT’;
caseObj.Type = ‘Complaint’;
caseObj.Is_Complaint_Checkbox__c = true;
caseObj.Priority = ‘Is Complaint’;
caseObj.Subject = ”;
caseObj.Description = ”;
var result = sforce.connection.create([caseObj]);
if(result[0].getBoolean(“success”)){
window.location = “/” + result[0].id ;
}else{
alert(‘Could not create record ‘+result);
}