Use Script Includes and Confirm Popup from UI Action

rbazua
Kilo Contributor

So I have this complex requirement from a simple UI Action, and I'm having problems because it needs to call some complex code from Script Includes API and open Confirm Popups for the user.

The Workflow:

  1. User clicks the button
  2. UI Action calls some complex   Script Includes to execute validations and other process, I am not the author of those SI API but I need to execute their process.
  3. After the SI finishes, I have to check the results and proceed according to the results.
  4. For one of the conditions, I need to pop up a confirm button with OK/Cancel for the user. If OK is selected, update the record state, if Cancel is selected, abort the process.

The Problem:

Server Side code is executed, which needs to execute a client side Confirm, and according to the confirm execute more Server Side stuff.

The confirm() is a javascript client sided thing, and the Script Include is server side. The mix of both is giving me a hard time.

If I check the Client checkbox in the UI Action, I have to wrap it all in a function called from the onClick(). If I do that, the Script Includes are not defined and it doesn't work.

There's a trick to execute first some Client code and then execute the Server code.

How can I do something like this?

Execute Server side SI,

ask the client for confirmation according to the SI results,

execute more Server side updates according to the user selection.

Thanks

1 ACCEPTED SOLUTION

Jon Barnes
Kilo Sage

You will want to create a new Client Callable Script include as sort of a wrapper to your main script include. Then you can interact via the GlideAjax class on the client side with your new client callable script, which will communicate with the client as it gets the results.



for example, if your current script include is called Script1, you will create a new script include called Script1Ajax like this:



var Script1Ajax = Class.create();


Script1Ajax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


 


  step1: function() {


      var parm = this.getParameter('sysparm_whatever');


      var ret = new Script1().runStep1(parm);


      // I like to work with JSON, so I usually convert objects to JSON


      // if applicable


      return new JSON().encode(ret);


  },


 


  step2: function() {


      var parm = this.getParameter('sysparm_whatever');


      var ret = new Script1().runStep2(parm);


      return new JSON().encode(ret);


  },


 


  type: 'Script1Ajax'


});



Then your UI Action would be client side, and you would call your new script like this:



function uiActionClicked() {


  var ga = new   GlideAjax('Script1Ajax');


  ga.addParam('sysparm_name', 'step1');


  ga.addParam('sysparm_whatever', 'anything you need to pass to the server');


  ga.getXMLAnswer(step1Complete);



  function step1Complete(ans) {


      var resp = JSON.parse(ans);


      if (resp.someConditionIsTrue) {


          if (confirm('Are you sure?')) {


              var ga1 = new   GlideAjax('Script1Ajax');


              ga1.addParam('sysparm_name', 'step2');


              ga1.addParam('sysparm_whatever', 'anything you need to pass to the server');


              ga1.getXMLAnswer(step2Complete);


          }


      }  


  }



  function step2Complete(ans) {


      var resp = JSON.parse(ans);


      // from here you can do what you need to do with the results


  }


}



I didn't test this out, so I apologize if there are any syntax errors but hopefully this gives you an idea of how to interact with your pre-existing SI.


View solution in original post

1 REPLY 1

Jon Barnes
Kilo Sage

You will want to create a new Client Callable Script include as sort of a wrapper to your main script include. Then you can interact via the GlideAjax class on the client side with your new client callable script, which will communicate with the client as it gets the results.



for example, if your current script include is called Script1, you will create a new script include called Script1Ajax like this:



var Script1Ajax = Class.create();


Script1Ajax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


 


  step1: function() {


      var parm = this.getParameter('sysparm_whatever');


      var ret = new Script1().runStep1(parm);


      // I like to work with JSON, so I usually convert objects to JSON


      // if applicable


      return new JSON().encode(ret);


  },


 


  step2: function() {


      var parm = this.getParameter('sysparm_whatever');


      var ret = new Script1().runStep2(parm);


      return new JSON().encode(ret);


  },


 


  type: 'Script1Ajax'


});



Then your UI Action would be client side, and you would call your new script like this:



function uiActionClicked() {


  var ga = new   GlideAjax('Script1Ajax');


  ga.addParam('sysparm_name', 'step1');


  ga.addParam('sysparm_whatever', 'anything you need to pass to the server');


  ga.getXMLAnswer(step1Complete);



  function step1Complete(ans) {


      var resp = JSON.parse(ans);


      if (resp.someConditionIsTrue) {


          if (confirm('Are you sure?')) {


              var ga1 = new   GlideAjax('Script1Ajax');


              ga1.addParam('sysparm_name', 'step2');


              ga1.addParam('sysparm_whatever', 'anything you need to pass to the server');


              ga1.getXMLAnswer(step2Complete);


          }


      }  


  }



  function step2Complete(ans) {


      var resp = JSON.parse(ans);


      // from here you can do what you need to do with the results


  }


}



I didn't test this out, so I apologize if there are any syntax errors but hopefully this gives you an idea of how to interact with your pre-existing SI.