How to make variable mandatory based task field conditions.

vanamalashi
Tera Contributor

Hello Servicenow,

I have a requirement as below.

We have a table sn_spend_psd_procurement_task, we have fields Action type for task and Postponed Date.

Under variables we have Postponement by industrial means. How can i configure below requirement for these fields and variable.

1. For Action type for task = milestone, Postponed date" is Mandatory if "Postponement by industrial means" is checked otherwise the field is optional.

2. For Action type for task = milestone, Postponement by industrial means should be Mandatory if Postponed date is not empty.

I have configure OnChange client scriptas below, but this is not working.

    var post = g_form.getValue('variables.postponment_by_industrial_means');
    var posdat = g_form.getValue('u_postponed_date');
   // var asg = g_form.getValue('assigned_to');

    if (newValue == 'milestone' && post == 'Yes') {
        g_form.setMandatory('u_postponed_date', true);
    }

    if (newValue == 'milestone' && posdat != '') {
        g_form.setMandatory('variables.postponment_by_industrial_means', true);
    }

 

Thanks and Regards,

Shivani

3 REPLIES 3

Brad Bowman
Kilo Patron

As with any script, you'll have to add some logs to see how far it is getting, the values it is assigning to the script variables, and which if conditions are being met.  Since this is a client script you can do this with alerts for immediate feedback.  With correct temporary logging you will see where the script is not working as intended.

vaishali231
Giga Guru

Hey @vanamalashi 


Client Script 1
Type OnChange
Field name Action type for task

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
applyPostponementRules();
}

function applyPostponementRules() {
var actionType = g_form.getValue('action_type_for_task');
var postponedDate = g_form.getValue('u_postponed_date');
var industrialPostponement = g_form.getValue('postponment_by_industrial_means');

if (actionType == 'milestone' && industrialPostponement == 'true') {
g_form.setMandatory('u_postponed_date', true);
} else {
g_form.setMandatory('u_postponed_date', false);
}

if (actionType == 'milestone' && postponedDate) {
g_form.setMandatory('postponment_by_industrial_means', true);
} else {
g_form.setMandatory('postponment_by_industrial_means', false);
}
}

Client Script 2
Type OnChange
Field name u_postponed_date

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
applyPostponementRules();
}

function applyPostponementRules() {
var actionType = g_form.getValue('action_type_for_task');
var postponedDate = g_form.getValue('u_postponed_date');
var industrialPostponement = g_form.getValue('postponment_by_industrial_means');

if (actionType == 'milestone' && industrialPostponement == 'true') {
g_form.setMandatory('u_postponed_date', true);
} else {
g_form.setMandatory('u_postponed_date', false);
}

if (actionType == 'milestone' && postponedDate) {
g_form.setMandatory('postponment_by_industrial_means', true);
} else {
g_form.setMandatory('postponment_by_industrial_means', false);
}
}


Client Script 3
Type OnChange
Field name postponment_by_industrial_means

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
applyPostponementRules();
}


*************************************************************************************************************
If this response helps, please mark it as Accept as Solution and Helpful.
Doing so helps others in the community and encourages me to keep contributing.

Regards
Vaishali Singh

hey @vanamalashi 

 

Hope you are doing well.

Did my previous reply answer your question?

If it was helpful, please mark it as correct ✓ and close the thread 🔒. This will help other readers find the solution more easily.

Regards,
Vaishali Singh