We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

how to code it to add a variable 'plan' as 'Work Notes'

SonnyY
Tera Contributor

The command from the code below: addPlanToIncident () is to add a variable 'plan' to 'Additional Comments'. Does any one know how to code it to add a variable 'plan' as 'Work Notes'?
sn_itsm_aia.InvestigatingAndResolvingIncidentsUtil().addPlanToIncident(givenIncidentNumber, plan);

 

 

This code below is used to create a variable 'plan' and add it to the 'Additional Comments'.

 

(function(inputs) {
    var plan = inputs['plan_to_add_array'];
var givenIncidentNumber = inputs['given_incident_number'];

    if(typeof plan == "string") {
try{
plan = JSON.parse(plan);
}catch(ex){
gs.error("Error while parsing plan to add as comments: {0}", ex);
return new sn_itsm_aia.InvestigatingAndResolvingIncidentsUtil().addPlanToIncident(givenIncidentNumber, plan);
}
    }
    plan = plan.map(function(step, index) {
        return step;
    }).join('\n');
    
    return new sn_itsm_aia.InvestigatingAndResolvingIncidentsUtil().addPlanToIncident(givenIncidentNumber, plan);
})(inputs);

3 REPLIES 3

sivasankaris
Tera Guru

Hi @SonnyY ,

In ServiceNow, the method addPlanToIncident() is a pre-built function within the InvestigatingAndResolvingIncidentsUtil Script Include. By default, this specific method is hardcoded to target the comments field (Additional Comments).

To switch this to work_notes, you generally have two options: check if a similar "Work Notes" method exists in that utility, or—more reliably—use the standard GlideRecord API to update the record directly.

 

Option 1: Using the Standard GlideRecord Approach

Since the out-of-the-box utility method is designed for comments, the most direct way to ensure the data goes to Work Notes is to write a small update script.

Replace your final return line with this logic:

 
(function(inputs) {
    var plan = inputs['plan_to_add_array'];
    var givenIncidentNumber = inputs['given_incident_number'];

    if (typeof plan == "string") {
        try {
            plan = JSON.parse(plan);
        } catch (ex) {
            gs.error("Error while parsing plan: {0}", ex);
        }
    }

    // Format the array into a newline-separated string
    var planString = (Array.isArray(plan)) ? plan.join('\n') : plan;

    // Direct Update to Work Notes
    var grIncident = new GlideRecord('incident');
    if (grIncident.get('number', givenIncidentNumber)) {
        grIncident.work_notes = planString; 
        grIncident.update();
        return true; // Or return a success message
    }
    
    return false;
})(inputs);

If this works, please mark it as helpful and please Accept my Solution..

Best Regards,

SIVASANKARI S

Hi, SIVASANKARI, 

Many thanks for your swift response. 

I've followed your instruction and it is still added into the 'Additional Comments'. Please see the screen shot.  It would be easier if there's a command in the Util to add it straight to Work Notes. Any recommendation for the Syntax library?

 

Kind regards, 

Sonny

SonnyY
Tera Contributor

Warning: Alert level: Warning.
Script uses GlideRecord. If directly processing input or returning information to the caller, use GlideRecordSecure instead.

 

I tried a few times. It's not working for me. When I used your code in the Script pop out window, it returned with a warning as above. Then I saved it and run. It seems fine, the test runs perfect.

However, the plan was added to the 'Additional Comments', not Work Note. When I return to the Script, I found out your code was not updated.