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

CapaJC
ServiceNow Employee
ServiceNow Employee

Maybe try an onChange client script on the drop-down field, and if it has that one value, do this (replace "short_description" with your field name):



var e = g_form.getControl("short_description");
if (e)
e.setAttribute("maxlength", 11);


If it has another value, set maxlength to 25.


tsam
Kilo Explorer

Hi -

I have tried your sample script and i still can't force the field limited to 11 characters when a particular selection is mode from my drop-down list. Below is what I currently have for my script:

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

var sap = g_form.getControl("u_job_type");

if (sap == "SAP Job"){
sap.setAttribute("maxlength", 11);
}
}

Am i missing anything else?

Thanks.


Hi.
What you currently have doesn't work: if (sap == "SAP Job")...
You should also declare another variable for your choice field. This is an onChange Client script for the field that contains your choices.

Try this:
function onChange(control, oldValue, newValue, isLoading) {
var ujt = g_form.getControl('u_job_type');
var ichose = g_form.getValue('choicelist'); //replace with the your field name
if (ichose == 'SAP Job'){
ujt.maxLength = 11;
}else{
ujt.maxLength = 25;
}
}


I have tried your sample script and it's still not working for me. I was also thinking of another way to get this done - checking the length of a field to make sure it fits the max length requiremnt of 32 characters but I am not sure how to check that.

Any assistance will be very much appreciated.

Thanks.