help with script, please

Gemma4
Mega Sage

Hi Everyone,

I have a requirement to make fields mandatory on the ritm level but only mandatory for a group of users completing the form? Below are the details of the process

The variables are hidden on the catalog item. 

If someon in the group project management (pm role)  determines additional information is needed then the additional variables are required by a yes/no ui policy

The problem is the manager doesn't need to complete the form with the required fields. An email is triggered to the group that needs to complete it. So I need to exclude her and others from seeing them as required. 

Below is the script I have so far but not sure if there is a better way to do this or if its possible? Currently the variable objective is not becoming mandatory when additional information is selected. 

thanks in advance for any feedback you have. 

 

 

onChange script section: 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return; //return if nothing changed
   }

   //Check to see if user has pm role and grab the additional information field value
    var role = g_user.hasRole('pm');
    var ynAddtnlInfo = g_form.getValue('additional_information');
 
    //if the user selects yes and they have the pm role, then make the objectives field mandatory
    if (ynAddtnlInfo == 'yes') {
        if( role == true) {
            g_form.setMandatory('objectives',true);
        }
        else { //they don't have the pm role, so make it not mandatory
            g_form.setMandatory('objectives',false);
        }
    }
    else { //they selected no, so don't make it mandatory
        g_form.setMandatory('objectives',false);
    }   
}

 

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@Gemma4 

script looks fine.

Did you add alert and check if the IF statements are giving TRUE

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

Hi Ankur,

How do I add an alert to check? 

I tried both script recommendations above and unfortunately I'm still having issues. Both scripts produce different results. 

 

  1. This script below by @Ratnakar7 is :

Allows user with role PM to select Yes at additional fields 

Objective is Not required for the user that selected Yes

The above is perfect

The problem is user without PM role should then be able to go in RITM and variable objective is required. The objective variable is not required even if I refresh or try to select it again. 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return; // Return if nothing changed
    }

    // Check to see if the user has the "pm" role and grab the additional information field value
    var hasPMRole = g_user.hasRole('pm');
    var ynAddtnlInfo = g_form.getValue('additional_information');

    // Get the GlideFormElement object of the "objectives" field
    var objectivesField = g_form.getField('objectives');

    // If the user selects "yes" and they have the "pm" role, then make the "objectives" field mandatory
    if (ynAddtnlInfo === 'yes' && hasPMRole) {
        objectivesField.setMandatory(true);
    } else {
        // Otherwise, don't make it mandatory
        objectivesField.setMandatory(false);
    }
}

 

My original script above and below is having a different issue also

A user with role PM can select Yes at additional fields 

Objective is then  required for the user that selected Yes

The above is a problem because the user that selects Yes at additional information is simply responsible for deterring if additional information is required. A second user without the role PM needs to complete the required fields. The second user that accesses the RITM does not view objective as required. The only way the 2nd user can view it as required is if they select additional information no and then change it to yes. 

 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return; //return if nothing changed
}

//Check to see if user has pm role and grab the additional information field value
var role = g_user.hasRole('pm');
var ynAddtnlInfo = g_form.getValue('additional_information');

//if the user selects yes and they have the pm role, then make the objectives field mandatory
if (ynAddtnlInfo == 'Yes') {
if( role == true) {
g_form.setMandatory('objectives',true);
}
else { //they don't have the pm role, so make it not mandatory
g_form.setMandatory('objectives',false);
}
}
else { //they selected no, so don't make it mandatory
g_form.setMandatory('objectives',false);
}
}

 

 

@Gemma4 

Did you check value returned is Yes or yes

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

Harish KM
Kilo Patron
Kilo Patron

Hi change the below line

if (ynAddtnlInfo == 'yes') to if (ynAddtnlInfo == 'Yes') // Y is capital letter if you chose yes/no variable type

Regards
Harish