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

gsftSubmit(null, g_form.getFormElement(), 'create_security_incident'); 	

Is calling another UI Action not a script include.

You need to use a GlideAjax to call the script include.

These links might be helpful:

what is the Use of gsftSubmit , why should we use it, when use it?

GlideAjax Example Cheat Sheet

AJAX (asynchronous JavaScript and XML)

Next Experience - Workspace UI Actions

I have a workspace client script calling a script include to get my on hold reason choices from the choice table and then it pops up the modal with the choices. Then it sets that value on the form. The last link above can help you with that.

 var ga = new GlideAjax('GetINCChoices');
    ga.addParam('sysparm_name', 'getOnHold');
    ga.getXMLAnswer(function(answer1) {
        var answer = JSON.parse(answer1);
        fields.push({
            type: 'choice',
            name: 'hold_reason',
            label: getMessage('On Hold Reason'),
            choices: answer.choices,
            mandatory: true
        });

        g_modal.showFields({
            title: "On Hold Reason",
            fields: fields,
            size: 'lg'
        }, {
            title: "On Hold Reason Note",
            fields: [{
                type: 'textarea',
                name: 'comment',
                label: getMessage('On Hold Reason Note'),
                mandatory: true,
                size: "lg"
            }],

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!

Hi @Dagan , 

I have a question. I've followed the steps provided but the problem I'm facing is that the popup confirmation appears when I click the UI button, but before I can press confirm, the new record already gets created.

JordyZ_0-1718936234974.png

Should I be replacing the g_form.submit with the name of my action, in this case "create_case"? When I do that, I can press confirm on the popup, but no record gets created. Should I change anything on line 8?

JordyZ_1-1718936354642.png

 



Thanks in advance!

Hi @JordyZ, thanks for your question, I'm happy to help. I noticed your action name is a bit generic. Is it possible that action name already exists elsewhere? Everything else looks correct from what I can see.

 

I actually still have access to the instance this example is on, so I can look further into your issue later today

Hi @Dagan , thanks for getting back to me! I've figured it out in the meanwhile, just a silly mistake on my part.