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.

Force Mandatory Variable

Community Alums
Not applicable

Hi Connections,

 

I have a variable. Safe Updating with three choices, when safe updating is selected, I want to push user to fill any ONE of the below variables, Standard User Role OR Safe Manager Role.  How can I achieve this. any suggestions... to show that two variables i have created UI policy to appear on the form when safe updating is selected....

 

Rafmine_0-1699483036531.png

 

appreciate any help.

 

Best Regards,

Rafmine.

 

 

 

18 REPLIES 18

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

update as this, you should use OR condition

function onSubmit() {
	//Type appropriate comment here, and begin script below

	var val = g_form.getValue('safeUpdatingVariable');
	var userRole = g_form.getValue('userRoleVariable');
	var managerRole = g_form.getValue('managerRoleVariable');

	if(val == 'add user to sale value' && (userRole == '' || managerRole == '')){
		alert('Kindly fill the Standard User Role Value & Safe Manager Role Value');
		return false;
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Vishal Birajdar
Giga Sage

Hi @Community Alums 

 

Jus a suggestion...!!

Best way is if possible then to create one new variable of type select box & depending upon the selection you can show the user role fields like below : 

 

1.Select box with dropdown

VishalBirajdar_0-1699507806937.png

 

2. On selection show the necessary fields

If user role selected

VishalBirajdar_1-1699507878346.png

 

If manager role selected

 

VishalBirajdar_2-1699507923705.png

 

 

 

 

 

 

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Community Alums
Not applicable

Hi Vishal,

 

I cannot create new variables, its not allowed... Your idea is best but my bad.... do you have second option?

 

Rafmine.

Hi @Community Alums 

 

Another workaround might be like this : 

 

As you mentioned in your post , you have created ui policy to show both variable on form now try below :

 

1. Create onChange() client script on "Safe updating"

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
/*if Safe updating == Add user to safe then make both fields mandatory*/
   if (newValue == 'add_user_to_safe'){
     g_form.setMandatory('standard_user_role',true);
     g_form.setMandatory('standard_manager_role',true);
   } else {
      g_form.setMandatory('standard_user_role',false);
     g_form.setMandatory('standard_manager_role',false);
   }
   
}

 

2.Create onChange() client script on Standard User role variable 

 

function onChange(control, oldValue, newValue, isLoading) {
/*if user have entred some value in Standard user role variable then make Standard manager role variable mandatory as false*/
   if (newValue != ''){
     g_form.setMandatory('standard_manager_role',false);
   } else {
     g_form.setMandatory('standard_manager_role',true);
   }
   
}

 

3.Create onChange() client script on Standard Manager role variable 

 

 

function onChange(control, oldValue, newValue, isLoading) {
/*if user have entred some value in Standard manager role variable then make Standard user role variable mandatory as false*/
   if (newValue != ''){
     g_form.setMandatory('standard_user_role',false);
   } else {
     g_form.setMandatory('standard_user_role',true);
   }
   
}

 

 

You can adjust the script as per your need as there might be contradiction.

Also use your backend name of  variables whenever necessary.

 

Hope this helps...!!

 

 

  

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates