REST Attachment API

kevinray
Giga Expert

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='+g...

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);

1 ACCEPTED SOLUTION

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());


View solution in original post

6 REPLIES 6

Mike Patel
Tera Sage

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)



find_real_file.png


twithers63
Giga Contributor

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.


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


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());