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

@Sirri 

I hope you are doing well!

Did it work ? Was my reply helpful?


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

@Medi C ,

 

Thank you for your response,  That's worked for me. I have added the below line to that code:

var removeassistant = g_form.getValue('assistant_name_to_add')
 

I hope your clear understanding on my requirement.

I need more scenario I'm using the client script currently on change of assistant_name_to_add but in case I need to use the same type of check in case of onchange of name_of_the_people_supported in variable set of variable.

 

Please let me know if you have any queries

 

Thank you

 

@Sirri 
I am glad this worked for you.

You can use the same logic as a catalog client scripts in your variable set.

 

could you please try that?


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

@Medi C @Pavankumar_1 

 

Hi Both Thank you for your response.

 

I'm trying to achieve this on submit client script using this script but we have another options in action as Remove Assistant & Remove Delegate Access non assistant if we select these option in action when we submit we are getting the this error('Unexpected end of JSON input' ) in JavaScript  console. please let know the reason why we are getting this error.

 

var peopleSupported = JSON.parse(g_form.getValue("IO:7b2ca032db36d01074e716f35b9619b3"));
     var removeassistant = g_form.getValue('assistant_name_to_add');
     var action = g_form.getValue('action');
     var previousassistant = g_form.getValue('previous_assistant_name');
     var NewAssitstant = g_form.getValue('new_assistant_name');
     //alert(action);

     for (var i = 0; i < peopleSupported.length; i++) {
         var name = peopleSupported[i].names_of_the_people_supported;
         // Compare variable name to other values
         if ((action == 'Add Assitant' || action == 'Add Delegate Access (non-assistant)') && (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');
             alert('Assistant or Delegate Name to Add and Names of the people supported cannot be the same.');
             return false; // Prevent form submission
         } else if (action == 'Change Assistant' && previousassistant == NewAssitstant) {
             alert('Previous assitant/New Assitant/People supported cannot be same');
             return false;
         } else if (action == 'Change Assistant' && previousassistant == name) {
             alert('Previous assitant/New Assitant/People supported cannot be same');
             return false;
         } else if (action == 'Change Assistant' && NewAssitstant == name) {
             alert('Previous assitant/New Assitant/People supported cannot be same');
             return false;
         }

     }
     return true;