This script includes a TaskManager class for ServiceNow that automates the creation of various types of cases (Incidents, CSM Cases, and HR Cases) with AI-generated content using the NOW Assist Generate Content skill.
- ServiceNow instance with NOW Assist capabilities
- OpenAI API key
- Access to create and modify Script Includes in ServiceNow
To use the OpenAI API with ServiceNow, you need to set up your API key:
To ensure the TaskManager uses the correct AI provider:
- In the navigation filter, search for the OneExtend Capability table by entering sys_one_extend_capability.list.
- Open the record for the capability that you would like to configure, in this case we want to set a default provider for Generate Content.
- In the "OneExtend Definition Configs" related list, set OpenAI as the default provider.
- Save your changes.
- https://docs.servicenow.com/bundle/xanadu-intelligent-experiences/page/administer/generative-ai-controller/task/configure-a-provider-for-a-generative-ai-capability.html
- In your ServiceNow instance, navigate to System Definition > Script Includes.
- Click "New" to create a new Script Include.
- Set the following fields:
- Name: TaskManager
- API Name: global.TaskManager
- Client callable: false
- Active: true
- Copy the entire TaskManager code into the Script field.
- Click "Submit" to save the Script Include.
You can use the TaskManager in various ServiceNow server-side scripts. Here are some examples:
var taskManager = new TaskManager();
taskManager.createCase('incident', '<Your short description>');var taskManager = new TaskManager();
var changeRequestSysId = taskManager.createCase('change_request');var taskManager = new TaskManager();
var claimSysIds = taskManager.createCase('healthcare_claim');var taskManager = new TaskManager();
var claimSysIds = taskManager.createCase('healthcare_claim', null, 5); // Generates 5 claims(function executeRule(current, previous /*null when async*/) {
var taskManager = new TaskManager();
taskManager.createCase('csm_case', current.short_description);
})(current, previous);var taskManager = new TaskManager();
taskManager.createCase('hr_case', 'Annual performance review process initiation');function onExecute() {
var taskManager = new TaskManager();
taskManager.createCase('incident', g_form.getValue('short_description'));
return false; // Prevent form submission if needed
}(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var taskManager = new TaskManager();
var caseType = request.queryParams.case_type;
var shortDescription = request.queryParams.short_description;
if (caseType && shortDescription) {
taskManager.createCase(caseType, shortDescription);
response.setStatus(201);
response.setBody("Case created successfully");
} else {
response.setStatus(400);
response.setBody("Missing required parameters");
}
})(request, response);You can customize the TaskManager by modifying the following:
- Update the
sys_idconstants at the top of the script to match your ServiceNow instance's record system IDs. - Modify the
_createIncident,_createCSMCase, and_createHRCasemethods to include additional fields or logic specific to your needs. - Adjust the prompt templates in the
_generateEntriesmethod to generate different types of content.
If you encounter issues:
- Check the ServiceNow system logs for any error messages.
- Verify that your OpenAI API key is correctly configured and has sufficient credits.
- Ensure the Generate Content skill is properly set up with OpenAI as the default provider.
- Double-check that all required fields are being populated when creating cases.
Feel free to fork this project and submit pull requests with any enhancements or bug fixes. Please ensure you follow ServiceNow best practices and coding standards.
This project is licensed under the MIT License - see the LICENSE file for details.