action.setRedirectURL(current) is not working with List context menu UI action

Nithin_kumar
Tera Contributor

Hello experts,

 

I am trying to create a List context menu UI action; Comments should be mandatory when rejecting an approval from List context menu. I have tried the below but, when I say 'action.setRedirectURL(current)' it should be redirected to Approval record, but it is on the same change request form. Could you please help me on this?

 

Name: Reject

Table: Approval [sysapproval_approver]

List Context Menu: Checked

Condition: ootb

Script:

current.state = 'rejected';

if (!gs.nil(current.comments.getJournalEntry(-1))) {
current.update();
new ApprovalUserFeedback().rejected(current);
} else {
gs.addErrorMessage(gs.getMessage("Comments are required when rejecting an approval"));
current.state = 'requested';
current.setAbortAction(true);
action.setRedirectURL(current);
}

 

Nithin_kumar_0-1672248340716.png

Nithin_kumar_1-1672248373354.png

 

4 REPLIES 4

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Nithin_kumar 

 

To redirect to the approval record after the "Reject" UI action is executed, you can try the following changes to your script:

 

1. After setting the current.state to 'rejected', retrieve the approval record related to the current approval task using the current.getRelatedRecord() function. For example:

 

current.state = 'rejected';

var approval = current.getRelatedRecord();

 

 

  1. Instead of using action.setRedirectURL(current), use action.setRedirectURL(approval) to redirect to the approval record.

 

 
if (!gs.nil(current.comments.getJournalEntry(-1))) { current.update(); new ApprovalUserFeedback().rejected(current); action.setRedirectURL(approval); } else { gs.addErrorMessage(gs.getMessage("Comments are required when rejecting an approval")); current.state = 'requested'; current.setAbortAction(true); action.setRedirectURL(current); }

 

This should redirect to the approval record after the "Reject" UI action is executed, rather than staying on the same change request form. 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Nithin_kumar
Tera Contributor

Hi @Gunjan Kiratkar, Thank you for your suggestion.

 

I tried the above code, which is not working, throwing the below as redirection undefined.

Nithin_kumar_0-1672299350421.png

 

So, I tried the below code, and it is working only for successful condition. If I try the same approach in else condition, it reloading same Change request form only.

 

current.state = 'rejected';
//var approval = current.getRelatedRecord();
id = current.sys_id;

if (!gs.nil(current.comments.getJournalEntry(-1))) {
    current.update();
    new ApprovalUserFeedback().rejected(current);
    if (typeof window == 'undefined')
        redirectHere(); //change to next stage

} else {
    gs.addErrorMessage(gs.getMessage("Comments are required when rejecting an approval"));
    current.state = 'requested';
    current.setAbortAction(true);
}

function redirectHere() {
    action.setRedirectURL('/nav_to.do?uri=sysapproval_approver.do?sys_id='+id);
}

 

But I need this redirection for else condition, when the approver is rejecting without comments, we need to redirect to the approval record to add the comments.

ghouse
Tera Contributor

hi nithin,

              I have the similar requirement , did you find an way to redirect to the approval record when the approval is rejected. 

ghouse
Tera Contributor

@Nithin_kumar  please reply if you have the solution , thanks.