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.

How to compare catalog reference variables

chrish5
Giga Guru

I'm trying to compare two reference variables on a catalog item and if the second one matches the first one, alert the user of the duplication and clear the contents of the second variable.   First reference variable is new_site_owner and the second reference variable is new_site_owner2   Below is the catalog client script I am using woth Type: OnChange and Variable Name: new_site_owner2.       Is this the correct way to compare two reference variables?       Thanks

function onChange(control, oldValue, newValue, isLoading) {

  if (!isLoading || newValue == '') {

        return;

  }

  if(newValue == g_form.getValue('new_site_owner')) {

          alert("Owners cannot be the same");

          g_form.setValue('new_site_owner', '');

    }

    }

1 ACCEPTED SOLUTION

Hi Chris,



Use the () after the toString since it is a function. I hope this would solve your issue.



function onChange(control, oldValue, newValue, isLoading) {


              if (isLoading || newValue == '') {


      return;


  }


  if (g_form.getValue('new_site_owner2').toString() == g_form.getValue('new_site_owner').toString()) {


alert("The primary site owner and the secondary site owner cannot be the same person");


g_form.setValue('new_site_owner2', '');


  }


  }



You can also use the below condition if you writing onchange script on new_site_owner2 field.




if (newValue == g_form.getValue('new_site_owner').toString()) {




Regards


Viswanath


View solution in original post

7 REPLIES 7

Hi Chris,



Use the () after the toString since it is a function. I hope this would solve your issue.



function onChange(control, oldValue, newValue, isLoading) {


              if (isLoading || newValue == '') {


      return;


  }


  if (g_form.getValue('new_site_owner2').toString() == g_form.getValue('new_site_owner').toString()) {


alert("The primary site owner and the secondary site owner cannot be the same person");


g_form.setValue('new_site_owner2', '');


  }


  }



You can also use the below condition if you writing onchange script on new_site_owner2 field.




if (newValue == g_form.getValue('new_site_owner').toString()) {




Regards


Viswanath


chrish5
Giga Guru

Pradeep. I am unable to access the demo site.   It's telling me System initializing, please check back later.   I was able to resolve this thru another community users suggestion.   Thank you for your time.


cms1
Tera Contributor

Thank you for providing this script. I was able to make this work to avoid having the same 2 people be selected for approvers. My question is how do I reverse the script? Meaning, I want an alert message to appear if two variables are not equal. I thought I could simply use the same script but replace (==) with (!=) but that did not work.