0

I am working on a widget, and I use data/input variables to communicate between the Client script to Server script.

for ex., in the Client Script I use:

c.data.action= "removeEvent";
c.server.update().then(function(){ c.data.action = undefined;});

and I am waiting that action in the Server Script with:

if(input.action == "removeEvent"){
//some code here...
}

My problem is that once I completed that action (in the Server Script) I need to go back to my Client Script to updated the data that is showed to the user and trigger other functions. So, how can I know from the Client Script that the code in the Server Script has ended?

I try to use the data variable again like:

// In Server Script
data.finished = true;

//In client Script
if(data.finished ) {
//do something
}

But, the Client Script doesn't update.

Is there a way to do it with a watcher or subscribe on a variable so when it changes I know that the Server Script has finished?

Thanks all for your help!

 

 

DarkBee
  • 16,592
  • 6
  • 46
  • 58
mrbangybang
  • 683
  • 1
  • 9
  • 22

2 Answers2

0

To an extent this will depend on your client - Whether the client is service portal, platform UI, seismic+UIB page etc.

In general you have 2 options in this case

  1. Record watcher: https://developer.servicenow.com/dev.do#!/learn/learning-plans/quebec/servicenow_application_developer/app_store_learnv2_serviceportal_quebec_recordwatch OR
  2. Flow designer: Create a flow which will watch the server script or side-effect of server script like change to a record in database and based on that inject an action which will trigger refresh on the client UI.

In either case the client will need a callable hook that can be called by the server backend.

Milind Gokhale
  • 575
  • 2
  • 14
0

I ended up using GlideAjax and Scripts Includes that allows you to create functions that have return values for asynchronous or synchronous calls.

Exemple:

First, create the Ajax Call and their return function:

var ga = new GlideAjax("ucd_GetLocationData");
ga.addParam("sysparm_name", "getCampus");
ga.addParam("sysparm_buildingid", g_form.getValue("u_building"));
ga.getXML(updateCampus);


function updateCampus(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var clearvalue; // Stays Undefined

if (answer) {
     var returneddata = JSON.stringify(answer);
     g_form.setValue("campus", returneddata.sys_id, returneddata.name);
   } else {
     g_form.setValue("campus", clearvalue);
   }
}

Finally, create a Server Script (Script Include):

var ucd_GetLocationData = Class.create();

ucd_GetLocationData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCampus: function() {
    var buildingid = this.getParameter("sysparm_buildingid");
    var loc = new GlideRecord("cmn_location");
    if (loc.get(buildingid)) {
        var campus = new GlideRecord("cmn_location");
        if (campus.get(loc.parent)) {
            var json = new JSON();
            var results = {
                sys_id: campus.getValue("sys_id"),
                name: campus.getValue("name")
            };

            return json.parse(results);
        }
    } else {
        return null;
    }
}
});

Check the tutorial from here:

https://sn.jace.pro/getting-started/GlideAjax/

mrbangybang
  • 683
  • 1
  • 9
  • 22