Onchange client script

GBS
Tera Contributor

I have written the below onchange client script to make Subcategory mandatory when the Category is Software but it is not working.

 

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

    var category = g_form.getValue('category');

    if (category === 'software') {
        g_form.setMandatory('subcategory', true);
    } else {
        g_form.setMandatory('subcategory', false);
    }

}
8 REPLIES 8

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @GBS 

 

Create UI Policy

 

LearnNGrowAtul_0-1702995752137.png

 

LearnNGrowAtul_1-1702995763588.png

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Thanks for the response, but I want to achieve it by script 

Hi @GBS 

 

Description

When client script is used to make a field not mandatory, the field is instead made mandatory.

Steps to Reproduce

 

  1. Create a client script to make the Short Description field of a change request NOT mandatory.
    An example client script is attached to this article.
  2. Open a change request record.
    Note that the Short Description field does not appear to be mandatory.
  3. Clear the Short Description field and try to save the record.
    A warning is displayed stating that the short description is mandatory and the record cannot be saved. 

Workaround

Work around the issue by setting your script to use a boolean argument rather than a string:

Before: g_form.setMandatory(field, 'false')

After: g_form.setMandatory(field, false);


Related Problem: PRB706263
*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @GBS 

 

Syntax:

 

g_form.setMandatory('fieldname',true); // To make mandatory

 

g_form.setMandatoy('fieldname',false); //to make non-mandatory



g_form.setReadOnly('fieldname',true); // To make Readonly

 

g_form.setReadOnly('fieldname',false); //to make non-readonly

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************