Close Case Script - Add Work Notes

Jason116
Tera Contributor

I am writing a background script to close out a large number of cases that have been stuck in the Resolved state. The script itself is fairly straightforward — it simply closes the cases and updates a few fields such as Account, Contact, Assigned To, and Category.

However, I’ve encountered an issue: I need to silence notifications as part of the process, but I also need to add a work note to each case. It seems that by suppressing notifications, journal field updates (like work notes) are also being suppressed, and as a result, the work notes are not being added. Everything else in the script is functioning correctly — it’s just the work notes that aren’t showing up

 

var caseNumbers = [
    'CS0xxxxx',
    'CS0xxxxx',
    'CS01xxxx'
];

// Set field values
var accountSysId = '';
var contactSysId = '';
var assignedToSysId = '6';
var categoryValue = '39';

var workNoteText =
"Add work note here........)";

for (var i = 0; i < caseNumbers.length; i++) {
    var caseNumber = caseNumbers[i];

    var gr = new GlideRecord('sn_customerservice_case');
    gr.addQuery('number', caseNumber);
    gr.query();

    if (gr.next()) {
        gr.setWorkflow(false);
        gr.autoSysFields(false);

        gr.setValue('state', '3');
        gr.setValue('account', accountSysId);
        gr.setValue('contact', contactSysId);
        gr.setValue('assigned_to', assignedToSysId);
        gr.setValue('category', categoryValue);

        gr.update();

        gr.addWorkNote(workNoteText);
        gr.update();

        gs.print('Case ' + caseNumber + ' updated successfully.');
    } else {
        gs.print('Case ' + caseNumber + ' not found.');

 

7 REPLIES 7

Chaitanya ILCR
Kilo Patron

Hi @Jason116 ,

try this script

var caseNumbers = [
    'CS0xxxxx',
    'CS0xxxxx',
    'CS01xxxx'
];

// Set field values
var accountSysId = '';
var contactSysId = '';
var assignedToSysId = '6';
var categoryValue = '39';

var workNoteText =
    "Add work note here........)";

for (var i = 0; i < caseNumbers.length; i++) {
    var caseNumber = caseNumbers[i];

    var gr = new GlideRecord('sn_customerservice_case');
    // gr.addQuery('number', caseNumber);
    // gr.query();

    if (gr.get('number',caseNumber)) {
        gr.setWorkflow(false);
        gr.autoSysFields(false);

        gr.setValue('state', '3');
        gr.setValue('account', accountSysId);
        gr.setValue('contact', contactSysId);
        gr.setValue('assigned_to', assignedToSysId);
        gr.setValue('category', categoryValue);
        gr.work_notes = workNoteText;
        gr.update();

        // gr.addWorkNote(workNoteText); there is no addWorkNote method
        // gr.update();

        gs.print('Case ' + caseNumber + ' updated successfully.');
    } else {
        gs.print('Case ' + caseNumber + ' not found.');
    }
}

there is no method as addWorkNote in the gliderecord

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

Hi @Jason116 ,

you can use No code data management job for this

 

ChaitanyaILCR_0-1748481120350.png

create it against your table with condition number is one of your cases

 

and use field & values to set values

 

Auto updating system fields uncheck this (this is equivalent to autoSysFields(false) doesn't update the system fields)

Run business rules and engines uncheck this (this is equivalent to the setWorkflow(false) doesn't trigger notifications workflow engines etc)

 

once done click on continue UI action 

 

once the record is saved click on the execute now UI action

ChaitanyaILCR_0-1748481662531.png

 

easy to setup and use

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

No did not work, still no work note added

Hi @Jason116 ,

 

the worknote will be added with setWorkflow(false) and autoSysFields(false) it's just that it's not visible on the form because this not made to sy_history_set

 

someone who has never opened the form might see the work note when opened the record

 

just delete the sys_history_set entry of the case record for immediate effect and reload the form

 

use below script

var caseNumbers = [
    'CS0xxxxx',
    'CS0xxxxx',
    'CS01xxxx'
];

// Set field values
var accountSysId = '';
var contactSysId = '';
var assignedToSysId = '6';
var categoryValue = '39';

var workNoteText = "Add work note here........)";

for (var i = 0; i < caseNumbers.length; i++) {
    var caseNumber = caseNumbers[i];

    var gr = new GlideRecord('sn_customerservice_case');

    if (gr.get('number', caseNumber)) {

        gr.setValue('state', '3');
        gr.setValue('account', accountSysId);
        gr.setValue('contact', contactSysId);
        gr.setValue('assigned_to', assignedToSysId);
        gr.setValue('category', categoryValue);
        gr.work_notes = workNoteText;
        gr.setWorkflow(false);
        gr.autoSysFields(false);
        gr.update();

        ///these lines delete the existing history set of current record so that the changes will be seen the form
        var list = new GlideRecord("sys_history_set");
        list.addEncodedQuery("id=" + gr.getUniqueValue() + "^table=sn_customerservice_case");
        list.setLimit(10);
        list.query();
        list.deleteMultiple();


        gs.info('Case ' + caseNumber + ' updated successfully.');
    } else {
        gs.info('Case ' + caseNumber + ' not found.');
    }
}

 

@Roshnee Dash , @Robert H your thoughts on this 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya