Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

CRM REST BUILDER WITH SOME MODIFICATION

Posted on by 100

Hello All, I am using this JS on an onchange event of a field and the output is 'Bad Request'. Please see the script below. Thank you!

function displayacctname() {
if (Xrm.Page.data.entity.attributes.get("new_lookuptest").getValue() != null) {
var CustomerId = Xrm.Page.data.entity.attributes.get("new_lookuptest").getValue()[0].id;
var CustomerName = Xrm.Page.data.entity.attributes.get("new_lookuptest").getValue()[0].name;
var CustomerType = Xrm.Page.data.entity.attributes.get("new_lookuptest").getValue()[0].entityType;
var api = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_accountfuels?$select=new_fueldba,new_fulladdress&$filter=new_accountfuelid eq '" +CustomerId+"'", 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 new_fueldba = result["new_fueldba"];
var new_fulladdress = result["new_fulladdress"];
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();

};
//Display the lookup name
alert(api);
alert(CustomerName);
alert(CustomerId);
alert(api);

};

*This post is locked for comments

  • RARO91 Profile Picture
    RARO91 100 on at
    RE: CRM REST BUILDER WITH SOME MODIFICATION

    I changed it and appended .replace and it partially helped! Thank you!

  • Verified answer
    RARO91 Profile Picture
    RARO91 100 on at
    RE: CRM REST BUILDER WITH SOME MODIFICATION

    I started developer tools(IE F12) and saw what the response was.

    A binary operator with incompatible types was detected. Found operand types 'Edm.Guid' and 'Edm.String' for operator kind 'Equal'.

    Type Mismatch. I was trying pass Guid to filter and that wasn't working.

    In the Open request, I was enclosing the GUID with quotes and that caused it not to work. remove the quotes. See below. req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_accountfuels?$select=new_fueldba,new_fulladdress&$filter=new_accountfuelid eq " +CustomerId", true);

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: CRM REST BUILDER WITH SOME MODIFICATION

    Hello,

    Try replace line

    var CustomerId = Xrm.Page.data.entity.attributes.get("new_lookuptest").getValue()[0].id;

    with line

    var CustomerId = Xrm.Page.data.entity.attributes.get("new_lookuptest").getValue()[0].id.replace("{", "").replace("}", "");

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans