logo

NJP

Varan back to basics :isValidEncodedQuery() and glide.invalid_query.returns_no_rows

Import · Nov 15, 2022 · article

In order to prevent accidently querying ServiceNow using a malformed encoded query implement following

1. Create a system property glide.invalid_query.returns_no_rows with value as true.

2. Add validation in your scripts to check if encoded query is valid using isValidEncodedQuery()

Video :

Example script :

try {var counter = 0;var grServer = new GlideRecord('cmdb_ci_server');var enQuery = 'sys_class_name=cmdb_ci_server'; // class is servervar isValidQuery = grServer.isValidEncodedQuery(enQuery);if (isValidQuery) {grServer.addEncodedQuery(enQuery);//grServer.setLimit(1);grServer.query();while (grServer.next()) {grServer.setValue('short_description', "Iam a server");grServer.setWorkflow();//grServer.update();counter++;}} else {throw "Encoded query is invalid. Please check and update the script";}gs.print('Records updated = ' + counter);}catch(ex) {var message = ex + '';gs.print('caught exception=' + message);

}

View original source

https://www.servicenow.com/community/now-platform-articles/varan-back-to-basics-isvalidencodedquery-and-glide-invalid-query/ta-p/2382631