Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Copy the value of a variable to a read only variable on the same catalog item

Cat
Mega Guru

Hi All,

 

I have a catalog item where there are 2 variables - article_name and article_name2.

article_name2 is a read only variable (single line text)

article_name is a single line text variable.

When the users fills in the article_name variable, I want the system to automatically populate article_name2 with the same information.

 

I have tried the following as a catalog client script, but it doesn't populate the field:

function onSubmit() { 
    var articleName = g_form.getValue('article_name');
    
    if (articleName) {
        // Set the value of the variable
        g_form.setValue('article_name2', articleName);
    }
    return true;
}

Is anyone able to help me achieve this?

 

Thank you

1 ACCEPTED SOLUTION

OlaN
Tera Sage
Tera Sage

Hi,

I would start by changing the logic to happen as an onChange Client script (have it interact on change of the first variable article_name).
Then the user can see the results before the form is submitted.

and in the script you would only need to do the setValue-part, and use the newValue input as basis, like this:

 

g_form.setValue('article_name2', newValue);

 

View solution in original post

5 REPLIES 5

OlaN
Tera Sage
Tera Sage

Hi,

I would start by changing the logic to happen as an onChange Client script (have it interact on change of the first variable article_name).
Then the user can see the results before the form is submitted.

and in the script you would only need to do the setValue-part, and use the newValue input as basis, like this:

 

g_form.setValue('article_name2', newValue);

 

Thank you, but unfortunately, that didn't work. I updated in to an onChange script and entered as below

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   //Type appropriate comment here, and begin script below
   g_form.setValue('article_name2', newValue);
}

Is this script correct?

The script looks correct, assuming your variable name actually is "article_name2".

 

Did you also set the Variable name option on the client script?
It should trigger onchange of your first variable, the one you are copying text from.

onchange-client-script.png

Well, I was silly - I had it all correct but I had left the UI Type to the default of Desktop - change it to All and we were cooking!

Thank you for your help!