UI Action with mandatory form fields challenge

Valon Sheremeti
Kilo Guru

I have a need to run UI Action on a form which has mandatory fields.

Any best practices are appreciated.

 

So far this is all I can think of:

 

-- I can run UI Action with 'Client' checked , (but I need mandatory fields to remain mandatory after UI Action completes)

The way I foresee this process with UI Action is below, but I'm not sure how to call step 3 below

1. Run (client side of UI Action):  g_form.setMandatory(field, false) - this is doable

2. Run (server side of UI Action): gs.addInfoMessage("SSN is:"+ current.variables.ssn.getDecryptedValue()) - this is doable

3. Run - again (client side of UI Action):  g_form.setMandatory(field, true) - this is my challenge - how to call this client script after both client (1) and server (2) scripts are executed.

 

Sharing other alternatives how to do this are appreciated,

Thank you

1 ACCEPTED SOLUTION

Valon Sheremeti
Kilo Guru

OK - I figured it out 🙂

So, the following UI Action script will mark required fields as notMandatory, then will execute server-side script and then will reload the form and previously mandatory fields will show up again (as mandatory)

 

Details below apply to 'Catalog Task'

I hope you will find them helpful

 

Action Name: display_ssn_and_dob

UI Action needs to have: Client box checked

On Click : nomandatory();

Condition: // you put your own conditions //

 

Script:

//The following code calls client side part of script

function nomandatory()
{
g_form.checkMandatory = false; //This makes form fields not mandatory
try {

//This code makes catalog task variables not mandatory
var allVariables = document.getElementById('variable_map').getElementsByTagName('item');
for(var i = 0; i < allVariables.length; i++){
var item = allVariables[i];
g_form.setMandatory('variables.' + item.getAttribute('qname').toString(),false);
}
} catch(err) {}

// The following code calls server side part of script
gsftSubmit(null, g_form.getFormElement(), 'display_ssn_and_dob');
}

showpii();

//This is Server side code

function showpii()
{
gs.addInfoMessage("Requester's SSN is: "+ current.variables.reqs_ssn.getDecryptedValue()+". Requester's DOB is:"+ current.variables.reqs_dob.getDecryptedValue());
action.setRedirectURL(current);
}

 

 

 

My research resources are:

https://community.servicenow.com/community?id=community_question&sys_id=25e15faddbdcdbc01dcaf3231f961926

 

View solution in original post

2 REPLIES 2

Valon Sheremeti
Kilo Guru

Simplifying my own question 🙂

How do I run a UI Action in a form which has mandatory fields (and those fields should remain mandatory after UI Action script completes)? 

Thank you

Valon Sheremeti
Kilo Guru

OK - I figured it out 🙂

So, the following UI Action script will mark required fields as notMandatory, then will execute server-side script and then will reload the form and previously mandatory fields will show up again (as mandatory)

 

Details below apply to 'Catalog Task'

I hope you will find them helpful

 

Action Name: display_ssn_and_dob

UI Action needs to have: Client box checked

On Click : nomandatory();

Condition: // you put your own conditions //

 

Script:

//The following code calls client side part of script

function nomandatory()
{
g_form.checkMandatory = false; //This makes form fields not mandatory
try {

//This code makes catalog task variables not mandatory
var allVariables = document.getElementById('variable_map').getElementsByTagName('item');
for(var i = 0; i < allVariables.length; i++){
var item = allVariables[i];
g_form.setMandatory('variables.' + item.getAttribute('qname').toString(),false);
}
} catch(err) {}

// The following code calls server side part of script
gsftSubmit(null, g_form.getFormElement(), 'display_ssn_and_dob');
}

showpii();

//This is Server side code

function showpii()
{
gs.addInfoMessage("Requester's SSN is: "+ current.variables.reqs_ssn.getDecryptedValue()+". Requester's DOB is:"+ current.variables.reqs_dob.getDecryptedValue());
action.setRedirectURL(current);
}

 

 

 

My research resources are:

https://community.servicenow.com/community?id=community_question&sys_id=25e15faddbdcdbc01dcaf3231f961926