Custom UI action button in Knowledge Management

akchauhan
Tera Contributor

I have created the UI action button as close_rejected for Knowledge Mgmt. When the Knowledge Article comes to review for Knowledge Manager and he finds that KA is not good enough so he can reject the article and no one one can action on that.  I have created below Server side script for this UI action . Knowledge article moving to close_rejected state but only in draft stage and not in other stage. also Knowledge article is not moving in read only when I am clicking on close_rejected. I have created Client side On Load script to move the form to read only. Also I am trying through UI Policy where I am putting the condition as workflow is close_rejected and then creating UI action for all the field in the form to make them read only. Please assist how to resolve this issue. backend Value of workflow choice - close_rejected

 

 

 

// Server-side Script for UI Action
(function executeAction(current) {
    // Set workflow state to close_rejected
    current.workflow_state = 'close_rejected';
    current.update();
    // Optional: if you also track article state separately
    // current.article_state = 'rejected';
    // current.update();
 
    // Add info message to show user
    gs.addInfoMessage("This knowledge article has been closed as rejected and is now read-only.");
 
    // Redirect back to the article (refresh form)
    action.setRedirectURL(current);
 
})(current);
 
Client script on Load to move the form to read only
function onLoad() {
  var state = g_form.getValue('workflow_state');
  if (state === 'close_rejected') {
    var fields = g_form.getEditableFields();
    for (var i = 0; i < fields.length; i++) {
      g_form.setReadOnly(fields[i], true);
    }
  }
}
 
 
akchauhan_1-1757995771169.png

UI Policy action

akchauhan_0-1757995717717.png

 

3 REPLIES 3

SP22
Mega Sage
Mega Sage

Hello @akchauhan ,

/ Server-side Script for UI Action

(function executeAction(current, action) {
    // Only proceed if not already in close_rejected
    if (current.workflow_state !== 'close_rejected') {
        current.workflow_state = 'close_rejected';
        // Optional: Set article state if you use a separate field
        // current.article_state = 'rejected';
        current.update();
        gs.addInfoMessage("This knowledge article has been closed as rejected and is now read-only.");
    } else {
        gs.addInfoMessage("This knowledge article is already in the rejected state.");
    }
    action.setRedirectURL(current);
})(current, action);


 Client script on Load to move the form to read only

 

function onLoad() {
    var state = g_form.getValue('workflow_state');
    if (state === 'close_rejected') {
        var fields = g_form.getFieldNames();
        for (var i = 0; i < fields.length; i++) {
            // Optionally skip mandatory system fields like sys_id, etc.
            if (fields[i] !== 'sys_id') {
                g_form.setReadOnly(fields[i], true);
            }
        }
    }
}

 

Thanks
Santosh.p

Ankur Bawiskar
Tera Patron
Tera Patron

@akchauhan 

so what's not working?

what debugging did you do?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Dear Ankur, 

As I have mentioned above that I have tried client script, UI policy and even Business rule also I have applied to run this UI action button. but nothing working for me and moreover this is first time to get into this kind of issues . hence not able to understand what went wrong. Please assist.