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

Ratnakar7
Mega Sage
Mega Sage

Hi @Gemma4 ,

 

Try with below updated script:

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);
    }
}

 

Thanks,

Ratnakar