The CreatorCon Call for Content is officially open! Get started here.

How to add comments to a record via background script

Laurie Marlowe1
Kilo Sage

Hello All,

I created a background script to cleanup some records and I want to add a comment to each record that is cleaned up.   I know this must be a syntax thingy - I'm fairly new to scripting.   The comment appears in the Activity log, however, it does not appear as a new entry.   It places the comment inside the previous comment in the log:

Capture.PNG

Here is my code:

processCleanupTickets();

function processCleanupTickets() {

var gr = new GlideRecord('sc_req_item');

gr.orderBy('sys_created_on');

gr.setLimit(1);

gr.addQuery('opened_at', '<', '2015-06-01');

gr.query();

while (gr.next()) {

   

    gr.setValue('u_requester',gr.variables.Requestor);

    gr.setValue('u_deliver_to',gr.variables.requested_for);

  gr.comments = 'Requested For and Deliver To fields populated from variables so VIP and Legal Hold icons will show on the fields.';

    gr.setWorkflow(false);

gr.update();

gs.print('RITM ' + gr.number + ' Opened ' + gr.opened_at + ' Requested For ' + gr.u_requester);

}

}

Any help is greatly appreciated!

Thanks,

Laurie

3 REPLIES 3

saritha9
Giga Expert

Hello Laurie,



As comments field is of type journal input it will not show that in the field once updated. You can see it in Activity log. If it is a journal type then the comments will be shown below the field.



Using Journal Fields - ServiceNow Wiki


Thank you, Saritha.   I knew it would be visible in the Activity log.   It was just inserting the comment into a previous log note.



I figured out I needed to remove the   gr.setWorkflow(false) and it would create a new log entry with the correct date/time stamp.


Yes. setWorkflow(false) disables running of any business rules.