Wednesday, July 1, 2009

CRM 4.0 Lookup: Retrieve Column values from Lookup window

There are times when you need to retrieve some values from the parent record, on the selection of the lookup value. In such a scenario, you can retrieve all column values of the lookup window using the following script:

Example: Order Form - Customer Field OnChange event
if(crmForm.all.customerid.DataValue != null)
{
var parentCustomerID = document.getElementById( "customerid" );
var lookupItems = parentCustomerID.items[0].values;
alert(lookupItems[0].value); // First column value of Lookup
alert(lookupItems[1].value); // Second column value of Lookup
alert(lookupItems[2].value); // Third column value of Lookup
alert(lookupItems[3].value); // Fourth column value of Lookup
alert(lookupItems[4].value); // Fifht column value of Lookup
alert(lookupItems[5].value); // Sixth column value of Lookup
}

This will display the Contact's 'Full Name', 'Parent Customer Name', 'Address 1: City', 'Address 1: Phone', 'Business Phone' and 'Email' values from the selected Contact (Default Contact Lookup View). If you select an Account, this will display the 'Account Name', 'Account Number', 'Primary Contact', 'Address 1: City', 'Main Phone' and 'Email' values from the selected Account.





The lookupItems array will hold all the values in the same sequence as defined in the Lookup View.

You can now add your required values in any lookup view and retrieve the same without making service calls.

Note: You should disable the Automatic Resolution, so that users cannot add values automatically. This script will work only if the Lookup View is loaded.

Feel free to reach out in case of any issues/queries in this regard.
Cheers!!!

8 comments:

  1. I have created a new fields named Last Appointment Date and Next Appointment Date in contact entity. I need to auto populate these fields from Appointment entity. How can i do that. Please help me??

    ReplyDelete
  2. Hi,
    after retrieving the column from the lookup field this column is clickable in the lookup view because it is a relationship,
    how can i set it to another look up field in the same form?

    ReplyDelete
  3. Hi again,

    is there a way to get the Guid of the parentcustomer and not just its name?

    thank you.

    ReplyDelete
  4. How would I get the lookup of other fields such as the Account address state and such? I get errors starting at alert(lookupItems[10].value);

    ReplyDelete
  5. Managed to solve this myself. All I had to do was add the desired fields to the Account Lookup View columns in the form customizations. Once I added them in there, I just did an alert on each array object and found the correct mappings. Here was my end result code:

    if(crmForm.all.parentcustomerid.DataValue != null && crmForm.all.address1_name.Value == null)

    {
    var parentCustomerID = document.getElementById( "parentcustomerid" );
    var lookupItems = parentCustomerID.items[0].values;
    crmForm.all.address1_name.value = lookupItems[5].value;
    crmForm.all.address1_telephone1.value = lookupItems[13].value;
    crmForm.all.address1_city.value = lookupItems[9].value;
    crmForm.all.address1_line1.value = lookupItems[6].value;
    crmForm.all.address1_line2.value = lookupItems[7].value;
    crmForm.all.address1_line3.value = lookupItems[8].value;
    crmForm.all.address1_stateorprovince.value = lookupItems[11].value;
    crmForm.all.address1_postalcode.value = lookupItems[10].value;
    crmForm.all.address1_country.value = lookupItems[12].value;
    }

    Hope this helps someone else :)

    ReplyDelete
  6. I've been looking for the proper way to do this for days - THANK YOU!

    ReplyDelete
  7. It is not working when i use form assistant for data entry.
    Is there any way to make it work?

    ReplyDelete
  8. In your script above, I only need one column to display, but I want it to display in company field instead of showing a alert box.

    Is this possible

    ReplyDelete