- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2016 08:56 AM
Not being able to use RegEx is really limiting.
So I created a workaround that I'd like to share: it is a simple example of a report source where I employ a script include to process my regex.
High level overview
- Report Source calls Script Include
- Script Include returns a regex-validated comma separated list
I used the baseline report source Incidents.Open and added a condition
NUMBER is one of
javascript:new global.RprtSrcHlpr().testThisRegEx('incident'
'number'
'INC001007\\d');
Notes:
a) the parameters are passed into the function "testThisRegEx" without a separating comma. The platform actually removes them when saving.
b) I am looking for incidents with number INC001007x. The regex must be passed as a string - hence the backslash has to be masked. 'INC001007[0-9]' does the same job
Script Include
Name:
RprtSrcHlpr
Client callable:
True
Description:
RprtSrcHlpr takes a regular expression from the condition builder and test a defined column.
Has to be used with "is one of".
Script:
var RprtSrcHlpr = Class.create();
RprtSrcHlpr.prototype = {
initialize: function() {
},
/*_________________________________________________________________
* Description: test a certain column using regular expression,
* because the ServiceNow "matches regex" is not working
* -in this example we are testing the incident number-
* Parameters: tbl - system table to get the column from
* col - column to test without leading and leading slash
* expr - regular expression
* Returns: a comma separated list of values (csl)
________________________________________________________________*/
testThisRegEx: function(tbl,col,expr) {
var resultArray = [];
var csl = "";
var rex = new RegExp(expr, 'g');
var gr = new GlideRecord(tbl);
// gr.addQuery("name", "value"); // add your query here
gr.query();
while (gr.next()) {
var val = gr.getValue(col);
var result = val.search(rex);
if (result==0) { // 0 is the position
resultArray.push(val);
}
}
csl = resultArray.toString();
return csl;
},
type: 'RprtSrcHlpr'
};
Maybe that helps. (Done on Helsinki - hence the global scope)