Custom field validation scripts?

mike_sol
Giga Contributor

Hello all,

We're implementing Service Catalog, and as such we're building up quite the library of forms.

One thing that seems to be a glaring piece of missing functionality is the ability to have custom field validations, e.g. for data types like URLs, IP addresses, etc. This appears to be a big weakness in the ServiceNow form engine.

I have no problem writing my own Javascript library for field validation (actually, I have all the code sitting around waiting to be refactored). However, I don't want to do that if there's some better way.

Certainly, it's not acceptable to implement this with an onChange script for every single form field. That's a lot of repeated code!

I had been thinking of doing this with a UI Script, but I also notice that there is a 'validation scripts' section. That seems like an appropriate place for the code, but I do not see any obvious way of connecting these validation scripts to form fields on Service Catalog items. There's a 'type' field in the validation script that lists what I think I've seen referred to as "internal types", but that's a bit opaque to me at the moment.

Any suggestions?

I think ideally, I'd write a series of data type validation functions (regexp based) for each of the specific field types I need extended validation on, and then write a catalog client script or UI policy that sets the extended validation on each field, hopefully with only one line required per field. In order for that to work, I'd need to be able to reference the validation library, perhaps with a UI script, though it seems like the use of those is "discouraged".

Surely someone else has implemented fields that need a very specific datatype or format.....

2 REPLIES 2

Frank_Wilhelm
Kilo Expert

I had this idea about implementing custom validations quite a while but your question finally made me test my idea. And turns out it works 🙂

What I found some time ago was that a list of field types is stored in the table "Field class" (


sys_glide_object
). The idea is to create a new type and for this custom type implement a custom validation. This is not really documented but I think I figured out how it works.

The important thing about creating a custom Field class is to select the base type which it Extends. With string you should be fine for most uses. Additionally to this you can specify a default Length and default Attributes which will be used in the dictionary entry when you create a new field of this type. Do not put anything into the Class name field.

With this setup you can then change the field type of an existing string field to the newly created one. You can then also select this type when building a Validation Script.

Since this is not documented you should try to get some official statement from ServiceNow before planning this for production.

_______________________________

If this is not a viable solution there is also another way which requires some more complicated steps to implement.

The basic idea is to use the Dictionary Attribute

field_decorations
to add a UI Macro to the field. This UI Macro will always be executed when the field is put on the form. The macro could then hook into the onSubmit and onChange to do the validation of the field value. This is more complicated and needs some knowledge about UI Macros.


Very interesting suggestions. Implementing a new Field type sounds like a logical way to proceed, as I wouldn't be reinventing the wheel and implementing my entire own way of entering field data types and hooking in for their validation.

However, just for clarification - the primary target of this validation will be Service Catalog Variables, which I believe are different from every other kind of field in the system, no? I don't know how to select from that list of internal field types when creating an SC variable.

If there's some way to do that, please fill me in, as even having access to some of the existing internal types would go a long way beyond simple string fields.