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

xiaix
Tera Guru

I figured out how to get what I'm looking for, although it's not how I'm wanting to do it.   I'd rather call a defined server script function, but the following works for now:



HTML:


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



Client:


function($scope)


{


      var c = this;


      $scope.checkin = function()


      {


              c.server.update();


              alert($scope.data.checkinResult);


      };


}



Server:


(function() {


      /* populate the 'data' object */


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




      if (!input)


      {


              gs.addInfoMessage("load");


              var userID = gs.getUserID();


              var userLocation = gs.getUser().getLocation();


              var Instance_URL = gs.getProperty('glide.servlet.uri');


              var checkinResult = "junk";




              data.bcpEmergency = false;


              data.emergencies = [];


              data.userLocation = userLocation;


              data.userID = userID;


              data.Instance_URL = Instance_URL;


              data.checkinResult = checkinResult;




              var bcp = new GlideRecord('u_bcp_site_emergencies');


              bcp.addNotNullQuery('u_alert_message');


              bcp.addQuery('u_active', true);


              bcp.query();




              while (bcp.next())


              {


                      data.bcpEmergency = true;




                      var listData = {};


                      listData.location = bcp.getDisplayValue('u_location').toString().trim();


                      listData.locationSysID = bcp.getValue('u_location').toString().trim();


                      listData.checkinRequired = bcp.getValue('u_employee_checkin_required').toString().toLowerCase();


                      listData.alertMessage = bcp.getValue('u_alert_message').toString().trim();




                      data.emergencies.push(listData);


              }


      }


      else if (input)


      {


              var bcpcheckinResult = new GlideRecord('u_bcp_site_emergencies');


              bcpcheckinResult.addNotNullQuery('u_alert_message');


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


              bcpcheckinResult.addQuery('u_active', true);


              bcpcheckinResult.query();




              if (bcpcheckinResult.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', bcpcheckinResult.sys_id);


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


                      bcp2.query();




                      if (bcp2.next())


                      {


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


                              bcp2.update();


                              data.checkinResult = "success";


                      }


                      else


                              data.checkinResult = "already";


              }


              else


                      data.checkinResult = "notfound";




              data.checkinResult = "error";


              gs.addInfoMessage(data.checkinResult);


      }




})();


I tried setting the data and alerting it after calling server.update, but it looks like the server script does not finish before the alert runs.