Certainly! Here is a Client Script for ServiceNow that meets your requirements. This script will check if the "short_description" field contains the string 'device setup', and if it does, it will set another field with this value.
Here's a step-by-step guide to achieve this:
- Navigate to the ServiceNow instance.
- Go to
System Definition > Client Scripts.
- Click on the
New button to create a new Client Script.
- Fill in the fields as necessary:
Name: Check Short Description for Device Setup
Table: <choose your relevant table, e.g., Incident>
Type: onChange or onSubmit depending on when you want the check to take place (usually onChange for immediate checks).
Field: short_description (if you're using onChange script).
- Add the following code to the
Script section:
(function executeRule(current, previous /*null when async*/) {
// Only proceed if the short_description field is populated
if (current.short_description) {
var shortDesc = current.short_description.toString().toLowerCase();
// Check if it contains 'device setup'
if (shortDesc.indexOf('device setup') !== -1) {
// Perform action: set another field to the value found in short_description
// Change 'u_custom_field' to the target field you want to set
current.u_custom_field = current.short_description;
}
}
})(current, previous);
- Save and activate the Client Script.
Here's a breakdown of the script:
- It checks if
short_description contains any value.
- It converts the
short_description to lowercase to make the check case-insensitive.
- It checks if the
short_description contains the string 'device setup'.
- If it does, it sets the value of another custom field (
u_custom_field) to the short_description.
If you are using an onChange Client Script, you might have to tweak the script slightly to use g_form instead of current, and you wouldn't pass previous like this. Instead, you would work directly with the form fields.
Here's an example for an onChange Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Convert the new value to lowercase
var shortDesc = newValue.toLowerCase();
// Check if it contains 'device setup'
if (shortDesc.indexOf('device setup') !== -1) {
// Set another field to the value found in short_description
// Change 'u_custom_field' to the target field you want to set
g_form.setValue('u_custom_field', newValue);
}
}
Replace u_custom_field with the actual field name you intend to populate.
Remember to test the client script to ensure it meets your exact requirements in your development or test instance before deploying it to production.