Populate short description dynamically based on selected variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2022 03:49 AM
Hey guys,
Before I started this little project I had no experience with Javascript and wanted to learn something, so take it into mind when providing explanations 🙂
I'm working on a record producer form for our local use for the business.
The form utilizes a couple of variables to select. I need to populate short description based on those selected variables.
Here's how it looks like:
User selects CT1/CT2/CT3 (any combination of those, the rest variables are not supposed to have this kind of automation) and it should populate short description and description with the selected content.
i.e User select CT1 & CT3 with "Server Occupied" issue, short description should be: "CT1 CT3 Server Occupied"
Now, my understanding of Javascript is very limited, but here's what I was able to produce so far server-side:
I would prefer this to be server-side as I don't have experience just yet running this client-side.
var issue = producer.is_the_issue_any_of_the_following.getGlideObject().getDisplayValue();
var ctArr = [ producer.CT1, producer.CT2, producer.CT3 ];
var CT1 = producer.CT1;
var CT2 = producer.CT2;
var CT3 = producer.CT3;
var shortDesc = [];
for (var CT in ctArr) {
if (producer.CT1 == 'true' || producer.CT2 == 'true' || producer.CT3 == 'true') {
shortDesc.push(ctArr[CT].toString());
}
}
current.short_description = shortDesc.toString()+ ' ' +issue;
This gives me this result:
Couple of issues here:
- This shows true/false instead of displaying getLabel(): I was able to previously use the following to see the display value: producer.CT3.getGlideObject().getQuestion().getLabel(); But this doesn't seem to work with the current iteration of the script and I can't seem to get it to work that way
- It displays all variables, even the false ones which is not the goal. Only Variables with value 'true' should be visible in short description.
I was hoping to do this the smart way instead of using a bunch of "if" statements as part of learning curve.
Please help as I spent unhealthy amount of googling around ;).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2022 07:50 AM
Hi Cezary,
Hope you are doing fine.
To populate the short description of a change request in ServiceNow dynamically based on selected variables, you can use a client script to update the short description field whenever the selected variables are changed.
Here is an example of how you can implement this using a client script:
-
First, create a client script that listens for changes to the selected variables. You can do this using the
g_form.addEventHandler()method in your client script. -
When the selected variables are changed, the client script will be triggered. In the script, use the
g_form.setValue()method to update the value of the short description field with the desired text, based on the values of the selected variables. -
Save the client script and attach it to the change request form in ServiceNow. This will ensure that the script is executed whenever a user updates the selected variables on the form.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Get the values of the selected variables
var variable1 = g_form.getValue('variable1');
var variable2 = g_form.getValue('variable2');
// Set the short description based on the values of the selected variables
var short_description = 'Change request for ' + variable1 + ' and ' + variable2;
g_form.setValue('short_description', short_description);
}
// Register the onChange() function as an event handler for changes to the selected variables
g_form.addEventHandler('variable1', 'change', onChange);
g_form.addEventHandler('variable2', 'change', onChange);
In this script, the onChange() function is called whenever the selected variables are changed. The function gets the values of the selected variables using the g_form.getValue() method, and then sets the short description field using the g_form.setValue() method. The onChange() function is registered as an event handler for changes to the selected variables using the g_form.addEventHandler() method.
Please mark this answer correct if you find it helpful.
Regards,
Amit Gujarathi
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2022 03:11 AM
Hey @Amit Gujarathi
Much appreciated for your reply and help, however, this doesn't seem to work:
Here's what I changed your script to:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Get the values of the selected variables
var CT1 = g_form.getValue('CT1');
var CT2 = g_form.getValue('CT2');
var CT3 = g_form.getValue('CT3');
// Set the short description based on the values of the selected variables
var short_description = CT1 + ' - ' + CT2 + ' - ' +CT3;
g_form.setValue('short_description', short_description);
}
// Register the onChange() function as an event handler for changes to the selected variables
g_form.addEventHandler('CT1', 'change', onChange);
g_form.addEventHandler('CT2', 'change', onChange);
g_form.addEventHandler('CT3', 'change', onChange);
The reason for that, I think is that, in original form the "Short description" should by default not be visible:
So would onChange be applicable here? It's either that, or I'm doing something wrong with the script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 03:58 AM
Why don't you just a on Before, Insert business rule to update the short description.
Just use:
