Limit character size on a variable

nogizaka46_jame
Kilo Contributor

Hello good day!

I was wondering if there's a way you can set the max length of a certain variable through an on change script, if you can see the screenshot below, I'm trying to cut down the max length of the Job Title(string field) to 28 characters only, I've tried setting the variable attributes (max_length=28) and it's working fine but it seems like it's only working on load, the problem is, I have a on change script where it populates all the user's information, It seems like setting variable attributes doesn't work for on change scenario:

sapp.jpg

sap2.jpg

3 REPLIES 3

shloke04
Kilo Patron

Hi,



Yes it can be done using On Submit Catalog Client Script for the particular Item instead of on Change Script which would allow you to abort the form submission if the Number of Characters exceeds 28. Please see the script below :




Script:



function onSubmit() {


  //Type appropriate comment here, and begin script below



  var var_name = g_form.getValue('Variable Name');


  g_form.hideFieldMsg('Variable Name', true);


  if(var_name .length > 28)


  {


      g_form.showFieldMsg('Variable Name','VariableName should be less than 28Characters','error');


  return false;


  }


  }



find_real_file.png




In the above Screen shot Replace "description" variable with your Variable Name.



Hope this helps.Mark the answer as correct/helpful based on impact.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

johnolivermendo
ServiceNow Employee
ServiceNow Employee

Hey James,



Are you saying that your onChange auto population script is overriding your max_length set in your variable attributes field? Or are you trying to dynamically set your variable's max length based on different scenarios?



Either way I'd like to know how you're currently trying to set it using your onChange script. Please include your script so I can better assist you.



I would assume you're trying to use a client side glide record query to the variables table and setting the value there.


Christian Engs2
Giga Contributor

Hi 

I solved it with a little onload client jquery script:

 

function onLoad() {
  setTimeout(function() {
    jQuery('input[name="problem.short_description"').attr("maxlength","100");
  }, 1000);
}

find_real_file.png