Mandatory fields not showing red asterisk

mjg5111
Mega Guru

Hello,

I currently have a requirement that on the click of a Request Approval UI Action, certain fields need to be made mandatory. At first, only 4 fields needed to be a part of this, however the requirements changed and 5 additional fields were added to the list. The first 4 fields are working properly, if the field doesn't contain a value, an error message appears and the red asterisk appears next to the appropriate field.

I added the 5 additional fields into the UI Action, navigated back to the form, and clicked it. This time, the first 4 fields were identified with a red asterisk, but the 5 additional fields that I added did NOT have the red asterisk next to them. I tested another field in the same form section and the red asterisk appeared. I then created a new 'test' field and included it in the UI Action script, but no asterisk. I've included my code below:

function requestApproval() {

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

var pricingStructure = g_form.getValue('pricing_structure');

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

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

var stc = g_form.getValue('u_standard_terms_and_conditions');

var perUnitCost = g_form.getValue('u_specified_per_unit_cost');

var minPurchase = g_form.getValue('u_minimum_purchase_requirement');

var initialPayment = g_form.getValue('u_initial_payment_to_enter_agreement');

var penalties = g_form.getValue('u_specified_penalties_for_breaking');

if((quantities == '') || (pricingStructure == '') || (pricing == '') || (freight == '') || (stc == '') || (perUnitCost == '') || (minPurchase == '') || (initialPayment == '') || (penalties == '')) {

g_form.setMandatory('quantities', true);                                             //Red asterisk appears for this field

g_form.setMandatory('pricing_structure', true);                       //Red asterisk appears for this field

g_form.setMandatory('pricing', true);                                                       //Red asterisk appears for this field

g_form.setMandatory('freight', true);                                                         //Red asterisk appears for this field

g_form.setMandatory('stc', true);                                                                   /*Red asterisk DOES NOT appear*/

g_form.setMandatory('perUnitCost', true);                                     /*Red asterisk DOES NOT appear*/

g_form.setMandatory('minPurchase', true);                                 /*Red asterisk DOES NOT appear*/

g_form.setMandatory('initialPayment', true);                               /*Red asterisk DOES NOT appear*/

g_form.setMandatory('penalties', true);                                               /*Red asterisk DOES NOT appear*/

g_form.addErrorMessage('You must complete all mandatory fields before requesting approval.');

return false;

}

// Call the UI Action, and skip the "onclick" function.

gsftSubmit(null, g_form.getFormElement(), 'request_approval');

}

if (typeof window == 'undefined')

validateRequest();

function validateRequest() {

current.status = 'approval_requested';

current.update();

action.setRedirectURL(current);

}

I think it might be an issue with ServiceNow, but I wanted to see if anyone else has ever had this issue or knows of a way to resolve it. Thank you!

1 ACCEPTED SOLUTION

mjg5111
Mega Guru

**UPDATE**



I figured out my issue. If you look at the code, you'll notice that I used the variables (which were the values of the fields) instead of using the field name in my setMandatory(). I was using g_form.setMandatory('stc', true); instead of g_form.setMandatory('u_standard_terms_and_conditions', true);



Thank you for responding Gurpreet!


View solution in original post

4 REPLIES 4

Gurpreet07
Mega Sage

Any errors on console? Any other conflicting UI Policies/Scripts ?


Hello Gupreet,



I can't find any errors on the console nor are there any conflicting UI policies or scripts.


Gurpreet,



For the JS log (referencing code from my original question):



g_form.setMandatory('freight', true);                                                         //Red asterisk appears for this field


          jslog(g_form.isMandatory('freight'));                                                 *Returns True


g_form.setMandatory('stc', true);                                                                   /*Red asterisk DOES NOT appear*/


          jslog(g_form.isMandatory('freight'));                                                 *Returns False



The jslog is saying that the fields are not mandatory even though I am unable to save the record unless I complete all of those fields.


mjg5111
Mega Guru

**UPDATE**



I figured out my issue. If you look at the code, you'll notice that I used the variables (which were the values of the fields) instead of using the field name in my setMandatory(). I was using g_form.setMandatory('stc', true); instead of g_form.setMandatory('u_standard_terms_and_conditions', true);



Thank you for responding Gurpreet!