Monday, June 27, 2011

CRM 2011 - Get and Set Lookup Values Using Java Script

SET VALUES>>
function SetLookupValue(fieldName, id, name, entityType) {
if(fieldName != null) {
var lookupValue = new Array();
lookupValue[0] = new Object();
lookupValue[0].id = id;
lookupValue[0].name = name;
lookupValue[0].entityType = entityType;

Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
}
}
// USE
SetLookupValue("new_blogid", "{FD140AAF-4DF4-11DD-BD17-0019B9312238}", "CRM Stuff", "new_blog");


================================================
GET VALUES>>
var lookUpObjectValue = Xrm.Page.getAttribute("yourlookupattributename");


    if (lookUpObjectValue != null)
  {
         var lookuptextvalue = lookUpObjectValue[0].name;


         var lookupid = lookUpObjectValue[0].id;
}

1 comment:

  1. var lookUpObjectValue = Xrm.Page.getAttribute("yourlookupattributename");

    tihis a wron code it takes my half hour

    right code ise
    lookupItem = Xrm.Page.getAttribute("yourAttributeSchemaName").getValue();

    ReplyDelete