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".
I hope this helps!
-
Slight correction:
ReplyDeleteIt should be " if (lookupItem !== null) "
!= does a different check than !==, and if you try lookupItem[0] it attempts to resolve the array before verifying null, thus producing an error.
Or I could just check for lookupitem != null instead of lookupitem[0] != null.
DeleteYou make a good point. I will fix it.
Hello,
ReplyDeleteIf i also want to fetch other attributes (fields) of an entity of lookup. How can i do that ?
Once you have the ID you can use a retrieve request
Deletehttp://mileyja.blogspot.com/2011/04/how-to-use-retrieve-messages-in-jscript.html
Cheers!
The question not answered here is how do you retrieve the ID in idValue? value[0].id = idValue;
ReplyDeleteDoes this not work?
Deletevar guid = lookupItem[0].id;
The code you referenced was setting a value, not retrieving.
DeleteThis comment has been removed by the author.
ReplyDeleteI appreciate the blogging you do. Maybe you can point me down the right path on this one.
ReplyDeleteI have two fields on my Case form: One is a lookup to Teams, the other is a lookup to users. On the Team form, I have a field called 'pw_primaryactionee'
(also a lookup) that may or may not be populated with a User value.
The idea is that when I select a TEAM on the case form, the 'Actionee' value (also on the case form) populated if the 'Primary Actionee' field on the team form is populated.
That all works. The issue that I'm having is that AFTER I SAVE THE CASE, the value that *is* populated into my "pw_actionee" field on the case form is gone. (note: the function 'teamChange' is called on the onChange event of the team lookup field on the case form. 'setFieldReq' is just a function I'm calling from a home-grown library to set a field as Business Required)
Code snips below. Any ideas?
function teamChange() {
context = Xrm.Page.context;
serverUrl = context.getServerUrl();
var theTeam = Xrm.Page.getAttribute("pw_cc").getValue();
retrieveRecord(theTeam[0].id, "TeamSet", retrieveSuccessfulTeam, null);
}
function retrieveSuccessfulTeam(Data, textStatus, XmlHttpRequest) {
if (Data != null) {
var arrACT = new Array();
arrACT[0] = new Object();
arrACT[0].id = Data.pw_PrimaryActionee.id
arrACT[0].entityType = Data.pw_PrimaryActionee.LogicalName;
arrACT[0].name = Data.pw_PrimaryActionee.Name;
Xrm.Page.getAttribute("pw_actionee").setValue(arrACT);
setFieldReq("pw_actionee");
}
else {
var ctlACTIONEE = Xrm.Page.getControl("pw_actionee");
var atrACTIONEE = ctlACTIONEE.getAttribute();
atrACTIONEE.setValue(null);
setFieldReq("pw_actionee");
}
}
Never mind. I'm just looking at the forest and can't see the trees again...
ReplyDelete"arrACT[0].id = Data.pw_PrimaryActionee.id" should have been
"arrACT[0].id = Data.pw_PrimaryActionee.Id"
stupid case-sensitive silly languages...
Thanks for your patience as I bash my head into the walls to lean my lessons...
Hi,
ReplyDeleteI am trying to retrieve Owner's Team in MSD CRM 2011 and set it to lookup Owner's Team attribute.
Can you guide me with the script for the same.
Task goes like dis ...
ReplyDelete1)Take contact entity then in contact form create a lookup field with name contact type lo students,employee's etc ..
2)Now again open another entity like school in school form create field contact..lookup
3)When you select contact.. dani type untadi kada.. adi automatic ga select ipovali USING JAVA SCRIPT
Can any one sol d issue for me
Delete