Pre-popoulate Salesforce Lookup Fields Using URL Parameters
A few years ago I wrote a post about how to populate information into Salesforce fields by navigating the User from page to page. I received a lot of good feedback on that post and the logic for it pretty much remains unchanged today. The intent of that original post was to identify how an administrator could use button overrides to get a User from one page to another and still auto-fill some of the details for the next page on behalf of the User. Go here to see my original post.
This post ties into that one in that I want to provide some additional details for populating Salesforce lookup fields. This is most frequently the next question I receive from people that read the first post regarding this topic.
The first hurdle to get over regarding this topic is that the logic for populating lookup fields on standard objects can be slightly different than when populating values on custom objects. Therefore, I am going to outline how to pass a value into a lookup field on a custom object. This is because the overall concept is the same but the steps outlined below will work for all custom objects and not necessarily standard objects. I guess I mean to indicate that this information can be applied to a wider array of business needs and that's why I'm writing about it.
So the main concept that applies to lookup fields on custom objects is that you need to pass both the Id of the record you wish to link via the lookup and also the Name of the record you wish to link. The easiest way to determine the fields to use for this example would be to view the source code of the page where you will ultimately pass the values.
The parameter to use for the Id of the record you want to link will look something like this; CFXXXXXXXXXXXXXXX_lkid. This parameter is always prefixed with the characters "CF" and it always ends with the characters "_lkid". The characters in between those are the fifteen (15) character Id for the field definition, which you can grab from the page where the field was actually defined in the Salesforce setup. The parameter to use for the Name is setup in a simliar way; CFXXXXXXXXXXXXXXX.
A real world example from one of my orgs is where I needed to pre-populate a Contact relationship on a new record based on the Contact that was already associated to another object. Using a button override I could add the string "/a08/e?CF00NA0000002h0MI={!Contact.Name}&CF00NA0000002h0MI_lkid={!Contact.Id}"
Technically, if you were to pass only the Id of the record to the page the association would still be maintained when the data is saved by the user. However, passing the Name will allow the user to see the relationship being made and eliminate any confusion about exactly what is happening as they navigate.
With that said, I want to make a note that this sort of URL "hacking" is not really supported by salesforce.com and that they would prefer you refrain from doing things as I describe on this page. However, I've been working with the Salesforce platform since 2003 and this logic has not changed once over the years. Of course, it will probably change in the next release and I'll get more emails about how this tidbit of information is useless. Until then please feel free to try this out and let me know if you have any questions.