Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

CRM 2016 Javascript return bad request

Posted on by Microsoft Employee

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

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM 2016 Javascript return bad request

    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;




  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: CRM 2016 Javascript return bad request

    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;
    }
  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: CRM 2016 Javascript return bad request

    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.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans