Setting Max length for a string field based on choice selection

tsam
Kilo Explorer

Hi -

I have a field in Service now that I need to limit the max length based on a selection from a drop-down list.

The field in question is a string field and I have been able to set the current max length to 25 characters, but I need this to be set to 11 characters based on 1 entry in my drop-down selection list.

Any assistance will be very much appreciated.

Thanks.

8 REPLIES 8

Please verify the element names in your script.

In demo008, I have this running as both a catalog client script and a client script on the incident form.


Here is the script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

var jt = g_form.getControl('u_job_type');//choice field
var sap = g_form.getValue('u_job_type');//selection value

if (sap == 'SAP Job'){
jt.maxLength = 11;
}else{
jt.maxLength = 25;
}
}


JordanLind
Kilo Guru

Please confirm which fields you are declaring in the variables.

The value in var jt should be the name of the string field that you want to control the length.
The value in var sap should be the name of the choice field.

var jt = g_form.getControl('u_job_type');//choice field
var sap = g_form.getValue('u_job_type');//selection value


tsam
Kilo Explorer

In case anyone else comes across this same question - below is the script I used to solve my issue:

--------------------------------------------------------------------------------------------------------
function onSubmit(control, oldValue, newValue, isLoading, isTemplate) {
//Set number of characters required for field1 below

var mLength = 20;
var field1 = g_form.getControl('field1'); //field I needed to set maximum number of characters
var field2 = g_form.getValue('field2'); // my choice field selection drop down field

if (field2 == 'Selection' && field1.value.length > mLength){
g_form.hideErrorBox('field1');
g_form.showErrorBox('field1', 'ONLY a maximum of ' + mLength + ' characters is allowed in this field.');
return false;
}
else{
g_form.hideErrorBox('field1');
}
}
---------------------------------------------------------------------------------------------------------------------

Thanks to everyone for your continued suggestions and assistance.