Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

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

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Chris Hetrick



Can you try with the below script


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


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


        return;


  }


  if(g_form.getDisplayBox('new_site_owner2').value== g_form.getDisplayBox('new_site_owner').value) {


          alert("Owners cannot be the same");


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


    }


    }



Please let me know if you have any questions.



Thanks,


Pradeep Sharma


chrish5
Giga Guru

Pradeep,


Thanks for your reply.   I tried the script you provided, but it did not work.   If I set the reference variables new_site_owner and new_site_owner2 to the same value, nothing happens.  



I've tried the below script, but this throws up the alert for any vaules I put in the two variables.  



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', '');


    }


    }


Hi Chris Hetrick



It is working fine on the below demo


https://demo007.service-now.com/login.do


Username:admin,Password:admin


Steps for testing:Navigate to Service catalog->Maintain items->Access



Thanks,


Pradeep Sharma


Viswanath,


Adding the () to the end of the toString resolved my problem.   Thank you for your assistance.