The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to check the mandatory field that contains default value, is added or not through Client Script?

Rutuja1
Kilo Contributor

I have a field in a table which is mandatory field.

Now I have provided a default value for that field. But I want User to fill that field.

it should be like default value will be there and user should suppose to fill that mandatory. Or else the form should not submit.

How do I do this via client script 'onSubmit'? 

5 REPLIES 5

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi - You can do this via OnSubmit client script. Refer below blog for reference on how to check if the field is modified or not.

https://www.servicenowguru.com/scripting/business-rules-scripting/checking-modified-fields-script/

 

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Sample script:

var field1 = g_form.getControl('PASS FILED NAME HERE'); //Get the 'Caller' field control
  
if(field1.changed && field1 != 'PASS DEFALUT VALUE HERE'){
   
return true;
  }
else
{
return false;
}

@Rutuja - FYI - I don't think there is a need to use display BR for this requirement. This is assuming the field is available on the same form. In this option you can directly go with a Client Script.

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Rutuja 

you can try to use combination of display BR and onsubmit client script

Display BR: on your table it will store the default value when it is new record

(function executeRule(current, previous /*null when async*/) {

    // Add your code here

   if(current.isNewRecord()){
    g_scratchpad.value = current.field_name;

}

})(current, previous);

onSubmit client script:

function onSubmit(){

if(g_form.isNewRecord()){

if(g_form.getValue('field_name') == g_scratchpad.value){

// it means user has not changed the default value to some other value

alert('Please change the default value for field');
return false;
}
return true;
}

}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader