Converting String to Currency in a Playbook
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
52m ago
Getting a string into a currency field from a Playbook (when there’s no Run Script activity)
I hit this while updating a Playbook in ServiceNow, and it took me longer to sort out than I’d like to admit. So I’m writing it down, mostly so I don’t have to figure it out again.
The setup was pretty simple. A Record Producer asks the user for a dollar amount. Later on, an Update Record activity in the Playbook needs to write that amount into a currency field on the parent record.
The first snag was the variable itself. Decimal and Numeric aren’t options for Record Producer variables, and the Currency type didn’t render properly for me in Workspace. I ended up using a single line text variable with a client script to validate what the user types.
I did think about just stripping out anything that wasn’t a digit or a decimal point and moving on, but I decided against it. If someone types something strange and the system quietly “fixes” it, you can end up saving a number they never meant to enter. I’d rather reject it and tell them why.
The second snag was the bigger one. Now I had a String data pill and a currency field, and Update Record wouldn’t map one to the other. In Flow Designer I’d just add a script step and convert it. Playbooks don’t have that. There’s no Run Script activity in the palette.
What does work is building a small custom Flow Action and exposing it to the Playbook through an Activity Definition. I’d done this once before and couldn’t remember all the pieces, so here they are.
Build the Flow Action
In Flow Designer, create a new Action with one String input and one float output. Then add a Script step:
javascript
(function execute(inputs, outputs) {
try {
var str = inputs['stringToParse'].toString();
var flt = parseFloat(str) || 0;
outputs.floattoreturn = flt;
} catch (e) {
gs.error('Parse Float Action: failed to parse string to float. Input was: ' + inputs['stringToParse']);
}
})(inputs, outputs);
The toString() probably isn’t needed since the input is already a String, but it doesn’t hurt anything. The part to watch is the || 0. If parseFloat gets something like “abc” it returns NaN, and that turns into 0, so a bad value lands in your currency field as zero instead of throwing an error. My client script validation catches that before it gets here. If yours doesn’t, you might want to throw instead.
Publish the Action
Create the Activity Definition
Go to Process Automation > Process Automation Administration > Activity Definitions [sys_pd_activity_definition] and create a new record. Set the Automation Plan to the Action you just published. You can leave the layout empty. The Action still runs. It just won’t show anything in the running Playbook, which is what you want for a background conversion like this. This step makes it selectable inside of flow designer.
Add it to the Playbook
In Workflow Studio, add the new activity to the stage right before your Update Record activity, or wherever you are needing the currency. Then in Update Record, map the Action’s output to the currency field instead of the original variable.
That’s all of it: two new records and one change to the Playbook. The activity is also reusable. The next time something in a Playbook needs a script step, you can build it the same way.
The Workflow Automation CoE has a community article on custom Playbook activities that covers Activity Definitions in more detail if you want to read further:
https://www.servicenow.com/community/workflow-automation-articles/custom-playbook-activities-workflo...
If you’ve found a cleaner way around this, let me know in the comments.