Easy way to eliminate leading/trailing spaces in ALL Catalog Items?

MBarrott
Mega Sage

I would like to ensure that text fields such as Names are cleaned up prior to submission. 

 

I could create an onChange Client Script for each respective field but is there a simpler solution?

 

I have lot of Catalog Items and don't deem it appropriate to create multiples of the same script with minor field modifications. 

 

Is there a more appropriate solution? Right now I'm using a regex validation but something which automatically cleans up the fields would be optimal.

3 REPLIES 3

Chaitanya ILCR
Kilo Patron

Hi @MBarrott ,
Assuming you are only removing leading/trailing spaces

you can create a before BR with insert and update operations

 

var fields = ['name', 'description']; //add the fields that you want to remove these space for
for (var i = 0; i < fields.length; i++) {
    var curValue = current.getValue(fields[i]);
    if (curValue)
        current.setValue(fields[i], curValue.trim());
}

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

Ankur Bawiskar
Tera Patron
Tera Patron

@MBarrott 

you can use before insert business rule on sc_req_item table and remove the leading and trailing spaces using trim() method on those single line text variables.

what script did you try and what didn't work?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I created one similar to @Chaitanya ILCR but with variables. You should be able to do a Before Insert/Update business rule on the sc_req_item table and put this code in it. It will only trim from variables that are string type.

(function(current, previous, gs, action) {
    for (var key in current.variables) {
        var v = current.variables[key];
        descriptor = v.getGlideObject().getQuestion().getED();
        var internalType = descriptor.getInternalType();
        if (internalType == 'string') {
			str = v.getDisplayValue().trim();
            v.setValue(str);
        }

    }
})(current, previous, gs, action);