Client script to make variables mandatory- after submit

Gemma4
Mega Sage

Hi everyone,

I am trying to make variables mandatory after the request has been submitted, if the user has the pmsubmitter rule and if a custom field additional information is set to yes. I'm attempting to do this with a client script (below and attached) . Below is the script I am trying and attached are the results. When I impersonate a user after the request has been submitted, with the pmsubmitter role the platform is showing a black * instead of red indicating the field is required. In addition, the user that submits the request needs to be able to show these variables as required on the portal after summitted and their manager selects Yes at additional information. Unfortunately the variables are not getting set as required on the portal. Is this possible? In the past I've utilized sctasks and a flow to achieve. Below 

 

Thank you in advance for any guidance you can provide!

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (g_user.hasRoleExactly("pmsubmitter"))

if (newValue === 'Yes') //yes
g_form.setMandatory('test1', true);
else
g_form.setMandatory('test1', false);
}clientscript.PNG

 

 Platform Required.pngportal not required2.PNG this but the requirement is to simply use the ritm for all the data. 

10 REPLIES 10

Gemma4
Mega Sage

Also, I tried the following onchange client script using field additional information and didn't have any luck

 

//When: onChange
//onChange of field: u_additional information ---additional informaion
//Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
//If the form is loading or the new value of the changing field is empty exit the script
if (isLoading || newValue == '') {
return;
}

if(newValue == 'Yes' && g_user.hasRoleExactly("pmsubmitter")) {

g_form.setMandatory('test1', true);
g_form.setMandatory('test2', true);
}
//repeat for the other scenarios
else if (newValue == 'No' && g_user.hasRoleExactly("pmsubmitter")) {

g_form.setMandatory('test1', false);
g_form.setMandatory('test2', false);
}

}

Albert
Tera Contributor

There could be a way to achieve this:

 

1. Create a client script (OnChange of Additional Information) or a UI Policy with Run scripts

2. Apply the conditions to only trigger the mandatory behavior to tickets that needs it (If necessary). Example: Apply only to that specific catalog item (Item == [sample_item] AND additional_info == yes)

3. Check for the Additional Information field's value and the user role -> Make any variable mandatory using example: g_form.setMandatory('variables.test1_variable_name', true);

Let me know if that helps. Thanks

Thank you so much for the feedback. 

Below are the changes I made and after the changes made when I select Yes at additional information I receive the following error 

Script error encountered when changing this field - please contact your System Administrator

Do you have any other suggestions. 

 

//When: onChange
//onChange of field: u_additional information ---additional informaion
//Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   //If the form is loading or the new value of the changing field is empty exit the script
   if (isLoading || newValue == '') {
      return;
   }
  
   if(newValue == 'Yes' && g_user.hasRoleExactly("pmsubmitter" && Item == [test_catalog_item] )) {
    
g_form.setMandatory('variables.test1_variable_name', true);
//g_form.setMandatory('test1', true);
// g_form.setMandatory('test2', true);
g_form.setMandatory('variables.test2_variable_name', true);   
   }
   //repeat for the other scenarios
   else if (newValue == 'No' && g_user.hasRoleExactly("pmsubmitter"))  {
  
//g_form.setMandatory('test1', false);
//g_form.setMandatory('test2', false);
g_form.setMandatory('variables.test1_variable_name', false); 
g_form.setMandatory('variables.test2_variable_name', false);      
   }
  
  }

 

Albert
Tera Contributor

use g_form.setMandatory('variables.[insert your variable name here]', true);

 

If my Variable is Test1 with name test_1

code should be g_form.setMandatory('variables.test_1', true);