Service Portal Widget client script to call server script function and handle returned value

xiaix
Tera Guru

All I'm trying to do is call the checkin_using_ess_button() function, which resides in the server script, and handle the data.

HTML:

<button ng-click="checkin()">Checkin</button>

Client:

function($scope)

{

      var c = this;

      $scope.checkin = function()

      {            

              var result = c.server.checkin_using_ess_button();

              if (result == "success")

                      alert("1");

              else if (result == "already")

                      alert("2");

              else if (result == "notfound")

                      alert("3");

              else if (result == "error")

                      alert("4");

              else

                      alert("something went waaaaay wrong");

      };

}

Server:

(function() {

      /* populate the 'data' object */

      /* e.g., data.table = $sp.getValue('table'); */

})();

if (input)

{

      function checkin_using_ess_button()

      {

              var bcp = new GlideRecord('u_bcp_site_emergencies');

              bcp.addNotNullQuery('u_alert_message');

              bcp.addQuery('u_location', gs.getUser().getLocation());

              bcp.addQuery('u_active', true);

              bcp.query();

              if (bcp.next())

              {

                      var bcp2 = new GlideRecord('u_bcp_emergencies_log');

                      bcp2.addQuery('u_location', gs.getUser().getLocation());

                      bcp2.addQuery('u_employee', gs.getUserID());

                      bcp2.addQuery('u_site_emergency', bcp.sys_id);

                      bcp2.addQuery('u_checked_in', 'false');

                      bcp2.query();

                      if (bcp2.next())

                      {

                              bcp2.setValue('u_checked_in', 'true');

                              bcp2.update();

                              return "success";

                      }

                      else

                              return "already";

              }

              else

                      return "notfound";

              return "error";

      }

}

Some of my ideas on how this might work is from here: ServicePortal call server from client controller?

All I'm trying to do is call the checkin_using_ess_button() function, which resides in the server script, and handle the data.

6 REPLIES 6

James_Neale
Mega Guru

Hi David,



The c.server object is only used to call c.server.update() which performs the AJAX call and refreshes the whole data object.



I haven't really tested this, but you should find this does what you're looking for.


I've modified the function slightly so users can only check-in once, but can see if they are checked in as well.



HTML:


<button ng-click="c.checkin()" class="btn"


ng-class="{'btn-default': !c.data.is_checked_in, 'btn-success': c.data.is_checked_in}"


ng-disabled="!c.data.can_checkin || c.data.is_checked_in">


      {{c.data.button_text}}


</button>



Client:


function($scope)


{


      var c = this;


      c.checkin = function() {


              if (c.data.is_checked_in) return;


              c.server.update();


      };


}



Server:


(function () {


      var location_id = gs.getUser().getLocation(),


              bcp, bcp2;


     


      data.button_text = 'No Emergency';


      data.is_checked_in = false;


      data.can_checkin = false;



      bcp = new GlideRecord('u_bcp_site_emergencies');


      bcp.addNotNullQuery('u_alert_message');


      bcp.addQuery('u_location', location_id);


      bcp.addQuery('u_active', true);


      bcp.setLimit(1);


      bcp.query();



      if (bcp.next())


      {


              bcp2 = new GlideRecord('u_bcp_emergencies_log');


              bcp2.addQuery('u_location', location_id);


              bcp2.addQuery('u_employee', gs.getUserID());


              bcp2.addQuery('u_site_emergency', bcp.sys_id);


              bcp2.addQuery('u_checked_in', 'false');


              bcp2.setLimit(1);


              bcp2.query();



              if (bcp2.next())


              {


                      if (bcp2.u_checked_in == false)


                      {


                              data.button_text = 'Check In';


                              data.can_checkin = true;


                      }


                      else {


                              if (input)


                              {


                                      bcp2.setValue('u_checked_in', 'true');


                                      bcp2.update();


                              }


                              data.button_text = 'Checked In';


                              data.is_checked_in = true;


                      }


              }


      }


})();



Cheers


James


Thanks, James.   I was writing my response as you were writing yours, and I see we were thinking the same thing.. lol



Thanks for your response as I see this looks like how I'm having to go about what I'm trying to do.


Actually, here is the correct Client Script code:


$scope.checkin = function()


{


      c.server.update().then(function(){


              if ($scope.data.checkinResult == "success")


                      alert("You have successfully checked in, acknowledging this site emergency.");


              else if ($scope.data.checkinResult == "already")


                      alert("already");


              else if ($scope.data.checkinResult == "notfound")


                      alert("notfound");


              else if ($scope.data.checkinResult == "error")


                      alert("error");


      });


};




You must put it in the .then(function(){}) or else you won't get the correct values!  


Yep, that's why I refactored so it didn't need it (I'm not a fan of the alert function!)



The 'then' always gets called once the AJAX call has resolved or failed (lookup JavaScript/angular promises if you're interested) and gives you the new data object returned by the server which is actually passed in as the first argument of the callback function. You can chain these as well which can be really handy.



Cheers,


James