how to get the variable value on catalog client script if variable type is yes/No

shreyajoshi
Kilo Expert

I want to check if the variable value is selected as 'yes' or 'No'. Should I write a script on OnChange or OnLoad?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Shreya,



It depends on the requirement.


If you want to determine the value before submission then have onSubmit client script. If you want to change the behavior of some other field(s) based on user's selection of this field i.e. Show particular field and make it mandatory if it is 'Yes' and vice-versa when it is 'No' then have onChange client script.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Harish KM
Kilo Patron
Kilo Patron

it shud b onchange. because your value will change every time you select yes or no



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


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


          return;


    }


var question = g_form.getValue('test');// variable name


  alert(question);


if (question=='yes')


{


//your logic


}


    //Type appropriate comment here, and begin script below


   


}


Regards
Harish

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Shreya,



It depends on the requirement.


If you want to determine the value before submission then have onSubmit client script. If you want to change the behavior of some other field(s) based on user's selection of this field i.e. Show particular field and make it mandatory if it is 'Yes' and vice-versa when it is 'No' then have onChange client script.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

harishdasari
Tera Guru

Hi Sherya,



As per my understanding you are asking how workflow has to check whether variable is returning YES or NO and based upon conditions workflow will route.



Then it that case you can take IF activity in workflow and write code something like this, so that if will check the variable in workflow whether it is yes or no



answer = ifScript();


    function ifScript()


{


    if(current.variables.your variable name == "Yes")


    {


return 'yes';


      }


          return 'no';


    }



Thank you


nayanawadhiya1
Kilo Sage

Shreya,



If you are confused to choose OnChange or OnLoad client script.


Best way is "Include none" in that variable and write script according to your requirement.