6 Replies · Latest reply on Jan 16, 2018 6:54 AM by kevinray

    REST Attachment API

      I created a Scripted REST API to process some inbound XML i'm getting from a 3rd party vendor. Part of the payload includes base64 string which is a PDF. I'm able to use the REST Attachment API to successfully attach the pdf to the record, but when download the document from the record and try to open it i get error: "Adobe could not open document because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded" This leads me to belive i'm not decoding correctly as it states. I've taken the string payload, converted it to a binary file manually then tried to upload that and it works, so I know the string is valid. Can someone tell me what is wrong with my script that isn't decoding the string correctly?

       

      (function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
      
      //take our incoming XML And convert it to JSON
      var JSONString = gs.xmlToJSON(request.body.dataString);
      
      //parse out the information we need
      //Envelope ID
      var envID = JSONString.DocuSignEnvelopeInformation.EnvelopeStatus.EnvelopeID;
      
      //Envelope Status
      var dsStatus = JSONString.DocuSignEnvelopeInformation.EnvelopeStatus.Status;
      
      //the document in string value
      var pdfDoc = JSONString.DocuSignEnvelopeInformation.DocumentPDFs.DocumentPDF.PDFBytes;
      
      //The name of the document
      var fileName = JSONString.DocuSignEnvelopeInformation.DocumentPDFs.DocumentPDF.Name;
      
      //Update the appropriate Legal Request to indicate contracts signatures have been obtained
      var gr = new GlideRecord('sn_sm_legal_request');
      gr.addQuery('u_docusign_envelope_id',envID);
      gr.query();
      if(gr.next()){
      gr.u_docusign_signatures_obtained = true;
      gr.update();
      }
      
      // Attach the signed contract to the legal request
      
      var reqDoc = new sn_ws.RESTMessageV2();
      reqDoc.setEndpoint('https://xxxx.service-now.com/api/now/attachment/file?table_name=sn_sm_legal_request&table_sys_id='+gr.sys_id+'+&file_name='+fileName);
      reqDoc.setHttpMethod('POST');
      var strDecodedString = GlideStringUtil.base64Decode(pdfDoc);
      reqDoc.setRequestHeader("Accept","application/xml");
      reqDoc.setRequestHeader('Content-Type','document/pdf');request.setRequestBody(strDecodedString);
      var respDoc = reqDoc.execute();
      gs.log(respDoc.getBody());
      
      
      return {
      
      Status: dsStatus,
      };
      
      })(request, response);
      
        • Re: REST Attachment API
          Mukeshkumar Patel

          I think there might be issue with #35. Can you verify attachment that is being attached to ticket has correct type.

           

          Go to sys_attachment.list and make sure content type is correct. (like below)

           

          PS - Please mark Helpful, Like, or Correct Answer if applicable.

          https://www.linkedin.com/in/patelmike

          • Re: REST Attachment API
            Todd Withers

            Unfortunately, I think your stuck since you have a base64 string .

             

            REST Attachment API with Base64 String

             

            You need binary.

             

            BTW: there is an off-the-shelf tool for integrating with DocuSign in the ServiceNow app store.  It's called eSignifi

             

            Only problem is that it doesn't support DocuSign connect which I assume is what you're using.

              • Re: REST Attachment API
                kevinray

                Right, but that is yet another monthly subscription. Not necessary as i'm so close to having it built for free AND using DocuSign Connect. Thans

                  • Re: REST Attachment API
                    Todd Withers

                    Ok, maybe this using the ecc queue rest endpoint which seems to accept base 64

                     

                    //get a ref to the record you want to attach to

                    var gr = new GlideRecord("core_company");

                    gr.get("db9854bedbcb4700c1125200cf9619ca");

                     

                    //you'll need to create a basic auth/user record if you want to auth with basic otherwise maybe there's another way??

                    var basicAuthRecord = new GlideRecord("sys_auth_profile_basic");

                    basicAuthRecord.addQuery("name", "<<your service account here>>");

                    basicAuthRecord.query();

                    basicAuthRecord.next()

                     

                    var request = new sn_ws.RESTMessageV2();

                    request.setEndpoint("https://<<yourhostname>>/api/now/table/ecc_queue");

                    request.setAuthenticationProfile("basic", basicAuthRecord.getUniqueValue());

                    request.setRequestHeader('Accept', 'application/json');

                    request.setHttpMethod("POST");

                    request.setRequestBody(global.JSON.stringify({

                      'agent':'AttachmentCreator',

                      'topic':'AttachmentCreator',

                      'source':gr.getTableName() + ":" + gr.getUniqueValue(),

                      'name':'test6.pdf:application/pdf',

                      'payload':<your base64 string>

                    }));

                    var response = request.execute();

                    gs.info(response.getStatusCode());

                    1 of 1 people found this helpful
                • Re: REST Attachment API
                  kevinray

                  I'm posting the full code so that people can see it completed for future reference.

                   

                  this code:

                  - Parses JSON payload

                  - Updats a record with a value from that payload

                  - sends base64 string from payload to ecc queue to decode and attach it (pdf) to same record as above

                   

                   

                  (function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

                   

                   

                   

                  //----take our incoming XML And convert it to JSON

                  var JSONString = gs.xmlToJSON(request.body.dataString);

                   

                   

                  //----parse out the information we need

                  //Envelope ID

                  var envID = JSONString.DocuSignEnvelopeInformation.EnvelopeStatus.EnvelopeID;

                  //Envelope Status

                  var dsStatus = JSONString.DocuSignEnvelopeInformation.EnvelopeStatus.Status;

                  //the document in string value

                  var pdfDoc = JSONString.DocuSignEnvelopeInformation.DocumentPDFs.DocumentPDF.PDFBytes;

                  //The name of the document

                  var fileName = JSONString.DocuSignEnvelopeInformation.DocumentPDFs.DocumentPDF.Name;

                   

                   

                  //Update the appropriate Legal Request to indicate contracts signatures have been obtained

                  var gr = new GlideRecord('sn_sm_Legal_request');

                  gr.addQuery('u_docusign_envelope_id',envID);

                  gr.query();

                  if(gr.next()){

                  gr.u_docusign_signatures_obtained = true;

                  gr.update();

                   

                  }

                   

                  // ----Attach the signed contract to the legal request

                   

                   

                  //Get Authentication Record

                  var basicAuthRecord = new GlideRecord("sys_auth_profile_basic");

                  basicAuthRecord.addQuery("name", "<<MY SERVICE ACCOUNT WENT HERE>>");

                  basicAuthRecord.query();

                  basicAuthRecord.next();

                   

                   

                  //Send base64 string to ecc queue for processing

                  var r = new sn_ws.RESTMessageV2();

                  r.setEndpoint("https://xxxxxx/api/now/table/ecc_queue");

                  r.setAuthenticationProfile("basic", basicAuthRecord.getUniqueValue());

                  r.setRequestHeader('Accept', 'application/json');

                  r.setHttpMethod("POST");

                  r.setRequestBody(global.JSON.stringify({

                  'agent':'AttachmentCreator',

                  'topic':'AttachmentCreator',

                  'source':gr.getTableName() + ":" + gr.getUniqueValue(),

                  'name':fileName+':application/pdf',

                  'payload':pdfDoc,

                  }));

                   

                  var re = r.execute();

                   

                   

                  return {

                   

                  Status: gr.getTableName() + ":" + gr.getUniqueValue(),

                  };

                   

                   

                  })(request, response);