ServicePortal call server from client controller?

dmfranko
Kilo Guru

Is it possible to call the server code from the client controller?   I want to be able to do this so I can poll in widget I'm creating.   I know I can do this by polling a REST service, but I'd rather use what's within the widget if possible.

1 ACCEPTED SOLUTION

tltoulson
Kilo Sage

Hi Dan,



Check out this doc: Client and server scripting



From the Client Controller you can use c.server.update() (assuming you are using the controller as syntax).   This will then call the Server Script in the widget.   Of course the issue here is that the Server Script is then called both on request and again on AJAX calls.   To distinguish, add an if check in the Server Script for the input variable which contains the AJAX data.



Server Script Example



if (input) {


      // AJAX from c.server.update()


}


else {


      // Initial load


}




If you check existing widgets, you will find this if (input) pattern.



Kind regards,



Travis


View solution in original post

6 REPLIES 6

tltoulson
Kilo Sage

Hi Dan,



Check out this doc: Client and server scripting



From the Client Controller you can use c.server.update() (assuming you are using the controller as syntax).   This will then call the Server Script in the widget.   Of course the issue here is that the Server Script is then called both on request and again on AJAX calls.   To distinguish, add an if check in the Server Script for the input variable which contains the AJAX data.



Server Script Example



if (input) {


      // AJAX from c.server.update()


}


else {


      // Initial load


}




If you check existing widgets, you will find this if (input) pattern.



Kind regards,



Travis


Yup.   That totally works and actually was what I was doing.   I was having a problem somewhere else.


Hi Dan- Can you please let me know how you have implemented polling mechanism? I have similar requirement where I am displaying the list through REST call and I have to poll this so that widget keep refreshing at particular interval.


I noticed this does in fact call twice, can you tell me the difference between the server.update() and the server.get() methods. I noticed with the get() method it doesn't call twice and seems to be a nicer syntax:


  c.deleteTask = function(sys_id) {


            c.server.get({


                      action: "deleteTask",


                      sysId: sys_id


            }).then(function(response) {


  });


You get a lot more back on the response too.



Cheers



Martin