How to validate values of two fields are not the same

ServiceNow Use6
Tera Guru

Hi,

Can anyone send me the script to validate that two field values are not the same in service catalog.

Regards

Suman P.

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi,

Use something like this :

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

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

          return;   }

   //Type appropriate comment here, and begin script below

       var first_user = g_form.getValue('first reference field name'); //Update the field name here

        if(first_user == newValue)

        {                alert('User can not be the same');

                  g_form.clearValue('second reference field'); //Update the field name here

        }  }

Learn more on Operators for comparing from here: https://docs.servicenow.com/en-US/bundle/sandiego-platform-user-interface/page/administer/navigation...

Mark my answer correct & Helpful, if Applicable.

Thanks,

Sandeep

View solution in original post

3 REPLIES 3

Filipe Cruz
Kilo Sage
Kilo Sage

Hi ServiceNow User,

if(g_form.getValue("variable1") != g_form.getValue("variable2")){
  //fields are different, do something
}
else{
  //fields are equal, do something
}

Please mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Best Regards,

Filipe Cruz

 

Community Alums
Not applicable

Hi,

Use something like this :

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

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

          return;   }

   //Type appropriate comment here, and begin script below

       var first_user = g_form.getValue('first reference field name'); //Update the field name here

        if(first_user == newValue)

        {                alert('User can not be the same');

                  g_form.clearValue('second reference field'); //Update the field name here

        }  }

Learn more on Operators for comparing from here: https://docs.servicenow.com/en-US/bundle/sandiego-platform-user-interface/page/administer/navigation...

Mark my answer correct & Helpful, if Applicable.

Thanks,

Sandeep

Mark Roethof
Tera Patron
Tera Patron

Hi there,

What's the reason for asking about a script? Have you considered a UI Policy?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn