output from fix script to a file

samadam
Kilo Sage

Is it possible to output gs.print to a log file from a fix script or scheduled job?

2 REPLIES 2

Daniel Draes
ServiceNow Employee
ServiceNow Employee

No, this is not possible. and even if it would be, you would not be able to get to that file



What I usually do in such case is one of the following:


a) write to log table using gs.log, gs.info etc.


b) You can create a file and attach it to a record - i tend to use my user record for such things See example below



// array to collect what needs to be stored in file


var updated_records = [];



// loop here on your content and store what you need in array...


// can even use GlideRecords as XML if you want


{


    var xml = new GlideRecordXMLSerializer();


    updated_records.push(xml.serialize(gr_data));


}


// now store to user record


var gr_user = new GlideRecord('sys_user');


gr_user.get('user_name', '<user_name>');


var ga = new GlideSysAttachment();


ga.write(gr_user, 'myfile.json', 'text', JSON(updated_records));


When I try this, I get the error on the ga.write line of:

    'JSON' is not a function

When I changed the line to:

    ga.write(gr_user, 'myfile.json', 'text', JSON.stringify(updated_records));

it worked.

Thanks for the tip! I've been looking for a workaround for a year!