In a previous blog post I went over how to populate fields in CRM. You may notice in that blog post I did not cover how to populate lookup fields, and this simply was because at the time I didn’t know an effective way to do this. After this problem coped up a few more times I decided it was about time to find a usable solution, so I took to the internet in an attempt to learn some tricks.
Many of you, like me, may have wondered why simply setting the lookup field to a value and moving off the field with JavaScript doesn’t force the field to lookup the value as it would if you were manually entering data. Now apparently this feature can be accessed simply by adding “_ledit” to the end of the attribute name.
Knowing this, populating lookup fields becomes almost as easy as populating regular fields, we just have to bear in mind that data value should, when possible, exactly match the record name. A limitation of this method is that if the record you are trying to look up is “Test” but there is a record named “Test 2” or “Tester” it will not work correctly. This is because when the lookup field searches for “Test” it finds more than 1 result, and therefore is uncertain of which record you want.
This method is however excellent for populating a field such as Price List, which is often the same, and typically won’t have any repeats in names.
Following is the code you require to populate a lookup field;
If the lookup field is on the default tab;
// Check to make sure there isn’t already a value in the lookup field.
if (crmForm.all.attribute.DataValue == null) {
// Change focus to the ledit section of the lookup field.
crmForm.all.attribute_ledit.focus();
// Input the text value of the name of the record you wish to populate in the lookup field.
crmForm.all.attribute_ledit.value = "Your Text Here";
}
Rename attribute to the name of the lookup field you are populating and change Your Text Here to the name of the record you wish to look up.
Now unfortunately the focus() command doesn’t work unless the correct tab is open. This means that if the lookup field you wish to populate is not on the main tab you require a few more lines of code for the population to work correctly.
If the lookup field is NOT on the default tab;
// Make sure there is no data already entered in the lookup field you wish to populate.
if (crmForm.all.attribute.DataValue == null) {
// Navigate to the appropriate tab.
crmForm.all.tab1Tab.click();
// Change focus to the ledit section of the lookup field.
crmForm.all.attribute_ledit.focus();
// Input the text value of the name of the record you wish to populate in the lookup field.
crmForm.all.attribute_ledit.value = "Your Text Here";
// Wait 1 millisecond then change to back to the primary tab. This Timeout is required for this action to work correctly.
setTimeout("crmForm.all.tab0Tab.click()",1);
}
Rename attribute to the name of the lookup field you are populating, 1 to the tab number the attribute is located on and Your Text Here to the name of the record you wish to look up.
NOTE: 0 is the first tab (default tab), 1 is the second tab, 2 is the third tab etc.
Using the Timeout is a workaround as I couldn’t get the JavaScript to fire the last command to bring the focus back to the default tab without it. If anyone has a solution for this I would love to hear it!