Answered by:
Autopopulate lookup field on selection of another lookup mscrm 2011

Question
-
Hi all,
I have a requirement where i need to autopopulate lookup field on selection of another lookup.
Thanks in Advance
Monday, October 29, 2012 5:02 AM
Answers
-
Hi all,
Below is the code to autopopulate lookup field based on another lookup using Javascript
function GetROBRR()
{
var EntityName, EntityId, subjectLookupFieldObject, ProjectName;
var subjectLookupId, subjectLookupName, subjectLookupType;
var resultXml;//Lookup selection to autopopulate LookupFieldObject = Xrm.Page.data.entity.attributes.get('new_resourcename');
if (LookupFieldObject.getValue() != null) {
EntityId = LookupFieldObject.getValue()[0].id;
EntityName = LookupFieldObject.getValue()[0].entityType;
resultXml = RetrieveEntityById(EntityName, EntityId, 'rob_designationlocus,new_reportingmanagerrob,rob_dateofjoining,rob_employeeid');
if (resultXml != null && resultXml.selectSingleNode('//q1:rob_designationlocus') != null) {
subjectLookupFieldObject = new Array();
subjectLookupFieldObject[0] = new Object();
subjectLookupId = resultXml.selectSingleNode('//q1:rob_designationlocus').nodeTypedValue;
subjectLookupName = resultXml.selectSingleNode('//q1:rob_designationlocus').getAttribute("name");
subjectLookupType = resultXml.selectSingleNode('//q1:rob_designationlocus').getAttribute("type");
subjectLookupFieldObject[0].id = subjectLookupId;
subjectLookupFieldObject[0].name = subjectLookupName;
subjectLookupFieldObject[0].entityType = subjectLookupType;
Xrm.Page.getAttribute('new_rrdesignation').setValue(subjectLookupFieldObject);
}
}
}
// Do not make any changes to this function
function RetrieveEntityById(prmEntityName, prmEntityId, prmEntityColumns)
{
var resultXml, errorCount, msg, xmlHttpRequest, arrayEntityColumns, xmlEntityColumns;
arrayEntityColumns = prmEntityColumns.split(",");
for (var i = 0; i < arrayEntityColumns.length; i++)
{
xmlEntityColumns += "<q1:Attribute>" + arrayEntityColumns[i] + "</q1:Attribute>";
}
var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();
//Prepare the SOAP message.
var xml = "<?xml version='1.0' encoding='utf-8'?>" +
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" +
" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
authenticationHeader +
"<soap:Body>" +
"<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<entityName>" + prmEntityName + "</entityName>" +
"<id>" + prmEntityId + "</id>" +
"<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
"<q1:Attributes>" +
xmlEntityColumns +
"</q1:Attributes>" +
"</columnSet>" +
"</Retrieve></soap:Body></soap:Envelope>";
//call function to create Soap Request to ms crm webservice
xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
resultXml = xmlHttpRequest.responseXML;
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0)
{
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert("Error Message : " + msg);
}
else {
return resultXml;
}
}- Marked as answer by Nandan21 Thursday, November 22, 2012 4:36 AM
Thursday, November 22, 2012 4:36 AM
All replies
-
Hi,
Here is the code sample
if (Xrm.Page.getAttribute("source_lookupfieldname").getValue() != null) {
var lookup = new Object();
var lookupValue = new Array();
lookup.id = Xrm.Page.getAttribute("source_lookupfieldname").getValue()[0].id;
lookup.entityType = Xrm.Page.getAttribute("source_lookupfieldname").getValue()[0].entityType;
lookup.name = Xrm.Page.getAttribute("source_lookupfieldname").getValue()[0].name;
lookupValue[0] = lookup;
Xrm.Page.getAttribute("target_lookupfieldname").setValue(lookupValue);
}- Proposed as answer by HIMBAPModerator Monday, October 29, 2012 6:01 AM
Monday, October 29, 2012 5:28 AM -
Hi,
If your another lookup values are depend on first lookup. then there is simple way to add field in another lookup.
go to customisation select ur form and check only show records. otherwise use below code
if (Xrm.Page.getAttribute("source_lookupfieldname").getValue() != null) {
var lookup = new Object();
var lookupValue = new Array();
lookup.id = Xrm.Page.getAttribute("source_lookupfieldname").getValue()[0].id;
lookup.entityType = Xrm.Page.getAttribute("source_lookupfieldname").getValue()[0].entityType;
lookup.name = Xrm.Page.getAttribute("source_lookupfieldname").getValue()[0].name;
lookupValue[0] = lookup;
Xrm.Page.getAttribute("target_lookupfieldname").setValue(lookupValue);
}Monday, October 29, 2012 5:48 AM -
Hi Vikram,
Scenario:
I have a custom Entity"XYZ" which is related to Account entity , i have a lookup in "XYZ" when i select any account from that lookup i want the fields to be autopopulated here which includes lookup , text, optioset also
Thanks fr ur reply
Monday, October 29, 2012 7:15 AM -
Hello,
You can refer following link and change the code according to your requirement.
http://vikrammscrm.blogspot.in/2012/04/auto-populate-form-fields-with-selected.html
Monday, October 29, 2012 7:47 AM -
Hi Nandan,
Please try the following code.
It assumes that your custom entity ("XYZ") entity has a lookup field for a Contact, email address field (plain text) and a picklist field ("new_relationshiptype").
Account entity also has these fields and XYZ has a relationship with Account entity.
In the XYZ form when you select an Account from the Account Lookup it will fetch the data for the above three fields from the account record and set them to the relevent fields in the XYZ form. I have used "if (crmForm.FormType == 1)" at the beginning so that this happens only when you create a new record.
Call this function at the onChange event of the AccountLookup field in XYZ form.
function GetAccountDetails() {
//debugger;
if (crmForm.FormType == 1) {
var AccountLookup = Xrm.Page.getAttribute("new_accountid").getValue();
if (AccountLookup != null) {
var AccountId = AccountLookup[0].id;
var serverUrl = "/mscrmservices/2007/crmservice.asmx";
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("POST", serverUrl, false);
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
var message =
[
"<?xml version='1.0' encoding='utf-8'?>",
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>",
GenerateAuthenticationHeader(),
"<soap:Body>",
" <Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>",
" <entityName>account</entityName>",
" <id>", AccountId, "</id>",
" <columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>",
" <q1:Attributes>",
" <q1:Attribute>primarycontactid</q1:Attribute>",
" <q1:Attribute>new_relationshiptype</q1:Attribute>",
" <q1:Attribute>emailaddress1</q1:Attribute>",
" </q1:Attributes>",
" </columnSet>",
" </Retrieve>",
"</soap:Body>",
"</soap:Envelope>"
].join("");
xmlhttp.send(message);
var resultXml = xmlhttp.responseXML;
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0) {
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
}
else {
if (resultXml.selectSingleNode('//q1:primarycontactid') != null) {
var contactid = resultXml.selectSingleNode("//q1:primarycontactid").text;
var contactName = resultXml.selectSingleNode("//q1:primarycontactid").getAttribute("name");
Xrm.Page.getAttribute("new_contactid").setValue([{ id: contactid,
name: contactName,
typename: "contact"
}]);
}
if (resultXml.selectSingleNode('//q1:emailaddress1') != null) {
var email = resultXml.selectSingleNode("//q1:emailaddress1").text;
Xrm.Page.getAttribute("new_email").setValue(email);
}
if (resultXml.selectSingleNode('//q1:new_relationshiptype') != null) {
var relationshipTypeid = resultXml.selectSingleNode("//q1:new_relationshiptype").text;
var relationshipTypeText = resultXml.selectSingleNode("//q1:new_relationshiptype").getAttribute("name");
Xrm.Page.getAttribute("new_relationshiptype").setValue(relationshipTypeid);
}
}
}
}
}I hope this is what you are looking for.
Regards
Sachith
Sachith Chandrasiri
- Edited by Sachith Vidanage Monday, October 29, 2012 4:56 PM
- Proposed as answer by Sachith Vidanage Monday, October 29, 2012 4:57 PM
Monday, October 29, 2012 4:54 PM -
Hi all,
Below is the code to autopopulate lookup field based on another lookup using Javascript
function GetROBRR()
{
var EntityName, EntityId, subjectLookupFieldObject, ProjectName;
var subjectLookupId, subjectLookupName, subjectLookupType;
var resultXml;//Lookup selection to autopopulate LookupFieldObject = Xrm.Page.data.entity.attributes.get('new_resourcename');
if (LookupFieldObject.getValue() != null) {
EntityId = LookupFieldObject.getValue()[0].id;
EntityName = LookupFieldObject.getValue()[0].entityType;
resultXml = RetrieveEntityById(EntityName, EntityId, 'rob_designationlocus,new_reportingmanagerrob,rob_dateofjoining,rob_employeeid');
if (resultXml != null && resultXml.selectSingleNode('//q1:rob_designationlocus') != null) {
subjectLookupFieldObject = new Array();
subjectLookupFieldObject[0] = new Object();
subjectLookupId = resultXml.selectSingleNode('//q1:rob_designationlocus').nodeTypedValue;
subjectLookupName = resultXml.selectSingleNode('//q1:rob_designationlocus').getAttribute("name");
subjectLookupType = resultXml.selectSingleNode('//q1:rob_designationlocus').getAttribute("type");
subjectLookupFieldObject[0].id = subjectLookupId;
subjectLookupFieldObject[0].name = subjectLookupName;
subjectLookupFieldObject[0].entityType = subjectLookupType;
Xrm.Page.getAttribute('new_rrdesignation').setValue(subjectLookupFieldObject);
}
}
}
// Do not make any changes to this function
function RetrieveEntityById(prmEntityName, prmEntityId, prmEntityColumns)
{
var resultXml, errorCount, msg, xmlHttpRequest, arrayEntityColumns, xmlEntityColumns;
arrayEntityColumns = prmEntityColumns.split(",");
for (var i = 0; i < arrayEntityColumns.length; i++)
{
xmlEntityColumns += "<q1:Attribute>" + arrayEntityColumns[i] + "</q1:Attribute>";
}
var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();
//Prepare the SOAP message.
var xml = "<?xml version='1.0' encoding='utf-8'?>" +
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" +
" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
authenticationHeader +
"<soap:Body>" +
"<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<entityName>" + prmEntityName + "</entityName>" +
"<id>" + prmEntityId + "</id>" +
"<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
"<q1:Attributes>" +
xmlEntityColumns +
"</q1:Attributes>" +
"</columnSet>" +
"</Retrieve></soap:Body></soap:Envelope>";
//call function to create Soap Request to ms crm webservice
xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
resultXml = xmlHttpRequest.responseXML;
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0)
{
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert("Error Message : " + msg);
}
else {
return resultXml;
}
}- Marked as answer by Nandan21 Thursday, November 22, 2012 4:36 AM
Thursday, November 22, 2012 4:36 AM -
Just Highlighting the syntax..
function GetROBRR() { var EntityName, EntityId, subjectLookupFieldObject, ProjectName; var subjectLookupId, subjectLookupName, subjectLookupType; var resultXml; //Lookup selection to autopopulate LookupFieldObject = Xrm.Page.data.entity.attributes.get('new_resourcename'); if (LookupFieldObject.getValue() != null) { EntityId = LookupFieldObject.getValue()[0].id; EntityName = LookupFieldObject.getValue()[0].entityType; resultXml = RetrieveEntityById(EntityName, EntityId, 'rob_designationlocus,new_reportingmanagerrob,rob_dateofjoining,rob_employeeid'); if (resultXml != null && resultXml.selectSingleNode('//q1:rob_designationlocus') != null) { subjectLookupFieldObject = new Array(); subjectLookupFieldObject[0] = new Object(); subjectLookupId = resultXml.selectSingleNode('//q1:rob_designationlocus').nodeTypedValue; subjectLookupName = resultXml.selectSingleNode('//q1:rob_designationlocus').getAttribute("name"); subjectLookupType = resultXml.selectSingleNode('//q1:rob_designationlocus').getAttribute("type"); subjectLookupFieldObject[0].id = subjectLookupId; subjectLookupFieldObject[0].name = subjectLookupName; subjectLookupFieldObject[0].entityType = subjectLookupType; Xrm.Page.getAttribute('new_rrdesignation').setValue(subjectLookupFieldObject); } } } // Do not make any changes to this function function RetrieveEntityById(prmEntityName, prmEntityId, prmEntityColumns) { var resultXml, errorCount, msg, xmlHttpRequest, arrayEntityColumns, xmlEntityColumns; arrayEntityColumns = prmEntityColumns.split(","); for (var i = 0; i < arrayEntityColumns.length; i++) { xmlEntityColumns += "<q1:Attribute>" + arrayEntityColumns[i] + "</q1:Attribute>"; } var authenticationHeader = Xrm.Page.context.getAuthenticationHeader(); //Prepare the SOAP message. var xml = "<?xml version='1.0' encoding='utf-8'?>" + "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" + " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" + authenticationHeader + "<soap:Body>" + "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" + "<entityName>" + prmEntityName + "</entityName>" + "<id>" + prmEntityId + "</id>" + "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" + "<q1:Attributes>" + xmlEntityColumns + "</q1:Attributes>" + "</columnSet>" + "</Retrieve></soap:Body></soap:Envelope>"; //call function to create Soap Request to ms crm webservice xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false); xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve"); xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xmlHttpRequest.setRequestHeader("Content-Length", xml.length); xmlHttpRequest.send(xml); resultXml = xmlHttpRequest.responseXML; var errorCount = resultXml.selectNodes('//error').length; if (errorCount != 0) { var msg = resultXml.selectSingleNode('//description').nodeTypedValue; alert("Error Message : " + msg); } else { return resultXml; } }
If it helps, Mark as answer to help others..else it's scrap
Wednesday, November 28, 2012 12:24 PM