This is one of those posts that comes from the fact that there are a lot of questions in the forums that relate to this topic and there are a lot of good posts that demonstrate either how to set a lookup value or get a lookup value, but there aren't many that show both. So that is what we will do today.
Here is a quick demo on how to either get or set a lookup value in Jscript in Microsoft Dynamics CRM 2011:
//Get a lookup value
var lookupItem = new Array();
lookupItem = Xrm.Page.getAttribute("yourAttributeSchemaName").getValue();
if (lookupItem != null)
{
var name = lookupItem[0].name;
var guid = lookupItem[0].id;
var entType = lookupItem[0].entityType;
}
//Set a lookup value
var value = new Array();
value[0] = new Object();
value[0].id = idValue;
value[0].name = textValue;
value[0].entityType = typeValue;
Xrm.Page.getAttribute("yourAttributeSchemaName").setValue(value);
//or alternatively you can set it like this
Xrm.Page.getAttribute("yourAttributeSchemaName").setValue( [{id: idValue, name: textValue, entityType: typeValue}]);
Note: You must wrap your code in a function in CRM 2011 to be called by an event handler. This is achieved by using the syntax below:
function test()
{
//my jscript code here!
}
Now you can just call your function name from an event handler by specifying the web resource and the function name "test".
No comments:
Post a Comment