0

I have PDF URL which i need to attach to my Notes area using javascript. I have tried below code but it didn't work. It says that PDF is corrupt while opening the PDF. Notes is getting created but when I click on PDF attachment. It throw an error of corrupt file while opening it.

    function CreateNotes()
    {
    var Annotation = new Object();
    Annotation.ObjectId = { LogicalName: "quote", Id: RecordId, Name: "" };
    Annotation.Subject = "Quote Report";
    Annotation.NoteText = "Downloaded Today";
    Annotation.FileName = "Quote.pdf";

    var PDFContentBase64 = btoa(unescape(encodeURIComponent(PDFURL)));
    Annotation.DocumentBody = PDFContentBase64;
    Annotation.MimeType = "application/pdf";


     createRecordSync(Annotation,"AnnotationSet");
     }

     function createRecordSync(entityObject, odataSetName) {
     var jsonEntity = window.JSON.stringify(entityObject);
     var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
     var createRecordReq = new XMLHttpRequest();
     var ODataPath = getServerUrl() + ODATA_ENDPOINT;
     createRecordReq.open("POST", ODataPath + "/" + odataSetName, false);
     createRecordReq.setRequestHeader("Accept", "application/json");
    createRecordReq.setRequestHeader("Content-Type", "application/json;   charset=utf-8");
    createRecordReq.send(jsonEntity);
    var newRecord = JSON.parse(createRecordReq.responseText).d;
    return newRecord;
    }
    function getServerUrl() {
        return Xrm.Page.context.getClientUrl()
     }
Kartik Patel
  • 9,303
  • 12
  • 33
  • 53

1 Answers1

0

Notes in CRM cannot contain links to external resources.

You can convert your entire PDF to base64 and store that in CRM with the approach you are currently using, but it does not appear to be what you are doing (encodeURIComponent(PDFURL) only encodes the URI - it does not actually fetch the PDF itself).

In case you actually wanted to store the entire PDF in CRM, you would need to fetch the PDF itself and base64 encode that.

Community
  • 1
  • 1
Henrik H
  • 5,747
  • 1
  • 21
  • 33