Tuesday, December 1, 2009

Default value for lookups

Hi, the following with some care will help to populate the default value for lookup fields( as we can do for picklist and text boxes).

for this the record should present earlier.
and we need to get the following 3 values.
GUID,ObjectTypeCode for entity,displayValue.

onchange of the lookup attribute add the following code and capture the 3 values as follows.

var lookupItem = new Array;
lookupItem = crmForm.all..DataValue;
// If there is data in the field, show it in a series of alerts.
if (lookupItem[0] != null){
// The text value of the lookup
alert("Display Value"+lookupItem[0].name);
// The GUID of the lookup
alert("GUID"+lookupItem[0].id);
// The entity type name. by using the entityname get the ObjectTypeCode from
//the MetadataSchema.Entity table
alert("Entity Name"+lookupItem[0].typename);
}

by using the entity name get the "ObjectTypeCode" from the "MetadataSchema.Entity" table from DB
now its ready to call the function.
call it onLoad of the form and it need to be done on creation of new record.

//the following will take 4 parameters
//1st GUID of the record to be set as default
//
function setDefault4Lookup(guid,entityName,displayValue,attributeID)
{
var lookupItem = new Array();
// Values on the signature of LookupControlItem are: GUID, type code and finally the name of the
//lookup value
lookupItem[0] = new LookupControlItem (guid, entityName, displayValue);
//Set the form control value to the lookupItem just created
attributeID.DataValue = lookupItem;
}

No comments:

Post a Comment