Mobile : Create Change Task from a Change Request form

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2023 03:55 PM
Hey guys,
I'm building a change request application on Now Agent and going crazy here.
I've got a Change Request applet built that shows the list and form too.
I need an Action and Function that will create a Change Task directly from a Change Request form. (Just want to set the short description, description and Change Request sysid on the chg task)
I've created the Action and Function. It shows up on my Change Request form too.
But when I select this function, the form opens with Short description and Description prefilled with the Change Request's short description and description, and upon submit, errors out.
Any ideas?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2023 10:18 PM
HI @AnirudhKumar ,
I trust you are doing great.
To create a Change Task directly from a Change Request form in the Now Agent application, you can follow these steps:
Create an Action and Function in ServiceNow. Make sure the Action is associated with the Change Request form.
- The Action should have a label that represents its purpose, such as "Create Change Task."
- The Function should contain the code logic to create a new Change Task record with the desired fields.
In the Function code, you can use the following snippet to create a new Change Task record and populate the required fields with values from the Change Request form:
function createChangeTask() {
var changeRequestSysId = g_form.getUniqueValue(); // Get the sys_id of the current Change Request form
var shortDescription = g_form.getValue('short_description'); // Get the short description from the Change Request form
var description = g_form.getValue('description'); // Get the description from the Change Request form
var changeTask = new GlideRecord('change_task'); // Create a new GlideRecord object for the Change Task table
changeTask.initialize(); // Initialize the new record
// Set the values for the fields in the new Change Task record
changeTask.setValue('short_description', shortDescription);
changeTask.setValue('description', description);
changeTask.setValue('change_request', changeRequestSysId);
var changeTaskSysId = changeTask.insert(); // Insert the new Change Task record into the database
if (changeTaskSysId) {
// Success! Display a confirmation message to the user
gs.addInfoMessage('Change Task created successfully: ' + changeTaskSysId);
} else {
// Oops! Display an error message to the user
gs.addErrorMessage('Failed to create Change Task');
}
// Redirect the user back to the Change Request form
action.setRedirectURL(changeRequestSysId);
}
- Save the Function and Action in ServiceNow.
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi