Workspace Experience - UI Action with 'g_modal.confirm()'

Dagan
Kilo Guru

I am trying to add to a custom workspace a UI Action that will pop a confirmation modal before creating a security incident and I have made it work in UI16 by having a server side version of the UI Action that performs the work and a client side version that provides a confirmation message, however I cannot seem  to translate that into a workspace UI Action.

 

I can either pop a modal which does nothing, or I can create the record, but I cannot figure out how to do both.

My server side action simply uses an API to create the security incident from the current ITSM incident:

var securityIncident = (new SecurityIncidentUtils()).createFromIncident(current);

No form button, workspace form button, etc.

 

The client side is a form button, client box is checked, and workspace form button,

onClick we call confirmSecurityIncident(), which does this:

function confirmSecurityIncident() {
	//Pop a confirmation modal prompting the user to confirm they want to open an SIR record
    var answer = confirm('Please confirm you would like to open a security incident on ' + g_form.getValue('number') + '?');

    if (answer) {
		//calls the create_security_incident action that represents the server side code.
        gsftSubmit(null, g_form.getFormElement(), 'create_security_incident'); 	
    } else {
        return false;
    }
}

and the workspace client script looks like so:

function onClick (g_form){
	
	
	if ( !g_modal.confirm('Please confirm you would like to create a security incident from this ticket') )
		return;	
	
	g_form.submit( 'create_security_incident' );
}

 

The modal shows up, but the console logs say "(g_form) [NOACTION] Could not find UI Action: create_security_incident"

I assume that is because it is not a form button, however I do not want two form buttons and need the second one to have my confirmation on UI16

I was trying to follow the example set forth on this community post.

 

Any help would be greatly appreciated

 

1 ACCEPTED SOLUTION

Dagan
Kilo Guru

Almost two years later, I have found a solution using the referenced links below:

 

Client and Server side code in a single UI action 

UI Actions in Workspaces 

Modeling our good friend GeoffreyOptumOp, it our form looks like this.

Dagan_0-1702324966746.png

1.  Check the "Client" checkbox.  The UI action is going to start with a modal confirmation.

2.  Different from what the other post says, this will actually be the onClick() function from the workspace section since we aren't using UI16 this time.

3.  Give your action a name.  You will need to reference this name in the script itself.  Basically, the UI Action script will "call itself", but the 2nd time it is invoked, it will be run on the server, not the client.

 

The script in the case of a workspace button will be all server code. This is what will run when the workspace calls the action a second time.

 

//If this code is being run in the context of the SERVER...
if (typeof window == 'undefined') {
    //Use only Server-Side objects and functions in this IF block
    //and in the functions called from this IF block.
    //gs.addInfoMessage("4.  UI Action Script running Server Side.");
    runServerSideCode();
}

function runServerSideCode() {
  gs.addInfoMessage("5.  Server Side function Activated.");
	
	//TBD.  Place your custom code Server-Side code HERE,
	//using only server-side objects such as "current" and "GlideRecord".
	
	gs.addInfoMessage("6.  Server Side Code COMPLETE.");
}

 

 

And lastly, our workspace form. This is where the second link from Ashley Snyder comes into play. The important part here is that instead of gsftSubmit() you will want to use g_form.submit("[action_name]");

 

function onClick(g_form) {
//alert("1.  UI Action clicked.  Client Side Code Activated.");
	var prompt = "Do you want wish to continue?";
	
	var bConditionPassed = g_modal.confirm(prompt);
	if (! bConditionPassed)
	{		
		return false;
	}
	var actionName = g_form.getActionName();
	g_form.submit(actionName);
		
}

 

And now you can use modals and server code in one UI action! It only took two years to figure out, haha!

View solution in original post

12 REPLIES 12

Sandhya Bellann
ServiceNow Employee
ServiceNow Employee

Please check below KB Article if it helps:

https://support.servicenow.com/kb?id=kb_article_view_popup&sysparm_article=KB0743790

The article pulled a 404. Think there's an extra '_popup' after article_view in your link:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0743790

 

Unfortunately, that doesn't seem to help here, so sorry

 

Cheri M
Kilo Sage

Hi Dagan,

In case you have not solved this yet, I thought I would respond.
I went ahead and put my actions in the script instead of trying to call the other action (I couldn't get it to work either).  This is what I have that is working:

function onClick(g_form) {
    var msg = getMessage("Are you sure you want to set this task to Close Incomplete?");
    g_modal.confirm(getMessage("Confirmation"), msg, function(confirmed) {
        if (confirmed) {
            g_form.setValue('state', 4);
            g_form.save();
        }
    });

    return false;
}

Hi Cheri,

 

It appears you are using the g_form object to do your work, but I'm trying to call a script include as part of my action. I replaced the content inside the if(confirmed) with 'return true;', however that never triggers a modal. Any ideas?