Make Additional Comments Mandatory when user click on the Reject Button

suresh40
Tera Contributor

Hi All,

 

I want to make comments mandatory while click on the reject Button. I have written script Comments are getting mandatory when clicking on the button But is not rejecting the request. Please help me on this .

 

 

function RejectButton() {
    if (g_form.getValue('comments') == "") {
        g_form.setMandatory('comments', true);
        gsftSubmit(null, g_form.getFormElement(), 'reject_action');
    }
    if (typeof window == 'undefined')
        reject();
 
    function reject() {
        current.update();
        var currentRec = current.sys_id;
        var gr = new GlideRecord('sysapproval_approver');
        gr.addQuery('sysapproval', currentRec);
        gr.addQuery('state', 'requested');
        gr.query();
 
        while (gr.next()) {
            gr.state = 'rejected';
            gr.update();
 
            action.setRedirectURL(current);
 
        }
    }
}

  

2 REPLIES 2

SwarnadeepNandy
Mega Sage

Hello @suresh40,

 

One option is to use a UI policy instead of a client script to make the comments field mandatory when rejecting the request. You can create a UI policy with the following conditions:

  • Table: sysapproval_approver
  • Field: comments
  • Mandatory: true
  • Reverse if false: true
  • On load: false
  • Condition: state=requested

This way, the comments field will only be mandatory when the state is requested, and the reject button will not disappear. You can also add a UI policy action to show a message to the user when they try to reject without comments. You can find more information about UI policies here.

Another option is to modify your client script to use g_form.submit instead of gsftSubmit. This will ensure that the form is submitted with the reject action and the comments are validated. You can also use g_form.addErrorMessage to display a message to the user when they try to reject without comments. Your script could look something like this:

 

function RejectButton() {
    if (g_form.getValue(‘comments’) == “”) {
        g_form.setMandatory(‘comments’, true);
        g_form.addErrorMessage(‘Please enter comments before rejecting’);
        return false;
    } else {
        g_form.submit(‘reject_action’);
    }
}

 

You can find more information about g_form.submit here.

 

Kind Regards,

Swarnadeep Nandy

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello @suresh40 ,

Making Additional Comments Mandatory when user click on the Reject Button is a part of client side script in the reject UI action.

Just check the condition if the addiditional commnets are empty and add an alert inside the loop alond with it return false someting like below:

 if (g_form.getValue('comments') == "") {
       alert('please add commnets');
      return false;
    }
If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.