
In ServiceNow, I start by selecting All > Studio and then selected my app. Near the bottom of my Catalog Item app I select the Catalog Client Scripts tab and select New.

I want to end a flow if the user enters a change ticket but the state of the change ticket is not Implement. I create two Single Line Text variables, change_ticket and change_ticket_state.

I remove all of the visible checkmarks from change_ticket_state since this just needs to be a hidden input.

And this is my client script that will display an alert if a change ticket was entered but the state of the change ticket is not Implement and this will also set value of the change_ticket_state input so that the value can be used in the Flow.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Get the change ticket number entered in the change_ticket form field
var change_ticket = g_form.getValue('change_ticket');
change_ticket = change_ticket.trim();
// Alert if the format of the change ticket is not valid
if (!/CHG[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/.test(change_ticket)) {
alert(change_ticket+" does NOT appear to be a valid change ticket number");
return;
}
// Lookup the change
var gr = new GlideRecord('change_request');
gr.addQuery('number', '=', change_ticket);
gr.query(getResponse);
}
function getResponse(result) {
if (result.getRowCount() == 0) {
alert("Sorry - Got 0 results with the change ticket number you provided. Is this a valid change ticket number?")
} else {
while(result.next()) {
g_form.setValue('change_ticket_state', result.getDisplayValue('state'), result.getDisplayValue('state'));
if (result.getDisplayValue('state') != "Implement") {
alert("It appears that the state of your change ticket "+result.getDisplayValue('number')+" is "+result.getDisplayValue('state')+ " - You will need to update your change ticket to Implement");
}
}
}
}
The Flow has an if statement if the change_ticket is not empty and change_ticket_state is not Implement.

In this scenario the requested item record (RITM) is updated with a comment stating that the state of the change ticket is not implement and the flow is ended.

Did you find this article helpful?
If so, consider buying me a coffee over at