Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Bad request

(0) ShareShare
ReportReport
Posted on by

I want to get invoice auto populate customer based on the invoice number entered in contact form and the other entity is Invoice entity.But i am getting Bad request after running it.Following is my code:

function InvoiceNumberOnChange(){

var invoiceNumber = Xrm.Page.getAttribute("new_invoicenumber").getValue();
if (invoiceNumber){
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/invoices?$select=CustomerId&$filter=invoicenumber eq '" +invoiceNumber+ "'", 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 results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
var fieldId = results.value[i]["CustomerId"];
var value = results.value[i]["CustomerId@OData.Community.Display.V1.FullName"];
var fieldType = results.value[i]["CustomerId@Microsoft.Dynamics.CRM.Invoices"];

SetLookUp("new_invoicenumber",fieldType, fieldId, value)
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
}

// *** FUNCTION: SetLookUp
// *** PARAMS:
// *** fieldName = The name of the lookup field
// *** fieldType = The type of field (contact, account etc)
// *** fieldId = The ID of the value to set (GUID)
// *** value = the value(name) to set
function SetLookUp(fieldName, fieldType, fieldId, value) {
try {
var object = new Array();
object[0] = new Object();
object[0].id = fieldId;
object[0].name = value;
object[0].entityType = fieldType;
Xrm.Page.getAttribute(fieldName).setValue(object);
}
catch (e) {
alert("Error in SetLookUp: fieldName = " + fieldName + " fieldType = " + fieldType + " fieldId = " + fieldId + " value = " + value + " error = " + e);
}
}

*This post is locked for comments

  • Raghu_b Profile Picture
    Raghu_b 228 on at
    RE: Bad request

    Hi Aditi,

    You are getting Bad Request because your query string parameter is wrong. Any field other than the primary key is used to filter the records in the Query string must be declared as a Alternate key and then used in the Query.

    Have you declared the new_invoice field as an Alternate key?

    After that the Query should look like below, here I'm using the emailaddress1 as an Alternate key-

    query = "/api/data/v8.0/accounts(emailaddress1='"+alternateKey+"')?$select=name,accountid,_transactioncurrencyid_value,vm_agencydiscount";

    FYI-

    http://www.inogic.com/blog/2016/10/retrieve-update-and-delete-record-using-alternate-key-in-dynamics-crm-web-api/

  • RE: Bad request

    look up field is new_invoice_contact

  • Suggested answer
    RE: Bad request

    Please verify if API version is v8.2. Go to Settings> Customzations> Developer Resources:

    221143.Capture.PNG

    In Contact Entity the fields are:-

    Invoice number Schema Name: new_invoicenumber , Is this a lookup or text field?

    Customer name Schema Name: FullName, Is this a lookup or text field?

  • RE: Bad request

    Hello,

    Still i am getting the error bad request

  • Suggested answer
    RE: Bad request

    Use this code:

    function InvoiceNumberOnChange(){
    var invoiceNumber = Xrm.Page.getAttribute("new_invoicenumber").getValue();
    if (invoiceNumber){
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/invoices?$select=CustomerId&$filter=invoicenumber eq '" +invoiceNumber+ "'", 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 results = JSON.parse(this.response);
    for (var i = 0; i < results.value.length; i++) {
    //var fieldId = results.value[i]["CustomerId"];
    var value = results.value[i]["CustomerId@OData.Community.Display.V1.FullName"];
    //var fieldType = results.value[i]["CustomerId@Microsoft.Dynamics.CRM.Invoices"];
    
    Xrm.Page.getAttribute("fullname").setValue(value);
    }
    } else {
    Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send();
    }
    }


  • RE: Bad request

    In Invoice Entity the fields are:-

    Invoice number Schema Name: InvoiceNumber

    Customer name Schema Name:  CustomerId

    In Contact Entity the fields are:-

    Invoice number Schema Name: new_invoicenumber

    Customer name Schema Name: FullName

    I want to get invoice auto populate customer based on the invoice number entered in contact form and the other entity is Invoice entity.But i am getting Bad request.

  • RE: Bad request

    You can't set FullName field with a lookup object. What field you want to set on contact form? what is the data type of the field.

  • RE: Bad request

    Hi,

    I have changed the code to SetLookUp("FullName",fieldType, fieldId, value) but still the error is coming up as Bad request.

  • Suggested answer
    RE: Bad request

    You are setting the invoice number field with customer lookup values. I think that's the issue.

    SetLookUp("new_invoicenumber",fieldType, fieldId, value)

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,432 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans