web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM 2016 Javascript return bad request

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    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.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    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;
    }
  • Verified answer
    Community Member Profile Picture
    on at

    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;




Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans