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.

Compare variable value and in a variable set variable value

Sirri
Tera Guru

Hi All,

 

Hi All,

 

I am currently working on a requirement that involves the following:

  1. A normal variable: assistant_name_to_add
  2. A variable from a variable set (people_supported_and_level_of_access), which contains a variable: names_of_the_people_supported

I am trying to compare these two variables and clear the values through an onChange script if they are the same. However, I am encountering an issue where the People Support variable is returning an empty value in the alert, even though it has a value.

Below is the code I am using:

 

var removeassistant = g_form.getValue('assistant_name_to_add');
    var peoplesupport = g_form.getValue('people_supported_and_level_of_access.names_of_the_people_supported');
    var action = g_form.getValue('action');

    alert("Remove Assistant: " + removeassistant);
    alert("People Support: " + peoplesupport);
    alert("Action: " + action);

    if (removeassistant == peoplesupport) {
        g_form.clearValue('assistant_name_to_add');
        g_form.showFieldMsg('assistant_name_to_add', 'Assistant or Delegate Name to Add and Names of the people supported cannot be the same.', 'error');
    }
 

Could you please review the code and let me know what I might be missing?

Your assistance in this matter would be greatly appreciated.

Thank you in advance for your help.

 
13 REPLIES 13

Medi C
Giga Sage
Giga Sage

@Sirri
Is it a MultiRow Variable Set?

Could you provide screenshot of the form?


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

@Medi C ,

 

It's multi row variable set. Please provide exact code.

 

Thank yo

Medi C
Giga Sage
Giga Sage

Hi @Sirri,

 

Please copy the sys_id of your variable set and try to access your variable set as follow:

g_form.getValue("IO:SYS_ID_OF_YOUR_VARIABLE_SET")

I have just tried it:

MediC_1-1741358572759.png

This would return a a sting in a JSON format:

MediC_0-1741358470250.png

Rest, would be to JSON.parse the value you got and extract the data you need from the variable set.


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

@Medi C ,

 

If my variable set sys id is 7b2ca032db36d01074e716f35b9619b3 how can I separate please can you provide script as per my requirement.

 

Thank you

@Sirri, Please try the following, use debugging methods and adjust it according to your requirements.

var peopleSupported = JSON.parse(g_form.getValue("IO:7b2ca032db36d01074e716f35b9619b3"));

for(var i=0; i<peopleSupported.length; i++){
   var name = peopleSupported[i].names_of_the_people_supported;
   //NOW YOU CAN COMPARE VARIABLE name to other values
   if (removeassistant == name) {
        g_form.clearValue('assistant_name_to_add');
        g_form.showFieldMsg('assistant_name_to_add', 'Assistant or Delegate Name to Add and Names of the people supported cannot be the same.', 'error');
    }
} 

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.