Skip to main content

Notifications

Announcements

No record found.

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

  • Verified answer
    Community Member Profile Picture
    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
    50,091 Moderator 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
    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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans