Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

xrm.webAPI createRecord v9 returning Bad Request Error

(0) ShareShare
ReportReport
Posted on by 392

Hi All, I have being trying to generate PDF from an SSRS report, everything works, but I am getting an error when I am trying to attach the PDF file to a note.  The Api POST call to create the note returns a BAD Request error. My code is below, the code below generates the PDF and then my Create nte method is called inside it.

function encodePdf(responseSession) {

    //Create request object that will be called to convert the response in PDF base 64 string
    var retrieveEntityReq = new XMLHttpRequest();

     //Create query string that will be passed to Report Server to generate PDF version of report response.
    var pth = Xrm.Page.context.getClientUrl() + "/Reserved.ReportViewerWebControl.axd?ReportSession=" + responseSession[0] + "&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=" + responseSession[1] + "&OpType=Export&FileName=Public&ContentDisposition=OnlyHtmlInline&Format=PDF";
    retrieveEntityReq.open("GET", pth, true);
    retrieveEntityReq.setRequestHeader("Accept", "*/*");
    retrieveEntityReq.responseType = "arraybuffer";
    retrieveEntityReq.onreadystatechange = function () {

        if (retrieveEntityReq.readyState == 4 && retrieveEntityReq.status == 200) {
            var binary = "";
            var bytes = new Uint8Array(this.response);

            for (var i = 0; i < bytes.byteLength; i++) {
                binary += String.fromCharCode(bytes[i]);
            }
            var base64PDFString  = btoa(binary);
            createNote(base64PDFString );
        }
    };
    //This statement sends the request for execution asynchronously. Callback function will be called on completion of the request.
    retrieveEntityReq.send();


  The method to create a note:

function createNote(data) {


    var recordId = Xrm.Page.data.entity.getId();
    recordId = recordId.replace('{', '').replace('}', '');

    var AccountName = Xrm.Page.getAttribute("name").getValue();
   
    
    var note =
    {
        "objectid": recordId,
        "objecttypecode":"invoice",
        "filename": AccountName + ".pdf",
        "subject": "Invoice: " + AccountName,
        "documentbody": data,
        "mimetype": "application/pdf",
        "isdocument": 1
    }
    
    Xrm.WebApi.createRecord("annotation", note).then(
        function success(result) {
            console.log("note created with ID: " + result.id);
            Xrm.Page.data.refresh(false);
            // perform operations on record creation
        },
        function (error) {
            console.log(error.message);
            // handle error conditions
        }
    );
    
}

What I'm I doing wrong here?

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: xrm.webAPI createRecord v9 returning Bad Request Error

    Just figure it out. It is salesorders :)

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: xrm.webAPI createRecord v9 returning Bad Request Error

    Hi. I am trying this but having issues. In my case I am trying to create a note in an order.

    something like this:

    var note =
                {
                    "objectid_salesorder@odata.bind": "/salesorder(" + recId + ")",
                    "objecttypecode": "salesorder",
                    "subject": title,
                    "isdocument": false,
                    "notetext": text
                };

    where is "/salesorder(" I already tried "order", "orders", "Order", "Orders" but always the same error: Resource not found for the segment 'salesorder' (or order, orders, etc)

    What am I doing wrong?

    In my case I am just creating a text note, without any attachments.

    Thanks in advance.

  • Victor Onyebuchi Profile Picture
    Victor Onyebuchi 392 on at
    RE: xrm.webAPI createRecord v9 returning Bad Request Error

    Kalpavruksh D365 CoE this worked. You're star bro, i have been on this for day. 

    Thanks!

  • Verified answer
    Kalpavruksh D365 CoE Profile Picture
    Kalpavruksh D365 CoE 2,545 on at
    RE: xrm.webAPI createRecord v9 returning Bad Request Error
     
    This how you can construct a note object. This a sample JSON format for creating a note-invoice.
     
    {    "objectid_invoice@odata.bind": "/invoices(5a9a8b6d-3a97-e911-a97b-000d3af29269)",
            "objecttypecode":"invoice",
            "filename": "Sample.pdf",
            "subject": "Invoice: ",
            "documentbody": "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=",
            "mimetype": "application/pdf",
            "isdocument": true
     
    Kindly update your code:
    var note =
        {
            "objectid_invoice@odata.bind": "/invoices("+recordId  +")",
            "objecttypecode":"invoice",
            "filename": AccountName + ".pdf",
            "subject": "Invoice: " + AccountName,
            "documentbody": data,
            "mimetype": "application/pdf",
            "isdocument": true
        }
        ]
    
  • necsa Profile Picture
    necsa 3,455 on at
    RE: xrm.webAPI createRecord v9 returning Bad Request Error

    Hi Boss,

    please debug your code and check the message of debugging. You will see POST message that the URL is difference then you define in your code. Match your code URL then it must working.

    Your code can work with Dynamics CRM V8.x but not with V 9.0

    Please take as reference following page

    www.cloudfronts.com/solved-corrupted-report-pdf-generated-using-javascript-in-dynamics-365-v9-0

  • Victor Onyebuchi Profile Picture
    Victor Onyebuchi 392 on at
    RE: xrm.webAPI createRecord v9 returning Bad Request Error

    Kalpavruksh D365 CoE still the same error. Dont you think its the argument that being passed in, thats making it to fail ?

  • Suggested answer
    Kalpavruksh D365 CoE Profile Picture
    Kalpavruksh D365 CoE 2,545 on at
    RE: xrm.webAPI createRecord v9 returning Bad Request Error
    Hi,
    Please find the comment below, "isdocument" is a Boolean field. Kindly pass true value. 
    5556.MicrosoftTeams_2D00_image-_2800_10_2900_.png

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

Announcing Our 2025 Season 1 Super Users!

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

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

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,317 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans