Everyone has seen the method of updating a CRM lookup field on a form, most examples look something like this.
// CRM 4 var value = new Array(); value[0] = new Object(); value[0].id = idValue; value[0].name = textValue; value[0].typename = typeValue; crmForm.all.fieldName.DataValue = value; // CRM 2011 var value = new Array(); value[0] = new Object(); value[0].id = idValue; value[0].name = textValue; value[0].entityType = typeValue; Xrm.Page.getAttribute("fieldName").setValue(value);
How about doing it on one line like this instead.
// CRM 4 crmForm.all.field.DataValue = [{id: idValue, name: textValue, typename: typeValue}]; // CRM 2011 Xrm.Page.getAttribute("fieldName").setValue( [{id: idValue, name: textValue, entityType: typeValue}]);
There is a short but interesting article about JavaScript called Seven JavaScript Things I Wish I Knew Much Earlier In My Career by Christian Heilmann if you have some spare time to read.
Hi Rhett,
Actually I had the exact same thought the other day…
Cheers,
Daniel
Not a bad article.
There is something to think about.
–
Sincerely yours, administrator of the site Tips and Tricks
Pingback: CRM 2011 – How to set up a lookup using Javascript « Hosk's Dynamic CRM 2011 Blog
I apologize for this code but, I am stuck with the new format after having used the conversion tool and the information on this and other sites. I previously could grab the lookup value of another entity from a parent entity. The code is trying to get the parentcustomerid from the contact table to fill in the Householdunitid field on a new entity.
///Get lookup ID
{
var lookupfield = Xrm.Page.getAttribute("new_ownerid").getValue();
if (lookupfield != null && lookupfield [0] != null)
{
var householdlookupvalue = lookupfield [0].id;
}
else
{
var householdlookupvalue = " ";
}
}
// Prepare variables for a contact to retrieve.
var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();
// Prepare the SOAP message.
var xml = ""+
""+
authenticationHeader+
""+
""+
"contact"+
""+lookupfield [0].id+""+
""+
""+
"parentcustomerid"+
""+
""+
""+
""+
"";
// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);
// Capture the result.
var resultXml = xHReq.responseXML;
// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0)
{
}
// Display the retrieved value.
else
{
//Create an array to set as the DataValue for the lookup control.
var lookupData = new Array();
//Create an Object add to the array.
var lookupItem= new Object();
//Set the id, typename, and name properties to the object.
lookupItem.id = resultXml.selectSingleNode("//q1:parentcustomerid").nodeTypedValue;
lookupItem.entityType = 'account';
lookupItem.name = resultXml.selectSingleNode("//q1:parentcustomerid").getAttribute("name");
// Add the object to the array.
lookupData[0] = lookupItem;
// Set the value of the lookup field to the value of the array.
Xrm.Page.getAttribute("new_householdunitid").setValue(lookupData);
}
Excellent stuff, small point on CRM 2011. To set the values in script on one line
Xrm.Page.getAttribute(“fieldName”).setValue( [{id: idValue, name: textValue, entityType: typeValue}]); didn’t work for me I had to use
Xrm.Page.getAttribute(“fieldName”).setValue( [{id: “idValue”, name: “textValue”, entityType: “typeValue”}]);
Only worked with quotes for me, and works perfectly.
Ok yes, I didn’t make it clear. You need ” quotes if you are passing in text values but you can also use variables in which case you do not need them.
Thanks,
Rhett
I use the code supplied like this:
Xrm.Page.getAttribute(“responsiblecontactid”).setValue([{id: results[0].attributes[“primarycontactid”].guid, name: results[0].attributes[“primarycontactid”].name, entityType: results[0].attributes[“primarycontactid”].type}]);
when this is parsed on the page it display the name of the lookup value but without showing the related entity icon and also it dosent let me save the form.
any clue on this particular issue
I found that for ti to work is a must to change the entityType: “typeValue” for enityName. Cheers hope it helps some one
Thanks for sharing Garcia.
Cheers,
Rhett
Had the exact same problem Thanks!!!
Pingback: CRM 2011 – How to set up a lookup using Javascript ! « Srikanth Reddy's Blog
Pingback: How to set up a lookup using Javascript | Web Fada Blog