This documentation is archived and is not being maintained.

How to Work with Lookup Form Controls

Forefront
banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

You can read values from, and set new values to, most of the Microsoft Dynamics CRM form controls, including lookups.

This sample is in the folder SDK\Client\HowTo\UsingLookups.

Example

This sample code shows how to read values from a primarycontactid lookup control on an account form.

var lookupItem = new Array;

// Get the lookup for the primarycontactid attribute on the account form.
lookupItem = crmForm.all.primarycontactid.DataValue;

// If there is data in the field, show it in a series of alerts.
if (lookupItem[0] != null)
{
    // Display the text value of the lookup.
    alert(lookupItem[0].name);

    // Display the entity type name.
    alert(lookupItem[0].typename);

    // Display the GUID of the lookup.
    alert(lookupItem[0].id);

    // Display the entity type code of the lookup.
    // A value of 1 = account, and a value of 2 = contact.
    alert(lookupItem[0].type);
}

Example

This example shows how to set the parent account lookup field on the account form.

// Create an array to set as the DataValue for the lookup control.
var lookupData = new Array();
// Create an object to add to the array.
   var lookupItem= new Object();
// Set the id, typename, and name properties to the object.
   lookupItem.id = '{1AAC1363-01A1-DB11-8432-0003FF9CE217}';
   lookupItem.typename = 'account';
   lookupItem.name = 'A Bike Store';
// Add the object to the array.
   lookupData[0] = lookupItem;
// Set the value of the lookup field to the value of the array.
   crmForm.all.parentaccountid.DataValue = lookupData;


© 2010 Microsoft Corporation. All rights reserved.


Show: