script not working from BR/workflow

Abdul Musavvir
Kilo Guru

Hi All,

 

I'm trying to generate a dynamic excel attachment from script. It is working perfectly fine but not from BR/workflow.
And also I'm getting all the logs with perfect value. Please suggest how to make it work from BR n workflow.

var selected_inc = "INC0000029,INC0000028,INC0000024,INC0000055";
var query = "incident_list.do?EXCEL&sysparm_query=numberIN"+selected_inc;

var instance_name = gs.getProperty('instance_name');

gs.print("query abdul" + instance_name +" " + query);
var sys_id ="b088ecfc2f3b3510c4fcfe1df699b69f";
try { 
   var request = new sn_ws.RESTMessageV2();
   var instanceUrl = "https://"+instance_name+".service-now.com/"; 
	gs.print('url abdul'+instanceUrl + query);
   request.setEndpoint(instanceUrl + query); 
   request.setHttpMethod("GET");

   var user = "admin"; 
   var password = "ABCD";

   request.setBasicAuth(user, password); 
   //request.setRequestHeader("Accept", "application/json");

   gs.print("before resonse abdul");
request.saveResponseBodyAsAttachment("sc_req_item",sys_id,'resolved_incidents.xls');
	gs.print("after resonse abdul ");

   var response = request.execute(); 
   var httpStatus = response.getStatusCode(); 
   gs.print(httpStatus); 
} catch (ex) { 
      var message = ex.getMessage(); 
      gs.print(message); 
}

 

 

-Abdul Musavvir

6 REPLIES 6

Aniket Chavan
Tera Sage
Tera Sage

Hello @Abdul Musavvir ,

Please give a try to the script below and let me know how it works for you.

var selected_inc = "INC0000029,INC0000028,INC0000024,INC0000055";
var query = "incident_list.do?EXCEL&sysparm_query=numberIN" + selected_inc;

var instance_name = gs.getProperty('instance_name');
var sys_id = "b088ecfc2f3b3510c4fcfe1df699b69f";

try {
   var request = new sn_ws.RESTMessageV2();
   var instanceUrl = "https://" + instance_name + ".service-now.com/";

   gs.print('URL: ' + instanceUrl + query);

   request.setEndpoint(instanceUrl + query);
   request.setHttpMethod("GET");

   var user = "admin";
   var password = "ABCD";

   request.setBasicAuth(user, password);

   gs.print("Before REST call");
   var response = request.execute();
   gs.print("After REST call");

   var httpStatus = response.getStatusCode();
   gs.print("HTTP Status Code: " + httpStatus);

   if (httpStatus === 200) {
      // Create Attachment using GlideRecord
      var attachment = new GlideRecord('sys_attachment');
      attachment.initialize();
      attachment.file_name = 'resolved_incidents.xls';
      attachment.content_type = 'application/vnd.ms-excel';
      attachment.table_sys_id = sys_id;
      attachment.file = response.getBody();
      attachment.insert();

      gs.print("Attachment created successfully");
   } else {
      gs.print("Error: " + response.getErrorMessage());
   }
} catch (ex) {
   var message = ex.getMessage();
   gs.print("Exception: " + message);
}

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

It worked partially but the attachment generated was of 0 bytes. 

Ankur Bawiskar
Tera Patron
Tera Patron

@Abdul Musavvir 

from where it's working fine?

where did you write the code when it worked?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar it worked on background script.