VaranAwesomenow
Mega Sage
//Query itask short description / description for a keyword and include tat info as a concatenated list

var sDebug = '';
var taskDetailsHash = {};
var keywordList = 'shared|folder';
var keywordArray = [];
keywordArray = keywordList.split("|");
sDebug += 'Printing search results for keyword count =' + keywordArray.length + "\n";
for (var i = 0; i < keywordArray.length; i++) {
    if (JSUtil.notNil(keywordArray[i])) {
        searchKeyword(keywordArray[i]);
    }
}


function searchKeyword(keyword) {
    var taskDetails = '';
    var taskDetailsArray = [];
    var grTask = new GlideRecord('task');
    var enTaskQuery = 'cmdb_ci.nameLIKE' + keyword + '^short_descriptionLIKE' + keyword + '^OR' + 'descriptionLIKE' + keyword;
    enTaskQuery = 'GOTO123TEXTQUERY321=' + keyword;
    grTask.addEncodedQuery(enTaskQuery);
    grTask.query();
    while (grTask.next()) {
		taskDetails = keyword + "|" + grTask.number +"|" + grTask.sys_class_name + "|" + grTask.cmdb_ci.name.toString() + "|" + grTask.short_description;
		sDebug += taskDetails + "\n";
    }
}
gs.print(sDebug);
 
This code is used to query the "task" table in ServiceNow based on a keyword and generate a pipe-separated output. Here's a breakdown of the code:
 
**Variables and Initialization**
 
* `sDebug`: an empty string variable used for debugging purposes.
* `taskDetailsHash`: an empty object (hash) that will store the task details.
* `keywordList`: a string containing the keywords to search, separated by pipes (`|`). In this case, it's set to `'shared|folder'`.
* `keywordArray`: an array created from the `keywordList` string, splitting it into individual keywords using the `split()` method.
* `sDebug` is updated with the number of keywords found in the `keywordList`.
 
**Main Loop**
 
The code loops through each keyword in the `keywordArray` using a `for` loop. For each keyword, it calls the `searchKeyword()` function.
 
**searchKeyword() Function**
 
This function performs the actual query on the "task" table based on the provided keyword.
 
* `taskDetails`: an empty string that will store the task details.
* `taskDetailsArray`: an empty array that will store the task details.
* `grTask`: a GlideRecord object created to query the "task" table.
* `enTaskQuery`: a string that constructs the query filter. It's a combination of three conditions:
1. `cmdb_ci.nameLIKE`: matches the task's CI name with the keyword.
2. `short_descriptionLIKE`: matches the task's short description with the keyword.
3. `descriptionLIKE`: matches the task's description with the keyword.
* `grTask.addEncodedQuery(enTaskQuery)`: adds the query filter to the GlideRecord object.
* `grTask.query()`: executes the query.
* The code loops through each record in the query result using a `while` loop and constructs a string `taskDetails` that contains the following information:
1. The keyword.
2. The task number.
3. The task's sys_class_name.
4. The task's CI name.
5. The task's short description.
* The `taskDetails` string is appended to `sDebug` with a newline character (`\n`).
 
**Final Output**
 
The code prints the `sDebug` string to the console, which contains the task details for each keyword found in the `keywordList`.
 
In summary, this code searches the "task" table in ServiceNow for tasks that match the provided keywords in the `keywordList`, and generates a pipe-separated output containing the task details for each matching task.