Getting attachments from 3rd party via REST API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-08-2018 09:41 AM
Hello fellow Developers.
New to REST and been stuck for a couple of days now trying to figure this out. We are getting an XML response from a 3rd party that contains a base64 string, which represents an attachment and i need to convert and attach it to the proper record.
It is not possible for us to use the OOTB Attachments API because the 3rd party can only send us all communications via a single endpoint, so regardless if they are sending us a status, or a document, it all has to come in via one endpoint. So, I wrote a Scripted REST API to get their response but I can not figure out how to convert that string to a document and attach it to the proper record.
My scripted REST API looks like this so far:
I Get the info I need,
Find the record,
Update a check box to indicate everything is done and we are getting an attachment,
--but this is where i'm stuck..how do i decode the string and document name, convert to a doc, and put it in the attachment table linked to the correct record (where reqNumber is the record where I want the attachment. I.e.LR1234567) --- THis is the "DocumentPDFs section i'm speaking of
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
//take our incoming XML And convert it to JSON
var JSONString = gs.xmlToJSON(request.body.dataString);
var envID = JSONString.DocuSignEnvelopeInformation.EnvelopeStatus.EnvelopeID;
var pdfDocument = JSONString.DocuSignEnvelopeInformation.DocumentPDFs.DocumentPDF.PDFBytes;
var reqNumber = JSONString.DocuSignEnvelopeInformation.CustomVariables.snReqNumber;
//Update the appropriate Legal Request
var gr = new GlideRecord('sn_sm_legal_request');
gr.addQuery('number',reqNumber);
gr.query();
if(gr.next()){
gr.u_docusign_signatures_obtained = true;
gr.update();
}
var gra = new GlideRecord('sys_attachment');
?????????????????????
return {
// Get Data We need
Status: Success,
};
})(request, response);
This is what part of the XML looks like
<?xml version='1.0' encoding='UTF-8'?>
<DocuSignEnvelopeInformation xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://www.docusign.net/API/3.0'>
<EnvelopeStatus>
<TimeGenerated>2018-01-04T11:09:04.5725923</TimeGenerated>
<EnvelopeID>0f825144-8366-52j2-8f3f-bffd48a94705</EnvelopeID>
<Subject>Test Number 2</Subject>
<snReqNumber>LR1234567</snReqNumber>
<UserName>test account</UserName>
<Email>somebusiness@gmail.com</Email>
<Status>Completed</Status>
<Created>2018-01-04T11:07:49.307</Created>
<Sent>2018-01-04T11:07:50.227</Sent>
<Delivered>2018-01-04T11:08:36.95</Delivered>
<Signed>2018-01-04T11:08:42.247</Signed>
<Completed>2018-01-04T11:08:42.247</Completed>
<ACStatus>Original</ACStatus>
<ACStatusDate>2018-01-04T11:07:49.307</ACStatusDate>
<ACHolder>test account 2</ACHolder>
<ACHolderEmail>somebusiness@gmail.com</ACHolderEmail>
<ACHolderLocation>DocuSign</ACHolderLocation>
<SigningLocation>Online</SigningLocation>
<SenderIPAddress>199.91.140.12 </SenderIPAddress>
<EnvelopePDFHash/>
<CustomFields/>
<AutoNavigation>true</AutoNavigation>
<EnvelopeIdStamping>true</EnvelopeIdStamping>
<AuthoritativeCopy>false</AuthoritativeCopy>
<DocumentStatuses>
<DocumentStatus>
<ID>1</ID>
<Name>1_signature_block.pdf</Name>
<TemplateName/>
<Sequence>1</Sequence>
</DocumentStatus>
</DocumentStatuses>
</EnvelopeStatus>
<DocumentPDFs>
<DocumentPDF>
<Name>1_signature_block.pdf</Name>
<PDFBytes>
JVBERi0xLjYKJfv8/f4KJVdyaXRpbmcgb2JqZWN0cy4uLgo0IDAgb2JqCjw8Ci9Bbm5vdHMgMTUgMCBSCi9Db250ZW50cyBbMTYgMCBSIDE3IDAgUiAxOCAwIFIgMTkgMCBSIDIwIDAgUiBdCi9Dcm9wQm94IFswLjAwMDAwIDAuMDAwMDAgNjEyLjAwMDAwIDc5Mi4wMDAwMCBdCi9NZWRpYUJveCBbMC4wMDAwMCAwLjAwMDAwIDYxMi4wMDAwMCA3OTIuMDAwMDAgXQovUGFyZW50IDMgMCBSCi9SZXNvdXJjZXMgMjEgMCBSCi9Sb3RhdGUgMAovU3RydWN0UGFyZW50cyAwCi9UYWJzIC9TCi9UeXBlIC9QYWdlCj4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Ha3XL9ZX9Y+31zgPG4nk1hUEwvOWwegAM8bA60xWsOltkS3M2AM17Sy3dQXDJUICkOF+C2gxHnaDnXmnOl6CnoOad1QWsa91yO3dfWhQ4uiCtNSXEiW1aENIQkoJaJGfb0XWPfg4yHYBbPPJQxlpqk+CqxTTqXOXpo41cM7RlbRkZgKxNQ8wXJMackvBdB/sX0w4zQSabWeG4RWQs+tyyPBpmlqBxF5wxl1lfVs+DRYrIcmwsjeNYishbMi9aX4HQ4kdVA6Ly/vgxn/vI6CJMN1RPCNY+b209312735OfbvYfF8q98rF3beIJUZVyFzkOmRNs2Jb0pqLRkldXnnYexymOFE0iLABeGaUpNW+Wr1zXpEBHDH
</PDFBytes>
</DocumentPDF>
</DocumentPDFs>
</DocuSignEnvelopeInformation

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-08-2018 12:57 PM
Since you have the Base64 string already, you could make a SOAP call back to your instance and use the AttachmentCreator Web Service to do the work for you.
See the link below.
AttachmentCreator SOAP web service
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-08-2018 01:11 PM
Hi thanks for the reply. Okay, sounds straight forward, but how do I make a SOAP call from the Scripted REST API?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2018 01:58 PM
We fought with this a lot while we were building eSignifi (DocuSign integration with ServiceNow)
This would work:
var filebytes = gs.somehowconvertb64tobinary(b64string);
var gsa = new GlideSysAttachment();
gsa.write(gr, "my file.pdf", "application/pdf", fileBytes);
If you can find someway to turn the b64 string into binary (gs.base64Decode only works with string values). Do you know any way to do that? This would really help me too.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2018 04:53 PM
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 base 64 string>
}));
var response = request.execute();
gs.info(response.getStatusCode());