In servicenow script include how to put condition that if short description contains xyz then return false
Assistant
In a ServiceNow Script Include, you generally write server-side JavaScript to perform various operations. If you want to create a method that checks whether the "short description" contains the string "xyz" and returns false if it does, you can use the following pattern:
User
I need to add query
Assistant
If you need to add a query to filter records before checking the "short description," you will typically use a GlideRecord query in the Script Include. Here's how you can modify the Script Include to query a table and then check the "short description" for "xyz":
User
Print sc task number based on query on background script
Assistant
To print the task number of Service Catalog Task (sc_task) records based on a specific query in a background script in ServiceNow, you can use the following script. This script can be executed in the Scripts - Background module.
Here’s an example script that filters tasks based on certain criteria and prints their task numbers:
// Create a new GlideRecord instance for the 'sc_task' table
var taskGr = new GlideRecord('sc_task');
// Add query conditions to filter the tasks
// For example, find tasks where the 'state' is 'Work in Progress'
taskGr.addQuery('state', 'Work in Progress');
// Add more query conditions as needed
// taskGr.addQuery('another_field', 'value');
// Execute the query against the database
taskGr.query();
// Iterate over the result set and print the task numbers
while (taskGr.next()) {
// Print the task number field
gs.print('Task Number: ' + taskGr.number);
}
User
i need to add query like gs.addQuery('short_description', contains 'xys')
Assistant
To filter records based on whether the short_description field contains a specific substring (e.g., "xyz"), you need to use the addQuery method with the CONTAINS operator in a GlideRecord query. Here's how you can do it in a background script:
// Create a new GlideRecord instance for the 'sc_task' table
var taskGr = new GlideRecord('sc_task');
// Add a query condition to filter tasks where 'short_description' contains 'xyz'
taskGr.addQuery('short_description', 'CONTAINS', 'xyz');
// Execute the query against the database
taskGr.query();
// Iterate over the result set and print the task numbers
while (taskGr.next()) {
// Print the task number
gs.print('Task Number: ' + taskGr.number);
}
var YourScriptInclude = Class.create();
YourScriptInclude.prototype = {
initialize: function() {
},
checkShortDescription: function(current) {
// Ensure 'current' is a GlideRecord object and has a 'short_description' field
if (current && current.short_description) {
var shortDescription = current.short_description.toLowerCase();
if (shortDescription.indexOf('xyz') !== -1) {
return false;
}
}
return true;
},
type: 'YourScriptInclude'
};
Explanation:
Class.create(): This is part of the Prototype framework used by ServiceNow for creating classes.
prototype: The prototype object is where you define functions that will be part of your class.
initialize: This function is automatically called when an instance of the class is created.
checkShortDescription: This custom method checks if the short_description field contains "xyz".
current: Typically, you'd pass a GlideRecord object that points to a record in the database.
toLowerCase(): This method is used to make the check case-insensitive.
indexOf('xyz'): Checks if the substring "xyz" is present in the short_description. If it is found, indexOf returns the position of the first occurrence; otherwise, it returns -1.
If "xyz" is found, the function returns false; otherwise, it returns true.
To use this Script Include, ensure it’s set for client-callable if needed on the client side, and use a GlideRecord appropriate for the table you are working with.
For example:
var gr = new GlideRecord('incident');
if (gr.get('sys_id_of_the_incident')) {
var check = new YourScriptInclude().checkShortDescription(gr);
if (!check) {
// Your logic when short description contains 'xyz'
}
}
Ensure to replace 'incident' with your specific table name and 'sys_id_of_the_incident' with the particular record you're examining.
var YourScriptInclude = Class.create();
YourScriptInclude.prototype = {
initialize: function() {
},
checkRecords: function() {
var gr = new GlideRecord('incident'); // Replace 'incident' with your table name
gr.addQuery('active', true); // Example condition: only active records
gr.query();
while (gr.next()) {
if (this.checkShortDescription(gr)) {
// Do something if the short description does not contain 'xyz'
} else {
// Do something if the short description contains 'xyz'
}
}
},
checkShortDescription: function(gr) {
if (gr.short_description) {
var shortDescription = gr.short_description.toLowerCase();
if (shortDescription.indexOf('xyz') !== -1) {
return false;
}
}
return true;
},
type: 'YourScriptInclude'
};
Explanation:
GlideRecord:
var gr = new GlideRecord('incident'); - Replace 'incident' with the name of the table you want to query.
addQuery:
gr.addQuery('active', true); - This adds a condition to only select records where the "active" field is true. You can customize this query to include any conditions necessary for your use case.
query:
gr.query(); - Executes the query against the database.
while (gr.next()):
Iterates through each record returned by the query. gr.next() fetches the next record in the result set.
checkShortDescription(gr):
This method checks if the "short_description" field contains "xyz".
If-Else Logic:
You can replace the comments within the if-else block to include your logic for processing records where the "short description" does or does not contain "xyz".
This structure allows you to query the database for specific records and then perform your condition check on each returned record. Adjust the query conditions in addQuery() to match your specific needs.
Explanation:
GlideRecord:
var taskGr = new GlideRecord('sc_task'); initializes a GlideRecord object for the sc_task table.
addQuery:
taskGr.addQuery('state', 'Work in Progress'); adds a condition to retrieve tasks that are in the "Work in Progress" state. You can replace 'state' and 'Work in Progress' with whatever field and value you need to filter by.
You can add more conditions using addQuery() to narrow down your results as needed.
query:
taskGr.query(); executes the select query with the established conditions.
while (taskGr.next()):
This loop iterates through each record in the result set.
taskGr.next() advances the record pointer to the next record in the GlideRecord object.
gs.print:
gs.print('Task Number: ' + taskGr.number); prints out the number field of each task that matches the query. The gs.print() method outputs to the system log.
You can run this script in the "Scripts - Background" module within ServiceNow to test and see the results in the system logs. Be sure to modify query conditions to suit your specific requirements.
Explanation:
GlideRecord Initialization:
var taskGr = new GlideRecord('sc_task'); creates a new GlideRecord instance targeting the sc_task table.
Query on Short Description:
taskGr.addQuery('short_description', 'CONTAINS', 'xyz'); adds a condition to filter records where the short_description field contains the substring "xyz".
The CONTAINS operator is used for substring matching in ServiceNow queries.
Query Execution:
taskGr.query(); executes the query with the defined condition(s).
Result Iteration and Output:
while (taskGr.next()): Iterates over each record returned by the query.
gs.print('Task Number: ' + taskGr.number);: Prints the task number of each record that matches the query.
Run this script in the "Scripts - Background" module in ServiceNow. It will output the task numbers to the system logs for all sc_task records where the short_description contains "xyz". Make sure the field names match those in your instance, and you can adjust the query condition as needed.