To limit Short description length to 80 characters

Gopi22
Giga Guru

Hi,

 

I have got a requirement wherein the short description field in Incident form should be limited to 80 characters.  

User who is typing should be able to type only 80 characters and when he/she types the 81st character, an error message should be displayed and not allow to type more.

Please help me with this. Thanks in advance!!!

 

Regards,

Gopi

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

I think, it has to be with onChange() client script, when user enters the value and move out of the field then it can display a message and also, we can set the length of the field to 80



  1. if (newValue.toString().length > 80) {
  2.       g_form.setValue('short_description', newValue.substring(0,79));
  3.       alert('Please enter 80 or less characters.');  
  4. }  

View solution in original post

8 REPLIES 8

shruti_tyagi
ServiceNow Employee
ServiceNow Employee

Hi Gopi,



1. This can be handled by updating length of dictionary record of short description. But if you already have data in this field larger than the length you will not be able to modify it due to existing BR. This is to prevent inadvertent truncation of data, a business rule named "Dictionary Change Rationally" prevents reducing the length of a string field. If you must shorten a string field, you can temporarily disable this business rule. Note that it is there to protect your data and should be enabled by default.



Check out this documentation:


http://wiki.servicenow.com/index.php?title=Introduction_to_Fields#Increasing_the_Length



2. If you dont want to modify dictionary, this can be hadled by onchange client script:



var myFieldValue = g_form.getValue('short_description');


if (myFieldValue.toString().length > 80) {


      g_form.setValue('short_description','');


      alert('write something');


}


Hi Shruti,

Will this Script work for "Additional Comments" also ? I tried the same but g_form.setValue('comments','') is  not working.

SanjivMeher
Kilo Patron
Kilo Patron

Hi Gopi,



You can write an onChange client script on short description to check the length and if it is greater than 80, use g_form.showErrorMsg to show the error message. Then substring to show the first 80 chars only.



Please mark this response as correct or helpful if it assisted you with your question.

Mwatkins
ServiceNow Employee
ServiceNow Employee

Hi Gopi!


I would go with a Client Script on the front end for good user experience and Business Rule on the server to catch anything that goes through.



Business Rule


Condition: current.getValue("short_description").length > 80


Script:


current.abortAction(true);


gs.addErrorMessage("The short description field can be no more than 80 characters")



Client Script


Type: onChange


Script:


var sDesc = g_form.getValue("short_description");


if (sDesc.length > 80) {


g_form.showFieldMsg("short_description", "The short description field can be no more than 80 characters", "error");


g_form.setValue("short_description", sDesc.substring(0,79));


}



I haven't tested those scripts, but they should get you 90% there.