Hi all,
This is my scenario:
On cases I've got the following field:
Customer - One of the out of the box fields that can point to either Account or Contact. I've added a javascript function so that it filters only to contacts and doesn't display any accounts. Therefore Customers will now always be a contact
Account - A custom lookup field referencing the Account entity. This needs to default to the Company Name account ID of the contact that was set in the Customer field above. I created a javascript function as per the below and it worked quite well.
function setAccount(executionContext) { var formContext = executionContext.getFormContext(); var customer = formContext.getAttribute("customerid").getValue(); // Check if customer field contains data if (customer != null){ // convert the returned Id into a useable format var newid = customer[0].id.slice(1, -1); var req = new XMLHttpRequest(); req.open("GET", Xrm.Page.context.getClientUrl() "/api/data/v9.1/contacts(" newid ")?$select=_parentcustomerid_value", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); req.onreadystatechange = function() { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var result = JSON.parse(this.response); var companyNameId = result["_parentcustomerid_value"]; var companyNameFormattedValue = result["_parentcustomerid_value@OData.Community.Display.V1.FormattedValue"]; var companyNameEntityName = result["_parentcustomerid_value@Microsoft.Dynamics.CRM.lookuplogicalname"]; formContext.getAttribute("new_account").setValue([{ name: companyNameFormattedValue, id: companyNameId, entityType: companyNameEntityName }]); } else { Xrm.Utility.alertDialog(this.statusText); } } }; req.send(); } }
Currency - A custom lookup field referencing the Currency entity. I now need this to get the currency ID of the companyNameId variable in the above javascript and then set it to the custom Currency lookup field on cases. This is where I'm getting stuck. No matter what I do I can't seem to set the currency ID to the same value as from the account. Below is the addition to the above code I've added to do this. Not sure what I'm doing wrong. Any help would be greatly appreciated.
function setAccountAndCurrency(executionContext) { var formContext = executionContext.getFormContext(); var customer = formContext.getAttribute("customerid").getValue(); // Check if customer field contains data if (customer != null){ // convert the returned Id into a useable format var newid = customer[0].id.slice(1, -1); // Get contact details with Company Name ID var req = new XMLHttpRequest(); req.open("GET", Xrm.Page.context.getClientUrl() "/api/data/v9.1/contacts(" newid ")?$select=_parentcustomerid_value,new_filterValue", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); req.onreadystatechange = function() { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var result = JSON.parse(this.response); //Store company ID into a format usable in cases var companyNameId = result["_parentcustomerid_value"]; var companyNameFormattedValue = result["_parentcustomerid_value@OData.Community.Display.V1.FormattedValue"]; var companyNameEntityName = result["_parentcustomerid_value@Microsoft.Dynamics.CRM.lookuplogicalname"]; // Retrieve the filter value from contact for use later on var filterId = result["new_filterValue"]; if(companyNameId != null){ //Check to see if contacts has an associated company // Set the refrential Account lookup field value on cases to the company of the contact formContext.getAttribute("new_account").setValue([{ name: companyNameFormattedValue, id: companyNameId, entityType: companyNameEntityName }]); var req = new XMLHttpRequest(); req.open("GET", Xrm.Page.context.getClientUrl() "/api/data/v9.1/accounts(" companyNameId ")?$select=_transactioncurrencyid_value", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); req.onreadystatechange = function() { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var result = JSON.parse(this.response); var currencyId = result["_transactioncurrencyid_value"]; var currencyFormattedValue = result["_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue"]; var currencyEntityName = result["_transactioncurrencyid_value@Microsoft.Dynamics.CRM.lookuplogicalname"]; if(currencyId != null){ formContext.getAttribute("new_currency").setValue([{name: currencyFormattedValue, id: currencyId, entityType: currencyEntityName}]); } }; req.send(); } } } else { Xrm.Utility.alertDialog(this.statusText); } } }; req.send(); } }}
I've never tried to retrieve values from multiple entities to then set lookup field values on a case before.