web
You’re offline. This is a read only version of the page.
close
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 410

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

I have the same question (0)
  • Suggested answer
    Kalpavruksh D365 CoE Profile Picture
    2,545 on at
    Hi,
    Please find the comment below, "isdocument" is a Boolean field. Kindly pass true value. 
    5556.MicrosoftTeams_2D00_image-_2800_10_2900_.png
  • Victor Onyebuchi Profile Picture
    410 on at

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

  • necsa Profile Picture
    3,455 on at

    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

  • Verified answer
    Kalpavruksh D365 CoE Profile Picture
    2,545 on at
     
    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
        }
        ]
    
  • Victor Onyebuchi Profile Picture
    410 on at

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

    Thanks!

  • Community Member Profile Picture
    on at

    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.

  • Community Member Profile Picture
    on at

    Just figure it out. It is salesorders :)

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans