Console and Workspace sync issue

anuradhav-k
Tera Contributor

In servicenow we have a custom application called Onsite in that i have a table called PP[Prevention Plan] and when try to renew the PP record by renew button in console the old PP gets inactive and open a new record automatically but in workspace its not the same. When I click on renew button the old gets inactive stays the new records will not open. I need to go back and refresh the page then I have to open a new created records. Please give me solution to sync the console and workspace.

-----------------------------------------

In UI action script we are using :- 

 

function onClickClientside() {
    //Call the UI Action and skip the 'onclick' function
    gsftSubmit(null, g_form.getFormElement(), 'renew_pp');
}

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
    serverSide();

function serverSide() {
    var ppUtils = new PreventionPlanHelper();
    var returnOfRenewal = ppUtils.renewPP(current);
    action.setRedirectURL("/x_aif_onsite_airb_prevention_plan.do?sys_id=" + returnOfRenewal.newPP);
    gs.addInfoMessage(returnOfRenewal.message);
}
------------------------------------------------------- 
And Workspace Client Script:-
 
function onClick(g_form) {
    //Call the UI Action and skip the 'onclick' function
    g_form.submit("renew_pp");
}
----------------------------------------
The same script is working for console & workspace both, but the issue in workspace is that it will deactivate  the existing PP and stay on the deactivated PP. To get the new PP I need to go back and refresh and open the record. But in console it works fine

 

1 REPLY 1

Pavan Srivastav
ServiceNow Employee

The root cause is clear. action.setRedirectURL() is a platform/console concept — it has no effect in a UI Builder-based workspace. The workspace ignores that redirect entirely, which is why it stays on the deactivated record.


Why It Works in Console But Not Workspace

Layer Console Workspace
Navigation action.setRedirectURL() works natively Completely ignored
Client script gsftSubmit() handles redirect response g_form.submit() submits but discards redirect
Page routing Platform handles URL change Must use workspace navigation APIs

The Fix

You need to handle navigation on the client side in the workspace after the server-side completes. The cleanest approach is to return the new record's sys_id from server side and navigate to it using the workspace navigation API.

Step 1 — Pass the new sys_id back via action.setReturnValue()

In your server-side code, add a return value alongside the existing redirect (keep the redirect for console compatibility):

 
 
javascript
function serverSide() {
    var ppUtils = new PreventionPlanHelper();
    var returnOfRenewal = ppUtils.renewPP(current);
    
    // Keep this for console — still works there
    action.setRedirectURL(
        "/x_aif_onsite_airb_prevention_plan.do?sys_id=" + returnOfRenewal.newPP
    );
    
    // Add this — passes new sys_id back to workspace client side
    action.setReturnValue(returnOfRenewal.newPP);
    
    gs.addInfoMessage(returnOfRenewal.message);
}

Step 2 — Update the Workspace Client Script to navigate after submit

 
 
javascript
function onClick(g_form) {
    g_form.submit("renew_pp", function(returnValue) {
        // returnValue is what action.setReturnValue() passed back
        if (returnValue) {
            var newSysId = returnValue;
            var tableName = 'x_aif_onsite_airb_prevention_plan';
            
            // Workspace navigation — opens the new PP record
            var url = '/now/workspace/agent/record/' + tableName + '/' + newSysId;
            window.location.assign(url);
        }
    });
}

⚠️ The callback parameter in g_form.submit() is supported in workspace client scripts to receive the server-side return value. This is the key difference from console behaviour.


Step 3 — If g_form.submit() callback doesn't fire (fallback option)

Some versions of workspace don't support the submit callback cleanly. In that case use a GlideAjax call instead to get the new sys_id directly from client side:

 
 
javascript
function onClick(g_form) {
    // First submit to trigger the server-side renewal
    g_form.submit("renew_pp");
    
    // Then query for the newest active PP linked to this record
    // Give server-side a moment to complete
    setTimeout(function() {
        var ga = new GlideAjax('PreventionPlanHelper');
        ga.addParam('sysparm_name', 'getLatestActivePP');
        ga.addParam('sysparm_current_sys_id', g_form.getUniqueValue());
        ga.getXMLAnswer(function(answer) {
            if (answer) {
                var tableName = 'x_aif_onsite_airb_prevention_plan';
                var url = '/now/workspace/agent/record/' + tableName + '/' + answer;
                window.location.assign(url);
            }
        });
    }, 2000); // adjust timing if needed
}

This requires adding a getLatestActivePP client-callable method to your PreventionPlanHelper Script Include:

 
 
javascript
getLatestActivePP: function() {
    var parentSysId = this.getParameter('sysparm_current_sys_id');
    var gr = new GlideRecord('x_aif_onsite_airb_prevention_plan');
    gr.addQuery('previous_pp', parentSysId); // adjust field name to match your schema
    gr.addQuery('active', true);
    gr.orderByDesc('sys_created_on');
    gr.setLimit(1);
    gr.query();
    if (gr.next()) {
        return gr.getUniqueValue();
    }
    return '';
}

Recommended Approach

Go with Step 1 + Step 2 first (the setReturnValue + submit callback pattern) as it is the least invasive change. Only fall back to Step 3 if the callback doesn't fire reliably in your instance version.

Approach Console impact Workspace fix Complexity
Step 1+2 setReturnValue None Clean Low
Step 3 GlideAjax fallback None Works Medium
Modify server side only None Won't work