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

Great to hear after all of this, but a head scratcher considering the parentCase GR works with that method/syntax. But, sometimes when something doesn't make sense, you have to try something that doesn't make sense...

Sandeep Rajput
Tera Patron
Tera Patron

@JordyZ Script looks okay to me, ideally the worknote should be added. As suggested by Brad, please check if you can find the same work note in sys_journal_field table.

Hi @Sandeep Rajput , thanks for taking a look at my new thread. I've checked the sys_journal_field but did not find any of the work notes. Any other ideas? Thanks in advance.