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)