In servicenow background script for adding worknotes in records
- User
In servicenow background script for adding worknotes in records
- Assistant
- User
Worknotes not seen
- Assistant
In servicenow background script for adding worknotes in records
Worknotes not seen
In ServiceNow, a background script can be used to add work notes to records. Background scripts are server-side JavaScript code executed in the context of the ServiceNow platform. These scripts can be used for complex data manipulation or automation tasks.
Here’s a basic example of how you could write a background script to add work notes to incident records:
System Definition > Scripts - Background in your ServiceNow instance.// Define the table you want to update, e.g., Incident
var tableName = 'incident';
// Create a GlideRecord object for the specified table
var gr = new GlideRecord(tableName);
// Add your query conditions. For example, here we fetch incidents that are in progress.
gr.addQuery('state', '2'); // 2 is the value for 'In Progress'
gr.query();
// Loop through the records returned by the query
while (gr.next()) {
// Add a work note to each record
gr.work_notes = 'This is an automated work note added via a background script.';
// Update the record in the database
gr.update();
}
// Print a message to the script log
gs.print('Work notes have been added to the selected records.');
Define the Table: Specify the table where you want to add work notes. In this example, we are using the incident table.
Create a GlideRecord Object: Instantiate the GlideRecord object for the specified table.
Add Query Conditions: Use addQuery() to filter records. In this case, we query for incidents that are in the In Progress state (state value 2).
Query: Execute the query to retrieve the filtered set of records.
Loop Through Records: Use a while loop to iterate through each record returned by the query.
Add Work Notes: For each record, we set the work_notes field. In this example, we add a static note text.
Update Record: Save the changes to the record in the database using update().
Log a Message: After updating the records, print a message to the script log to confirm the operation.
work_notes in this case) according to your specific use case and table schema.By using this approach, you can efficiently add work notes to multiple records through a background script in ServiceNow.
If work notes are not visible after executing the background script, it might be due to several reasons. Here are some troubleshooting steps and refinements to ensure the work notes are added correctly:
Use the addWorkNote Method:
work_notes field, use the addWorkNote() method provided by ServiceNow's Task class. This method ensures the work notes are properly logged.Validate the Field Names:
work_notes field for work notes.Check User and Permissions:
Here's an improved version of the script using the addWorkNote method:
// Define the table you want to update, e.g., Incident
var tableName = 'incident';
// Create a GlideRecord object for the specified table
var gr = new GlideRecord(tableName);
// Add your query conditions. For example, here we fetch incidents that are in progress.
gr.addQuery('state', '2'); // 2 is the value for 'In Progress'
gr.query();
// Loop through the records returned by the query
while (gr.next()) {
// Add a work note to each record using the addWorkNote method
gr.setValue('work_notes', 'This is an automated work note added via a background script.');
// Update the record in the database
gr.update();
}
// Print a message to the script log
gs.print('Work notes have been added to the selected records.');
Ensure Fields' Visibility:
work_notes field is visible in the form layout for the records you are updating.Audit Log:
ServiceNow Logging:
gs.log() or gs.print() to add logging statements in your script. This can help you debug by logging entries to the system logs.Here is a version of the script with enhanced logging for better debugging:
// Define the table you want to update, e.g., Incident
var tableName = 'incident';
// Create a GlideRecord object for the specified table
var gr = new GlideRecord(tableName);
// Add your query conditions. For example, here we fetch incidents that are in progress.
gr.addQuery('state', '2'); // 2 is the value for 'In Progress'
gr.query();
// Loop through the records returned by the query
while (gr.next()) {
// Log the sys_id of the record being updated for debugging purposes
gs.log('Updating record with sys_id: ' + gr.sys_id);
// Add a work note to each record using the addWorkNote method
gr.setValue('work_notes', 'This is an automated work note added via a background script.');
// Update the record in the database
gr.update();
// Log success after updating each record
gs.log('Work note added to record with sys_id: ' + gr.sys_id);
}
// Print a message to the script log
gs.print('Work notes have been added to the selected records.');
Scripts - Background editor, execute it and check the logs (System Logs > System Log > All) to see the log entries for each record processed.By following these steps, you should be able to ensure that work notes are correctly added to the records and are visible as expected.
Not yet included in a published release.