Hi, i want to retrieve look up value but this script returning bad request.
function autoCompleteAreaWhenRegionSelect() { var selectedRegion = Xrm.Page.getAttribute("lnkt_regionarea").getValue(); if (selectedRegion != null) { //var selectedRegionName = selectedRegion[0].name; var selectedRegionGuidId = selectedRegion[0].id; var selectedRegionEntityType = selectedRegion[0].entityType; selectedRegionGuidId = selectedRegionGuidId.replace('{', '').replace('}', ''); var serverUrl = window.location.protocol + "//" + window.location.host + "/" + Xrm.Page.context.getOrgUniqueName(); var request = serverUrl + "/XRMServices/2011/OrganizationData.svc/lnkt_regionareaSet?$select=lnkt_Area,lnkt_regionareaId&$filter=lnkt_regionareaId equal guid'" + selectedRegionGuidId + "'"; var response = sendRequestAjax(request); var area = response.lnkt_Area; if (response.lnkt_Area != null) { var areaLookupObject = new Array(); areaLookupObject[0] = new Object(); areaLookupObject[0].id = area.Id; areaLookupObject[0].entityType = area.LogicalName; areaLookupObject[0].name = area.Name; Xrm.Page.getAttribute("lnkt_regionid").setValue(areaLookupObject); } //set value other field as null Xrm.Page.getAttribute("lnkt_hubcodeid").setValue(); Xrm.Page.getAttribute("lnkt_nodecodeid").setValue(); } else { Xrm.Page.getAttribute("lnkt_hubcodeid").setValue(); Xrm.Page.getAttribute("lnkt_nodecodeid").setValue(); } } function sendRequestAjax(requestMessage) { var response = new Object(); var request = requestMessage; $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: request, async: false, beforeSend: function(XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function(data, textStatus, XmlHttpRequest) { response = data.d.results[0]; }, error: function(XmlHttpRequest, textStatus, errorThrown) { alert('Exception while sending request to CRM Server :' + errorThrown); } }); return response; }
In short, this script will retrieve lookup value from entity Regionarea, then paste the value into a field in the entity where this script is placed.
I have checked all the field schema name inside this script and im sure that the field name was correct.
Kindly need your help
*This post is locked for comments
We have resolved this. We need to check all parameters value. By doing this, we found that in the script below is returning 'undefined'
var selectedRegion = Xrm.Page.getAttribute("lnkt_regionarea").getValue();
We change the script into this one
var selectedRegion = Xrm.Page.getAttribute("lnkt_regionarea").getValue()[0].id;
Hell Fikri,
I am agreed with Mahender , its easy to generate the code using RESTBuilder , try to download rest builder and generate your code you dont need to write anything to retrieve the records.
However I found some issues with your code , comment the highlighted line and make sure are you using correct Schema Name of the field. Hope this helps.
function autoCompleteAreaWhenRegionSelect() { var selectedRegion = Xrm.Page.getAttribute("lnkt_regionarea").getValue(); if (selectedRegion != null) { //var selectedRegionName = selectedRegion[0].name; var selectedRegionGuidId = selectedRegion[0].id; var selectedRegionEntityType = selectedRegion[0].entityType; // selectedRegionGuidId = selectedRegionGuidId.replace('{', '').replace('}', ''); //Not Required to remove {} var serverUrl = window.location.protocol + "//" + window.location.host + "/" + Xrm.Page.context.getOrgUniqueName(); var request = serverUrl + "/XRMServices/2011/OrganizationData.svc/lnkt_regionareaSet?$select=lnkt_Area,lnkt_regionareaId&$filter=lnkt_regionareaId equal guid'" + selectedRegionGuidId + "'"; var response = sendRequestAjax(request); var area = response.lnkt_Area; if (response.lnkt_Area != null) { var areaLookupObject = new Array(); areaLookupObject[0] = new Object(); areaLookupObject[0].id = area.Id; areaLookupObject[0].entityType = area.LogicalName; areaLookupObject[0].name = area.Name; Xrm.Page.getAttribute("lnkt_regionid").setValue(areaLookupObject); } //set value other field as null Xrm.Page.getAttribute("lnkt_hubcodeid").setValue(); Xrm.Page.getAttribute("lnkt_nodecodeid").setValue(); } else { Xrm.Page.getAttribute("lnkt_hubcodeid").setValue(); Xrm.Page.getAttribute("lnkt_nodecodeid").setValue(); } } function sendRequestAjax(requestMessage) { var response = new Object(); var request = requestMessage; $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: request, async: false, beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function (data, textStatus, XmlHttpRequest) { response = data.d.results[0]; }, error: function (XmlHttpRequest, textStatus, errorThrown) { alert('Exception while sending request to CRM Server :' + errorThrown); } }); return response; }
Hi,
Try to debug your script and check if request URL is constructed correctly or not, you can also try to first browse the url itself to check if it's returning some data or not.
you can also use OData designer, it will be easily: github.com/.../CRMRESTBuilder once your have tested your request you can do further modification accordingly.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156