Make a field not mandatory

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 the manager determines additional information is needed then the variables are required by a yes/no ui policy

The problem is the manager doesn't need to complete the form, so I need to exclude her and others from seeing them as required. 

I realize the best way to achieve this is probably with a UIPolicy but I'm struggling on how to exclude the manager/group from seeing the variables 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?

 

function onSubmit() {

 

    //Type appropriate comment here, and begin script below

 

    g_form.setMandatory('field_name',false);

 

}

 

Thanks so much for any feedback you have!

27 REPLIES 27

OK, I tested this in my developer instance. 

Catalog UI Policy:

cgedney_0-1690310190383.pngcgedney_1-1690310207061.png

Then I turned the script on and set the true script to:

cgedney_2-1690310260817.png

 

Hope that works for you...

 

Hi @cgedney 

I've tested again in my pdi and still no luck. I checked with the business and doesn't sound like they want to utilize my workaround with the extra field. Below is a summary of everything I've attempted in case I'm missing something so small. Thank you for all the help, please let me know if you  have any ideas. 

 

maintain items (Project Management Request Catalog )
catalog Client script
 
Name:Mandatory Catalog Item Variables 3
message appeared
tested pdi
tested dev
ui type All
Selected
Applies on Catalog item view
applies on Requested Items
Isolate script selected
 
Notes
Objective is Not required for PM users when additional information is changed to Yes-That is correct
Objective is not required for all other users. The objective variable is required if I refresh or try to select it again. 
 
 
 
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
 
   //Type appropriate comment here, and begin script below
var role = g_user.hasRole('pm');
var ynAddtnlInfo = g_form.getValue('additional_information');
 
 g_form.addInfoMessage("Addtnl Info value: " + ynAddtnlInfo);
 g_form.addInfoMessage("Has pm role: " + role);
 
if (ynAddtnlInfo == 'Yes' && role == true) {
g_form.setMandatory('objectives',true);
}
else {
g_form.setMandatory('objectives',false);
}   
}
 
 
////////////////////////////////////////////////////////////////////////////////////////////??????????????
Maintain items- (Project Management Request Catalog )
catalog Client script
 
Name:Mandatory Catalog Item Variables 1
message appeared
tested pdi
tested dev
 
ui type All
Selected
Applies on Catalog item view
applies on Requested Items
Isolate script selected
 
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');
 
g_form.addInfoMessage("Addtnl Info value: " + ynAddtnlInfo);
 g_form.addInfoMessage("Has pm role: " + role);
 
    //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);
    }   
}
 
 
////////////////////////////////////////////////////////////////////////////////////////
Maintain items- (Project Management Request Catalog )
catalog Client script
 
Name:Mandatory Catalog Item Variables 2
message DID NOT appear
tested pdi
tested dev
 
ui type All
Selected
Applies on Catalog item view
applies on Requested Items
Isolate script selected
 
Notes: 
Objective is Not required for PM users when additional information is changed to Yes-That is correct
Objective is not required for all other users. 
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);
    }
}
 
 
/////////////////////////////////////////////
Catalog UI Policies
Name:Mandatory Catalog Item Variables 4 Catalog UI Policy
When to Apply additional informaiton is Yes
applies on requested items
onload selected
reversr if false selected
Execute if True
 
function onChange() {
var role = g_user.hasRole('pm');
g_form.addInfoMessage("Role; " + role);
if (role == true) {
g_form.setMandatory('objectives', true);
}
else {
g_form.setMandatory('objectives', false);
}
 
return true;
}
 
Execute if false
function onCondition() {
 
}
 

Hey Gemma4, 

 

OK, you need to de-activate the Catalog Client Script, since the UI Policy is doing the work now.

 

I am not 100% sure I understand the criteria you are asking for. Originally, you said that if the user has the pm role and they select Yes to the additional Info, then the objective is mandatory. Everything else, the objective is not mandatory. Is that correct?

 

If so, then this is what I have:

I created a catalog item with these variables:

cgedney_0-1690394906646.png

 

I then created a Catalog UI Policy:

cgedney_1-1690394976161.png

cgedney_2-1690394993437.png

And on the script tab:

cgedney_3-1690395034626.png

function onCondition() {
	var role = g_user.hasRole('pm');
	g_form.addInfoMessage("Role: " + role);
	if (role == true) {
		g_form.setMandatory('objectives', true);
	}
	else {
		g_form.setMandatory('objectives', false);
	}

	return true;
}

 

Let me know if that works.