- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2021 10:24 AM
I have been working on several on-change client scripts to make this work, but have so far been unsuccessful. The task to make two fields that share the same table populate with their respective values, based-on the value of another field from the same table.
Below, is my script include.
How do I go about writing an on-change CS to make this work?
Thank you!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2021 03:41 PM
This is what I have defined in my PDI:
HHave added the fields to the Change Request form:
After flushing the cache, when I change System, Analyst and Owner automatically update.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2021 01:59 PM
Thanks again for help! I tried utilizing your advice, above and made the following changes.
- Added the code, below to Line 3 of the SI:
var GetSysName = Class.create();
- Changed "getName" to "getOwner"
- Verified the fields names were correct
- Verified the System drop-down on the CR form was referencing the cmdb_application_product_model table, which it is
The only other thing worth mentioning is the Owner and Analyst fields on the CR form are strings, although I'm not sure that matters.
Thanks again!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2021 02:09 PM
var GetSysName = Class.create();
Should be the very 1st line of the Script Include (unless lines 1 and 2 are empty, of course).
The Owner and Analyst fields being string fields does matter, cause in that case you need to only use the display values:
g_form.setValue('u_system_owner', answer.owner.displayValue);
g_form.setValue('u_analyst', answer.analyst.displayValue);
Of course that also means that you could drop the line setting the unique values in the script inlude; no:
'uniqueValue': '' + rec.owner,
and no
'uniqueValue': '' + rec.u_analyst,
And you're welcome 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2021 10:55 AM
Works like a champ on display! However, when I submit the CR, I get the error: "Match Not Found, Reset to Original."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2021 03:13 AM
Hi
I have seen one issue with your script include. Variable 'gn' is a string and it is overridden when you are setting the analyst.
You can modify your script like this.
// modify line number 4 as below
var gn = []; // make it array type instead of string.
// After this you need set values in gn in below format.
gn.push(rec.u_system_owner);
gn.push(rec.u_analyst);
In your client side you can get the values from answer variable in below format.
// before setting values to your custom fields first give alert for answer to check if you are getting comma seprated sys ids.
alert('answer is '+answer);
g_form.setValue('u_system_analyst',answer[0]);
g_form.setValue('u_analyst',answer[1]);
Do let me know if this helps you.
Note: Please mark this answer as correct and helpful if it resolves the query and helpful alone if it lead you in right direction.
Thanks,
Mohit Kaushik
Mohit Kaushik
ServiceNow MVP (2023-2025)