Script not adding work note to incident

JordyZ
Mega Sage

Hi,

 

I have a workspace UI action that creates an unsaved incident record from a case. Through a business rule, after manually saving the incident, the case should get populated with a work note saying "incident has been associated with the case", this part works.

 

The part that doesn't work is that the incident should also get a work note added, stating the parent case number, account/consumer name (depending on which was populated in the case), account/consumer ID (depending on whether account or consumer was selected). 

 

This is the business rule (after insert):

// Check if the incident has a parent record and if the parent is a case
if (!gs.nil(current.parent)) {
    gs.info("Parent record found for the incident: " + current.parent); // Log the parent record
    var parentCase = new GlideRecord("sn_bom_financial_service");

    if (parentCase.get(current.parent)) {
        gs.info("Parent case found: " + parentCase.getValue("number")); // Log case number

        var incidentSysId = current.getValue("sys_id");
        var incidentNumber = current.getValue("number");

        // Update the case record with the incident sys_id
        parentCase.setValue("incident", incidentSysId);
        parentCase.update(); // Save the case record
        gs.info("Incident " + incidentNumber + " linked to the case: " + parentCase.getValue("number")); // Log case linkage

        // Add a work note to the case indicating that the incident is associated
        var caseWorkNote = "Incident " + incidentNumber + " has been associated with the Case";
        parentCase.setValue('work_notes', caseWorkNote);
        parentCase.update(); // Save the case to add the work note

        // Now, post a work note on the incident with client/business account details
        var clientOrBusinessInfo = '';
        var caseNumber = parentCase.getValue('number');
        var incidentWorkNote = '';

        // Check if the client (consumer) or account (business account) field is filled
        var consumerSysId = parentCase.getValue('consumer'); // Get the sys_id of the consumer (csm_consumer reference)
        var accountSysId = parentCase.getValue('account'); // Get the sys_id of the account (customer_account reference)

        // Fetch the Customer ID from the csm_consumer table if consumer is filled
        if (!gs.nil(consumerSysId)) {
            var consumerRecord = new GlideRecord('csm_consumer');
            if (consumerRecord.get(consumerSysId)) {
                var customerId = consumerRecord.getValue('u_customer_id'); // Fetch the Customer ID from csm_consumer
                clientOrBusinessInfo = consumerRecord.getDisplayValue('name') + ", Customer ID: " + customerId;

                // Construct the work note for Client
                incidentWorkNote = "Incident created from Case: " + caseNumber + "\n" +
                    "Client is: " + clientOrBusinessInfo;
                gs.info("Work note created for Client: " + incidentWorkNote); // Log the work note
            } else {
                gs.info("Failed to retrieve the consumer record with sys_id: " + consumerSysId); // Log failure
            }
        }
        // Fetch the Kldb ID from the customer_account table if account is filled
        else if (!gs.nil(accountSysId)) {
            var accountRecord = new GlideRecord('customer_account');
            if (accountRecord.get(accountSysId)) {
                var kldbId = accountRecord.getValue('u_kldb_id'); // Fetch the Kldb ID from customer_account
                clientOrBusinessInfo = accountRecord.getDisplayValue('name') + ", Kldb ID: " + kldbId;

                // Construct the work note for Business Account
                incidentWorkNote = "Incident created from Case: " + caseNumber + "\n" +
                    "Business account is: " + clientOrBusinessInfo;
                gs.info("Work note created for Business Account: " + incidentWorkNote); // Log the work note
            } else {
                gs.info("Failed to retrieve the account record with sys_id: " + accountSysId); // Log failure
            }
        } else {
            gs.info("Neither consumer nor account found on the case"); // Log empty fields
        }

        // Add the work note to the incident if it's not empty
        if (incidentWorkNote !== '') {
            var incidentRecord = new GlideRecord('incident');
            if (incidentRecord.get(incidentSysId)) {
                incidentRecord.setValue('work_notes', incidentWorkNote);
                incidentRecord.update(); // Save the incident to add the work note
                gs.info("Work note successfully added to incident: " + incidentNumber); // Log success
            }
        }
    }
}

Even the logs says the work note has been successfully added to the incident:

JordyZ_0-1727451891720.png

But the incident shows no added work note (I checked the filter too).

What could be the cause? 
Any help is appreciated!

1 ACCEPTED SOLUTION

This may be a Data Policy issue.  Before manually saving the incident, can you manually add a Work note?  How about after manually saving the incident?  We've confirmed what the note should be, that a record is returned by the incidentRecord GR, and I will assume you are looking at the same incident record for the work note.  Some other things to try, because stranger things have happened:

change 

                incidentRecord.setValue('work_notes', incidentWorkNote);

to

                incidentRecord.work_notes = incidentWorkNote;

then try a basic direct assignment like

 

                incidentRecord.work_notes = 'This better work';

 

Also add yet another log in this if block

gs.info('Script is running as ' + gs.getUserDisplayName());

 

View solution in original post

7 REPLIES 7

Brad Bowman
Kilo Patron
Kilo Patron

Good job with the log.  On quick glance, it looks like this is the last GR that's in question, and that's the log you are seeing.  Let me know if that's not right.  Add another log just before that to confirm that value on incidentWorkNote. Also change the existing log from incidentNumber to incidentRecord.number to confirm the actual incident returned by the GR.  Take a look at the sys_journal_field table (sorted by created descending) to see if the record is there, because journal fields are weird and can be tricky with what is seen in this table vs the Activity Filter on the record.

Hi @Brad Bowman  , thanks for your reply.
I've added an extra log, and it is showing up in the logs. I've also changed the incidentNumber to incidentRecord.number. Sys_journal_field did not show any of the work notes.

JordyZ_0-1727457917789.png

JordyZ_1-1727458098225.png

What else could it be? 

Thanks in advance!

Edit: could the work note not showing up, be somehow linked to this error I'm getting?

JordyZ_2-1727458261683.png

 

This may be a Data Policy issue.  Before manually saving the incident, can you manually add a Work note?  How about after manually saving the incident?  We've confirmed what the note should be, that a record is returned by the incidentRecord GR, and I will assume you are looking at the same incident record for the work note.  Some other things to try, because stranger things have happened:

change 

                incidentRecord.setValue('work_notes', incidentWorkNote);

to

                incidentRecord.work_notes = incidentWorkNote;

then try a basic direct assignment like

 

                incidentRecord.work_notes = 'This better work';

 

Also add yet another log in this if block

gs.info('Script is running as ' + gs.getUserDisplayName());

 

@Brad Bowman  , thank you so much!! 

The change to incidentRecord.work_notes = incidentWorkNote; fixed it!

JordyZ_0-1727473241507.png