The CreatorCon Call for Content is officially open! Get started here.

Dynamically Populate a Field Based-on the Value(s) of Another Field

appstorm
Tera Contributor

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.

find_real_file.png

find_real_file.png

Below, is my script include.

find_real_file.png

 

How do I go about writing an on-change CS to make this work?

Thank you!

1 ACCEPTED SOLUTION

-O-
Kilo Patron

This is what I have defined in my PDI:

Hfind_real_file.pngHave added the fields to the Change Request form:

find_real_file.png

After flushing the cache, when I change System, Analyst and Owner automatically update.

View solution in original post

38 REPLIES 38

appstorm
Tera Contributor

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!

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 🙂

appstorm
Tera Contributor

Works like a champ on display!  However, when I submit the CR, I get the error: "Match Not Found, Reset to Original."

Mohit Kaushik
Mega Sage
Mega Sage

Hi @appstorm ,

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

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)