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 REPLY 1

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