Add additional comments via script on requested items by disabling notifications

Community Alums
Not applicable

I want to add additional comment on requested items in bulk by disabling comments.

I am trying using scheduled job as below script but additional comments are not coming

var sc_req_item = new GlideRecord('sc_req_item');

sc_task_gr.addEncodedQuery("request.requested_for=javascript:gs.getUserID()^active=true^number=RITM0123312");// number of RITM on which comment shoulf go i.e. query

sc_req_item_gr.query();

while (sc_req_item_gr.next()) {
gs.log('Auto-reassign Scheduler : auto-reassigning the task record as part of request number- for '+sc_req_item_gr.getValue('req number'));

sc_req_item_gr.setValue('Additional comments','This ticket was closed unintentionally as part of a clean-up activity. As part of the correction the ticket has been re-opened at the same state, we apologize in advance for any inconvenience caused');//additional comment on RITMs
sc_req_item_gr.update();
}

 

Please guide how I can achieve this

23 REPLIES 23

Community Alums
Not applicable

Okay Thanks alot for the update and the links shared.

 

So I cannot achieve this using scheduled job correct?

No, you can achieve it no problem.

Please hit like and mark my response as correct if that helps
Regards,
Musab

Community Alums
Not applicable

How can I achieve it?

Notification which is triggering try to add this in 'Advance condition' and then run job to see to see if comment is getting updated.

var comment = journalDissection(current.comments.getJournalEntry(1));

if(comment.startsWith('This ticket was closed unintentionally as part of a clean-up activity')){
	answer = false;
}
else{
	answer = true;
}

function journalDissection(journal){
	var index = journal.indexOf('\n');
	return journal.substring(index,journal.length).trim();
}
Please hit like and mark my response as correct if that helps
Regards,
Musab

Akif_Shah
Mega Sage

Try this

var requested_item = new GlideRecord('sc_req_item');
requested_item.addEncodedQuery("request.requested_for=javascript:gs.getUserID()^active=true^number=RITM0123312");
requested_item.query();

while (requested_item.next()) {
    var id = requested_item.request; // store the sys_id of REQ which is related to RITM
gs.log('Auto-reassign Scheduler : auto-reassigning the task record as part of request number- for '+sc_req_item_gr.getValue('req number'));
    setComment(requested_item.sys_id, "This ticket was closed unintentionally as part of a clean-up activity. As part of the correction the ticket has been re-opened at the same state, we apologize in advance for any inconvenience caused");
    requested_item.setWorkflow(false);
    requested_item.update();
}

function setComment(ritm, comm){
    // Insert the text into the Journal table
    var grJournal = new GlideRecord('sys_journal_field');
    grJournal.initialize();
    grJournal.name = "sc_req_item";
    grJournal.element_id = ritm;
    grJournal.element = "comments";
    grJournal.value = comm;
    grJournal.insert();

    // Get the history set for the current record
    var grHistSet = new GlideRecord('sys_history_set');
    grHistSet.get('id', ritm);

    // Insert History line record
    var grHistLine = new GlideRecord(grHistSet.line_table);
    grHistLine.initialize();
    grHistLine.field = "comments";
    grHistLine.label = "Additional comments";
    grHistLine.setValue('new',comm);
    grHistLine.user_name = gs.getUserDisplayName();
    grHistLine.user_id = gs.getUserName();
    grHistLine.user = gs.getUserID();
    grHistLine.update_time = new GlideDateTime();
    grHistLine.set = grHistSet.getUniqueValue();
    grHistLine.insert();
}