Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

UI Action working in Native UI but not triggering on Configurable Workspace (Problem Table)

Asish17
Tera Contributor

Hi @community

 

I have a custom UI Action on the Problem (problem) table that validates child Problem Tasks (problem_task) before allowing a state transition.

Expected Functionality:

When a user clicks the UI Action to move the Problem record to the next state, the script checks whether any associated problem_task records are still active/open:

  • If active ptask records exist -> Show an error/alert message and prevent the state change.

  • If all ptask records are closed/inactive->  Advance the Problem state to the next state.

The Issue:

This UI Action works as expected in the Native UI, but when executed from the Workspace view, the validation does not work (or the state transition fails/bypasses the check).

Could someone advise on how to properly structure this UI Action for Workspace compatibility?

  • Do I need to configure the Workspace Client Script field separately?

  • What is the best practice for performing a synchronous/asynchronous GlideRecord or g_scratchpad check on child tasks in Workspace?

Any code snippets or guidance on setting up the Client vs. Server execution for Workspace UI Actions would be greatly appreciated!

 

Regards,

Asish

6 REPLIES 6

Rakesh_M
Mega Sage

Hi @Asish17  ,

1.You need to configure the Workspace Client Script field separately.
2.Refer BLOG - Handling OnSubmit Validation Without getXMLWait() in Native UI, Workspace & Portal for implementing the server-side call using a Script Include.

Ankith Sharma
Tera Guru

Hi @Asish17 

Yes. For Configurable Workspace, use a Declarative Action. Keep the validation on the server side rather than using client-side GlideRecord/g_scratchpad.

 

Example server-side logic:

var pt = new GlideRecord('problem_task');
pt.addQuery('problem', current.sys_id);
pt.addActiveQuery();
pt.setLimit( 1 ) ;
pt.query();

if (pt.hasNext()) {
gs.addErrorMessage('Cannot move the Problem forward. Active Problem Tasks exist.');
action.setAbortAction(true);
} else {
current.state = /* next state */;
current.update();
}

So the pattern is:

Declarative Action (Workspace) > Server-side validation > Update state

No need to duplicate the validation in a Workspace Client Script unless you specifically need client-side UI behavior.

If you found this useful, feel free to mark it as Accept as Solution and Helpful. It makes my day (and helps others too 😉).

Regards,
- Ankit
LinkedIn: https://www.linkedin.com/in/sharmaankith/



Ankur Bawiskar
Tera Patron

@Asish17 

share your UI action config screenshots and scripts here

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

Hi @Ankur Bawiskar ,

 

Thank you for your reply 🙂

 

UI Action Name - Resolve

Table - problem

Action name - move_to_resolved

onClick - onResolve()

condition - current.canWrite() && (current.state != ProblemState.STATES.RESOLVED) && (current.state == ProblemState.STATES.FIX_IN_PROGRESS) && new ProblemStateUtils().validateStateTransition(current, ProblemState.STATES.RESOLVED) && (gs.hasRole('problem_manager') || gs.getUser().isMemberOf(current.assignment_group))

 

Script - 

function onResolve() {
    var number = g_form.getUniqueValue('number');
    // Check for any outstanding problem tasks
    var taskCheck = updateaction(number);
       
    if ( taskCheck == false ) {

        if (!g_form.hasField("state") || !g_form.hasField("resolution_code")) {
            getMessage('Cannot resolve the Problem as atleast one of the following fields are not visible: \'State\' \'Resolution code\'', function(msg) {
                g_form.addErrorMessage(msg);
            });
            return false;
        }
        g_form.setValue("state", g_scratchpad.STATE.RESOLVED);
        g_form.setValue("resolution_code", g_scratchpad.RESOLUTION_CODES.FIX_APPLIED);
        g_form.save();

    }
}

function updateaction(prb_num){
    // Lookup the problem tasks associated with the story to make sure there are non outstanding
    // Set the query to use during the lookup for problem tasks
    var query = 'problem.sys_id='+ prb_num +'^problem.stateIN103,104^state!=157';
    var ga = new GlideAjax('problemtask');
    ga.addParam('sysparm_name', 'problemtask');
    ga.addParam('sysparm_pro_no', prb_num);
    ga.addParam('sysparm_query', query);
    ga.getXMLWait();
    var answer = (ga.getAnswer());
    var pro = answer;
    if (pro != false) {
        getMessage('The Problem record cannot be moved to \'Resolved\' state with associated problem task(s) active.', function(msg) {
                g_form.addErrorMessage(msg);
        });
        return true;
    } else {
        return false;
    }
}