automatically insert comment from UI action button on requested item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 06:53 PM - edited 09-19-2023 06:54 PM
I have created an UI Action button with the scripts as per below.
My problem is I want to insert the comment automatically when Service Desk agent press the "Cancel Request" UI Action button in the requested item, but it did not insert the comment in the scripts.
UI Action scripts with the insert comment.
"function reject() {
//g_form.setMandatory('comments', 'true');
var answer = confirm("Are you sure you want to Cancel Request?");
if (answer == true) {
gsftSubmit(null, g_form.getFormElement(), 'cancel_request'); //MUST call the 'Action name' set in this UI Action
} else {
return false;
}
}
if (typeof window == 'undefined') {
current.comments = ('The Request has cancelled by Requester'); //update comments into requested item
current.state = '4';
current.approval = 'Cancelled';
//sysapproval_approver.state = 'not_required';
//current.comments = "Cancel Request by Requester"; //update close notes
new Workflow().cancel(current); //cancel the workflow
current.setWorkflow(false);
current.update();
}"

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 08:05 PM
It sounds like you're facing an issue with your UI Action button script in ServiceNow. To automatically insert a comment when the "Cancel Request" UI Action button is pressed, you'll need to modify your script. Here's a general outline of what you should do:
- Ensure you have the necessary permissions: Make sure that the user who presses the "Cancel Request" button has the appropriate permissions to update the requested item or record.
- Edit the UI Action script: In your UI Action script, you should include code to insert a comment. The exact code will depend on the scripting language you're using (JavaScript, server-side script, etc.). Here's a simplified example in Javascript :
(function() {
// Get the current record (requested item)
var currentRecord = g_form.getRecord();
// Insert a comment
currentRecord.comments = "Request canceled by Service Desk agent.";
// Update the record
currentRecord.update();
// Refresh the form or perform any necessary actions
g_form.submit();
})();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 10:10 PM
Hi @TMKAM
Please try the below code:
This code works for me and updates the state and comments.
Hope this helps you.