Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Flow designer inline script to update an integer field with the sum of two other choice fields

RitaS
Tera Guru

Hi, I really want to use a flow to set a field value.  I could probably get this working easily as a business rule but at this point it's the principal of the thing.  I have 2 choice fields with numeric values that, when updated, should be summed together and that result used to update an integer field.  In the flow record I've plugged the script into an update record action using the following settings:

RitaS_0-1775753170733.png

and my script content below:

(function execute(inputs, outputs) {

var gr = inputs.current;
// Get value of input fields and convert to integers
var benefit_value = gr.getValue('u_benefit');
var effort_value = gr.getValue('u_effort');
var benefit_number = parseInt(benefit_value, 10) || 0;
var effort_number = parseInt(effort_value, 10) || 0;
// Add values together
var score_value  = benefit_number + effort_number;
// Set custom score and update record
gr.setValue('u_custom_score', score_value);
gr.update();

})(inputs, outputs);
 
When I try this, nothing happens, no error in the logs.  Any ideas on how to get this to work without admitting defeat and going back to a BR?
1 ACCEPTED SOLUTION

Brad Bowman
Mega Patron

The script field on actions has sample text to guide you in the expected/accepted values that you can use in the script, and how to update the field value, so you can't just make up a different convention like 'inputs' and 'outputs', with random GlideRecord methods like getValue, setValue and update.

 

Using 3 out of box fields on the demand table - 2 choice fields with numeric values: priority + state, and an integer field: order, for example a successful script on an Update Record action would look like this:

/*
**Access Flow/Action data using the fd_data object. Script must return a value. 
**Order number is offset by +1 in Error Handling Section.
**Available options display upon pressing "." after fd_data
**example: var shortDesc = fd_data.trigger.current.short_description;
**return shortDesc;
*/
// Get value of input fields and convert to integers
var benefit_value = fd_data.trigger.current.priority;
var effort_value = fd_data.trigger.current.state; 
var benefit_number = parseInt(benefit_value, 10) || 0;
var effort_number = parseInt(effort_value, 10) || 0;
// Add values together
var score_value  = benefit_number + effort_number;
// Set custom score and update record
return score_value;

BradBowman_0-1775768833064.png

 

 

View solution in original post

4 REPLIES 4

Brad Bowman
Mega Patron

The script field on actions has sample text to guide you in the expected/accepted values that you can use in the script, and how to update the field value, so you can't just make up a different convention like 'inputs' and 'outputs', with random GlideRecord methods like getValue, setValue and update.

 

Using 3 out of box fields on the demand table - 2 choice fields with numeric values: priority + state, and an integer field: order, for example a successful script on an Update Record action would look like this:

/*
**Access Flow/Action data using the fd_data object. Script must return a value. 
**Order number is offset by +1 in Error Handling Section.
**Available options display upon pressing "." after fd_data
**example: var shortDesc = fd_data.trigger.current.short_description;
**return shortDesc;
*/
// Get value of input fields and convert to integers
var benefit_value = fd_data.trigger.current.priority;
var effort_value = fd_data.trigger.current.state; 
var benefit_number = parseInt(benefit_value, 10) || 0;
var effort_number = parseInt(effort_value, 10) || 0;
// Add values together
var score_value  = benefit_number + effort_number;
// Set custom score and update record
return score_value;

BradBowman_0-1775768833064.png

 

 

Thanks so much, this works perfectly.  I started out with something similar but I had combined the 2 step input fields declaration into one command line.  Then I let ChatGPT lead me astray. I should have stuck with what the sample text showed. Thanks again for the help! 

AhsanM
Tera Expert

Hi Rita,

 

Good news, you are very close! The issue is that inside a Flow Designer Update Record action script, you should not call gr.update() yourself. The Flow Designer handles the record update automatically after your script runs. Calling gr.update() inside the script can actually cause the changes to be lost or conflict with the flow engine.
Also the way inputs.current works in Flow Designer is slightly different from a Business Rule. The inputs.current object is a read only representation of the trigger record. To set field values in an Update Record action you need to use the outputs object instead.

 

Try this approach:

 

(function execute(inputs, outputs) {

var gr = inputs.current;

var benefit_value = gr.getValue('u_benefit');
var effort_value = gr.getValue('u_effort');

var benefit_number = parseInt(benefit_value, 10) || 0;
var effort_number = parseInt(effort_value, 10) || 0;

var score_value = benefit_number + effort_number;

// Set the output value, do NOT call gr.update()
outputs.u_custom_score = score_value;

})(inputs, outputs);

 

Then in the Fields section of your Update Record action, map the u_custom_score field to the outputs.u_custom_score variable from your script.

 

If that approach does not work in your version, an alternative is to use a Script step instead of the Update Record action script, and do a fresh GlideRecord lookup using the sys_id from inputs.current:

 

(function execute(inputs, outputs) {

var sysId = inputs.current.sys_id.toString();

var benefit_number = parseInt(inputs.current.getValue('u_benefit'), 10) || 0;
var effort_number = parseInt(inputs.current.getValue('u_effort'), 10) || 0;

var score_value = benefit_number + effort_number;

var gr = new GlideRecord('dmn_demand');
if (gr.get(sysId)) {
gr.setValue('u_custom_score', score_value);
gr.setWorkflow(false);
gr.update();
}

})(inputs, outputs);

 

No defeat necessary, Flow Designer can absolutely handle this! 😄

Ahsan
ServiceNow Developer & Admin
Builder of NowFixer | Free AI debugging tool for ServiceNow scripts

Ankur Bawiskar
Tera Patron

@RitaS 

Script from @Brad Bowman  should work

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader