The CreatorCon Call for Content is officially open! Get started here.

automatically insert comment from UI action button on requested item

TMKAM
Tera Contributor

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();
}"

2 REPLIES 2

Community Alums
Not applicable

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:

  1. 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.
  2. 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();

 

})();

msd93
Kilo Sage

Hi @TMKAM 

 

Please try the below code:

function reject(){
    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 is cancelled by Requester'//update comments into requested item
current.state = '4';//closed incomplete
current.update();
current.setWorkflow(false);// Disable workflow for the current record

}
 
msd93_0-1695186594549.png

This code works for me and updates the state and comments.

 

Hope this helps you.