0

I was able to upload attachment of a form in ServiceNow to google drive through integration. But the file is stored in google drive as 'untitled'. But it should have a custom name eg'Invoice'..

Below I have shared my business rule script. Can anyone help to solve this.

  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.addQuery('file_name', current.u_number+'Purchase order.pdf');
attachmentGr.query();
    
if (attachmentGr.next()) {
    var fileName = attachmentGr.current('file_name').toString();
    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/drive/v3/files');
    restMessage.setEndpoint('https://www.googleapis.com/upload/drive/v2/files');
    restMessage.setRequestHeader('Authorization', 'Bearer ' + rt );
    restMessage.setRequestHeader('Content-Type', 'application/pdf');
    restMessage.setRequestHeader('uploadType', 'multipart');
    restMessage.setRequestBodyFromAttachment(attachmentSysId);
    //restMessage.addHeader("Content-Disposition", 'attachment; filename ="' +fileName+'"');
    //restMessage.addHeader("Content-Disposition", fileName);
    //restMessage.setRequestBody(contentStream);
    
    
//restMessage.saveResponseBodyAsAttachment(filename);
    restMessage.setRequestQueryParameter('name', 'Invoice');
    var response = restMessage.execute(); 
    if (response.getStatusCode() == 200) {
        var responseBody = response.getBody();
         var jsonResponse = JSON.parse(responseBody);
          jsonResponse.name = fileName;
        
        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);
TylerH
  • 20,799
  • 66
  • 75
  • 101

0 Answers0