Lookup Info
Looking up information in a Lookup field is harder than what it needs to be. I use two different methods. Each has had their purpose in my efforts of CRM. Ideally the first method is the shortest but on some forms has also given me the hardest time. The second is longer but seems to work on every form every time so is the one I opt to use even though there's more code involved.
Method 1
crmForm.all.XXXXXXXXXXXX.DataValue[0].name // Related XXXXXXXXXXXX with the actual lookup field
Method 2
// Setup a new lookup array
var lookupItem = new Array;
// This will get the lookup for the attribute primarycontactid on the Account form.
lookupItem = crmForm.all.XXXXXXXXXXXX .DataValue; // Related XXXXXXXXXXXX with the actual lookup field
// If there is data in the field, show it in a series of alerts.
if (lookupItem && lookupItem[0] != null) {
alert("Not NULL!");
} else {
alert("NULL!");
}
Get All of the Detailed Information from a Lookup control
// Setup a new lookup arrayvar lookupItem = new Array;
lookupItem = crmForm.all.XXXXXXXXXXXX .DataValue; // Related XXXXXXXXXXXX with the actual lookup field
// If the lookupItem isn't null (blank) then lets show alert boxes of what each one contains for testing purposes
if (lookupItem && lookupItem[0] != null) {
alert(lookupItem[0].name); // Text value of the lookup item
alert(lookupItem[0].typename); // Entity type name
alert(lookupItem[0].id); // Guid of the lookup entity
alert(lookupItem[0].type); // Entity type code (1=account, 2=contact, ...)
}
Setting Values of a Lookup Entity
// Setup a new lookup array
var lookupItem = new Array();
// Set the values on the lookupItem
// Format being: GUID, TypeCode, TextValue
lookupItem[0] = new LookupControlItem("{00000000-0000-0000-0000-000000000000}", 1234, "Text Value");
// Set the form to the value of the lookupItem
crmForm.all.XXXXXXXXXXX.DataValue = lookupItem;