Upload pdf to google drive from ServiceNow

Sharath807
Tera Contributor

Hi all I want to copy a attachment from servicenow to google drive using api integration.. I was able to send attachment to google drive.. Now the problem is ,it is getting attachment uploaded as 'Untitled' , But I want it to be named example 'Invoice'. and also i need that attachment should be seen in particular folder in google drive . what line i should add in script so that i can achieve this anyone have idea on this. can help?

5 REPLIES 5

Tony Chatfield1
Kilo Patron

Hi, without any details of your process\code, it is not possible for the community to assess your issue.
perhaps you could update this thread with clear and specific details of your script (as plain text).

@Tony Chatfield1 Hi sir.. sure below I have added my after update business rule script

 

(function executeRule(current, previous /*null when async*/) {
var poFormSysId = current.sys_id;
 
var attachmentGr = new GlideRecord('sys_attachment');
attachmentGr.addQuery('table_name', 'u_po_form');
attachmentGr.addQuery('table_sys_id', poFormSysId);
 
attachmentGr.query();
 
if (attachmentGr.next()) {
var fileName = attachmentGr.getValue('file_name');
var attachmentSysId = attachmentGr.getValue('sys_id');
//gs.addInfoMessage(attachmentSysId);
var oauth = new GoogleDrive();
    var authorizationUrl = oauth.getAuthorizationUrl();
//gs.addInfoMessage(authorizationUrl);
var at = oauth.getAccessToken();
var rt = oauth.refreshAccessToken();
//gs.addInfoMessage(rt);
//gs.addInfoMessage(at);
    var restMessage = new sn_ws.RESTMessageV2(); // create a new REST message
    restMessage.setHttpMethod('POST'); // set the HTTP method
    restMessage.setEndpoint('https://www.googleapis.com/upload/drive/v2/files');
    restMessage.setRequestHeader('Authorization', 'Bearer ' + rt );
    restMessage.setRequestHeader('Content-Type', 'application/pdf');
restMessage.setRequestBodyFromAttachment(attachmentSysId);
restMessage.addHeader("Content-Disposition", 'attachment; filename ="' +fileName+'"');
//restMessage.addHeader("Content-Disposition", fileName);
//restMessage.setRequestBody(contentStream);
 
        
 
    restMessage.setRequestQueryParameter('name', 'Invoice');
    var response = restMessage.execute(); 
    if (response.getStatusCode() == 200) {
        gs.addInfoMessage('PDF file uploaded to Google Drive');
    } else {
        gs.addErrorMessage('Error uploading PDF file to Google Drive: ' + response.getBody()); 
    }
} else {
   gs.addInfoMessage('No PDF file found for PO form ');
}
 
})(current, previous);

What happens when you do application/json instead of application/pdf

@DanielCordick creating 'Untitled' docs without content