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

Well, well well...I'll be an SOB..That worked! Thank you sir.


kevinray
Giga Expert

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