Client side confirm before Business Rule updates

Daryll Conway
Giga Guru

I have a function in a business rule that checks submission times of a change request against the first change task start date and the current CAB times.

I have the BR working fine but I've been asked to add a user confirmation to this process which will reject the submission is they choose and as this is all handled in a business rule I'm struggling to do this.

First of all thought I'll just add a boolean return using g_scratchpad that would trigger the confirmation in a client script but by the time I've got to the client script the business rule has already made the changes and has been submitted.

Can anyone think of a way to stop submission of a business rule (although I need to run to a point to decide if the client confirmation should be shown),

then from the user response submit or cancel?

Here is my business rule...

// Only check late if one of the given Business Entities and type is Normal or Emergency

var be = String(current.u_affected_business_entities);

var beList = [];

if(be.toLowerCase().indexOf(',') > -1){

      beList = be.split(',');

}

var stop = false;

if(beList.length>0){

      for(i=0;i<beList.length;i++){

              if(beList[i] == '5e58bb82a069d10040a915194b1407b8' || beList[i] == 'd89ba3e90f62350006ecd7b003050ed8'){

                      stop = true;

              }

      }

}

else{

      if(be == '5e58bb82a069d10040a915194b1407b8' || be == 'd89ba3e90f62350006ecd7b003050ed8'){

              stop = true;

      }

}

// UK CAB - Thursday @ 15:00 PM BST & Tuesday @ 9:00 BST

// Variables Start

var sdt;

if(current.work_start != ''){

      sdt = fixDateFormat(current.work_start.getDisplayValue()); //First Task Date/Time field

}

else{

      sdt = fixDateFormat(previous.work_start.getDisplayValue()); //First Task Date/Time field

}

var nowTimeDate = gs.nowDateTime();

var nowConverted = fixDateFormat(nowTimeDate);

var nowTime = String(nowConverted.split(' ')[1]);

var iLocalHours = nowTime.split(':')[0];

var nowTimeHour = parseInt(iLocalHours);

var tskTime = sdt.split(' ')[1];

var tskTimeHour = parseInt(tskTime.split(':')[0]);

var now = new GlideDateTime(String(nowConverted));

var tsk = new GlideDateTime(String(sdt));

var dayTsk = tsk.getDayOfWeek();

var daySub = now.getDayOfWeek();

var dateDiff = parseInt(gs.dateDiff(nowConverted, sdt, true));

var late = false;

g_scratchpad.emergency = false;

g_scratchpad.late = false;

// Variables End

function checkLate() {

      if((current.type == 'Emergency' || current.type == 'Normal') && !stop)

              {

              // Check if we have a submission date if not add one

              if(current.u_submitted_date == ''){

                      current.u_submitted_date = nowTimeDate;

              }

              // Kick off the function "getDates()" below...

              getDates();

      }

     

     

}

function getDates(){

      if(current.work_start == '' && previous.work_start == ''){

              // Do nothing we have no dates to work with

      }

      else{

              if(dateDiff < 518400 /* 6 Days */ && dateDiff > 7200 /* 2 Hours - Emergency if less than this*/){

                      resultShow();

              }

              if(dateDiff < 7200 /* 2 Hours - Emergency if less than this*/){

                              g_scratchpad.late = true;

                              g_scratchpad.emergency = true;

              }

      }

}

function resultShow(){

      // Submit and Task date both between Tuesday 9am & Thursday 2pm CABs

      if(((daySub > 2 || daySub < 4 ) || (daySub == 2 && nowTimeHour > 9) || (daySub == 4 && nowTimeHour < 14)) && (dayTsk > 2 || dayTsk < 4 || (dayTsk == 2 && tskTimeHour > 9) || (dayTsk == 4 && tskTimeHour < 14))) {

              late = true;

      }

      // Submit and Task date both between Thursday 2pm & Tuesday 9am CABs

      if(((daySub > 4 || daySub <=1 )|| (daySub == 2 && nowTimeHour < 9) || (daySub == 4 && nowTimeHour > 14)) && (dayTsk >= 4 || dayTsk <=1 || (dayTsk == 2 && tskTimeHour < 9) || (dayTsk == 4 && tskTimeHour == 14))){

              late = true;

      }

      if(late) {

              g_scratchpad.late = true;

      }

      else{

              g_scratchpad.late = false;      

      }

}

// Rearrange date/time format

// FROM : dd-MMM-yyyy hh:mm:ss

// TO : yyyy-MM-dd hh:mm:ss

function fixDateFormat(date){

      if (date !== null){

              var time = date.split(' ')[1];

              var dateWhole = date.split(' ')[0];

              var year = dateWhole.split('-')[2];

              var mon = dateWhole.split('-')[1];

              var day = dateWhole.split('-')[0];

              var rmonth = '';

             

              if (mon.toLowerCase() == 'jan')

                      rmonth = '01';

              if (mon.toLowerCase() == 'feb')

                      rmonth = '02';

              if (mon.toLowerCase() == 'mar')

                      rmonth = '03';

              if (mon.toLowerCase() == 'apr')

                      rmonth = '04';

              if (mon.toLowerCase() == 'may')

                      rmonth = '05';

              if (mon.toLowerCase() == 'jun')

                      rmonth = '06';

              if (mon.toLowerCase() == 'jul')

                      rmonth = '07';

              if (mon.toLowerCase() == 'aug')

                      rmonth = '08';

              if (mon.toLowerCase() == 'sep')

                      rmonth = '09';

              if (mon.toLowerCase() == 'oct')

                      rmonth = '10';

              if (mon.toLowerCase() == 'nov')

                      rmonth = '11';

              if (mon.toLowerCase() == 'dec')

                      rmonth = '12';

             

              return year+'-'+rmonth+'-'+day + ' ' + time;

      }

      else

              {

              return '0';

      }

}

checkLate();

1 ACCEPTED SOLUTION

Daryll Conway
Giga Guru

Just to tie this one up here is my final working scripts...



Client Script (onSubmit)


function onSubmit() {


      if(g_form.getActionName() == 'changeSubmit'){


              var late = false,


              emergency = false,


              subTime,


              Ready2Submit = false,


              sub,


              ws = String(g_form.getValue('work_start')),


              be = String(g_form.getValue('u_affected_business_entities')),


              type = String(g_form.getValue('type'));


              var ga = new GlideAjax('CHGLateSub');


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


              ga.addParam('sysparm_work_start', ws);


              ga.addParam('sysparm_be', be);


              ga.addParam('sysparm_type', type);


              ga.getXMLWait();


              var answer = ga.getAnswer();


              late = answer.split(',')[0];


              emergency = answer.split(',')[1];


              subTime = answer.split(',')[2];


             


              //alert('late = ' + late + ' : Emergency = ' + emergency + ' : Submission Time = ' + subTime);


              if(late == 'true' || emergency == 'true'){


                      if(confirm('This Change Request has been submitted late, and will therefore miss Change Advisory Board (CAB) review. Normal, business-impacting changes should be submitted for review at the CAB meeting. Do you still wish to submit for this date/time?')){


                              g_form.setValue('u_submitted_late', 'true');


                              g_form.setValue('u_submitted_date', subTime);


                              if(emergency == true){


                                      g_form.setValue('type', 'Emergency');


                              }


                              return true;


                      }


                      else{


                              //g_form.setValue('u_submitted_late', 'false');


                              return false;


                      }


                     


              }


      }


}




Script Include...


var CHGLateSub = Class.create();


CHGLateSub.prototype = Object.extendsObject(AbstractAjaxProcessor,{


      late: function() {


              gs.log('CHGLateSub started', 'DC Log');


              // UK CAB - Thursday @ 15:00 PM BST & Tuesday @ 9:00 BST


              var be = String(this.getParameter("sysparm_be"));


              var work_start = this.getParameter("sysparm_work_start");


              var type = this.getParameter("sysparm_type");


             


              var beList = [];


              if(be.toLowerCase().indexOf(',') > -1){


                      beList = be.split(',');


              }


              var stop = false;


              if(beList.length>0){


                      for(i=0;i<beList.length;i++){


                              if(beList[i] == '5e58bb82a069d10040a915194b1407b8' || beList[i] == 'd89ba3e90f62350006ecd7b003050ed8'){


                                      stop = true;


                              }


                      }


              }


              else{


                      if(be == '5e58bb82a069d10040a915194b1407b8' || be == 'd89ba3e90f62350006ecd7b003050ed8'){


                              stop = true;


                      }


              }


             


              // Variables Start


              var sdt;


              if(work_start != ''){


                      sdt = fixDateFormat(work_start); //First Task Date/Time field


              }


             


              var nowTimeDate = gs.nowDateTime();


              var nowConverted = fixDateFormat(nowTimeDate);


              var nowTime = String(nowConverted.split(' ')[1]);


              var iLocalHours = nowTime.split(':')[0];


              var nowTimeHour = parseInt(iLocalHours);


              var tskTime = sdt.split(' ')[1];


              var tskTimeHour = parseInt(tskTime.split(':')[0]);


              var now = new GlideDateTime(String(nowConverted));


              var tsk = new GlideDateTime(String(sdt));


              var dayTsk = tsk.getDayOfWeek();


              var daySub = now.getDayOfWeek();


              var dateDiff = parseInt(gs.dateDiff(nowConverted, sdt, true));


              var late = false;


              var emergency = false;


              // Variables End


             


              function checkLate() {


                      // Only check late if one of the given Business Entities and type is Normal or Emergency


                      if((type == 'Emergency' || type == 'Normal') && !stop)


                              {


                              getDates();


                      }


                     


                     


              }


             


              function getDates(){


                      if(work_start == ''){


                              // Do nothing we have no date to work with


                      }


                      else{


                              if(dateDiff < 518400 /* 6 Days */ && dateDiff > 7200 /* 2 Hours - Emergency if less than this*/){


                                      resultShow();


                              }


                              if(dateDiff < 7200 /* 2 Hours - Emergency if less than this*/){


                                      late = true;


                                      emergency = true;


                              }


                      }


              }


             


              function resultShow(){


                      // Submit and Task date both between Tuesday 9am & Thursday 2pm CABs


                      if((daySub == 2 && nowTimeHour > 9) || (daySub == 3) || (daySub == 4 && nowTimeHour < 14)){


                              if((dayTsk == 2 && tskTimeHour > 9) || (dayTsk == 3) || (dayTsk == 4 && tskTimeHour < 14)){


                                      gs.log('Between Tuesday 9am & Thursday 2pm CABS : day of submission = ' + daySub + ' Hour = ' + nowTimeHour + ' : day of first task = ' + dayTsk + ' Hour = ' + tskTimeHour, 'DC Log');


                                      late = true;


                              }


                      }


                      // Submit and Task date both between Thursday 2pm & Tuesday 9am CABs


                      if((daySub == 4 && nowTimeHour > 14) || (daySub == 5 || daySub == 6 || daySub == 7) || (daySub < 2 || (daySub == 2 && nowTimeHour < 9))){


                              if((dayTsk == 4 && tskTimeHour > 14) || (dayTsk == 5 || dayTsk == 6 || dayTsk == 7) || (dayTsk < 2 || (dayTsk == 2 && tskTimeHour < 9))){


                                      gs.log('Between Thursday 2pm & Tuesday 9am CABS : day of submission = ' + daySub + ' Hour = ' + nowTimeHour + ' : day of first task = ' + dayTsk + ' Hour = ' + tskTimeHour, 'DC Log');


                                      late = true;


                              }


                      }


              }


              // Rearrange date/time format


              // FROM : dd-MMM-yyyy hh:mm:ss


              // TO : yyyy-MM-dd hh:mm:ss


              function fixDateFormat(date){


                      if (date !== null){


                              var time = date.split(' ')[1];


                              var dateWhole = date.split(' ')[0];


                              var year = dateWhole.split('-')[2];


                              var mon = dateWhole.split('-')[1];


                              var day = dateWhole.split('-')[0];


                              var rmonth = '';


                             


                              if (mon.toLowerCase() == 'jan')


                                      rmonth = '01';


                              if (mon.toLowerCase() == 'feb')


                                      rmonth = '02';


                              if (mon.toLowerCase() == 'mar')


                                      rmonth = '03';


                              if (mon.toLowerCase() == 'apr')


                                      rmonth = '04';


                              if (mon.toLowerCase() == 'may')


                                      rmonth = '05';


                              if (mon.toLowerCase() == 'jun')


                                      rmonth = '06';


                              if (mon.toLowerCase() == 'jul')


                                      rmonth = '07';


                              if (mon.toLowerCase() == 'aug')


                                      rmonth = '08';


                              if (mon.toLowerCase() == 'sep')


                                      rmonth = '09';


                              if (mon.toLowerCase() == 'oct')


                                      rmonth = '10';


                              if (mon.toLowerCase() == 'nov')


                                      rmonth = '11';


                              if (mon.toLowerCase() == 'dec')


                                      rmonth = '12';


                             


                              return year+'-'+rmonth+'-'+day + ' ' + time;


                      }


                      else


                              {


                              return '0';


                      }


              }


              checkLate();


             


              return late + ',' + emergency + ',' + nowTimeDate;


             


      },


     


      type: 'CHGLateSub'


});


View solution in original post

6 REPLIES 6

Thijs Daemen
Mega Guru

Hi Daryll,



In order to present the user with a confirmation box you will need to keep it client side. To be more specific, you will need to create the following;



  • an onsubmit client script that calls a server side script include via ajax (return false from the onsubmit client script to cancel the form submit)
  • a script include that includes the logic you have in you BR at the moment (return a XML or JSON to the client)


you can script the confirmation in the client script via a normal javascript 'confirm' dialog. Be sure to use a synchronous ajax (getXMLWait()) otherwise the form will submit without waiting for the response form the server script include.



Some links with information:


GlideAjax - ServiceNow Wiki


GlideAJAX API


Hi Thijs,



I was also looking at this approach but I'm having issues with my script include...



Here is my script include...



var CHGLateSub = Class.create();


CHGLateSub.prototype = {


initialize: function() {


},



late: function() {


  gs.log('CHGLateSub started', 'DC Log');


  // UK CAB - Thursday @ 15:00 PM BST & Tuesday @ 9:00 BST


  var be = String(this.getParameter("sysParm_be"));


  var submitDate = this.getParameter("sysparm_submit");


  var work_start = this.getParameter("sysParm_work_start");


  var type = this.getParameter("sysParm_type");



  var beList = [];


  if(be.toLowerCase().indexOf(',') > -1){


    beList = be.split(',');


  }


  var stop = false;


  if(beList.length>0){


    for(i=0;i<beList.length;i++){


      if(beList[i] == '5e58bb82a069d10040a915194b1407b8' || beList[i] == 'd89ba3e90f62350006ecd7b003050ed8'){


        stop = true;


      }


    }


  }


  else{


    if(be == '5e58bb82a069d10040a915194b1407b8' || be == 'd89ba3e90f62350006ecd7b003050ed8'){


      stop = true;


    }


  }



  // Variables Start


  var sdt;


  if(work_start != ''){


    sdt = fixDateFormat(work_start.getDisplayValue()); //First Task Date/Time field


  }


  else{


    sdt = fixDateFormat(previous.work_start.getDisplayValue()); //First Task Date/Time field


  }



  var nowTimeDate = gs.nowDateTime();


  var nowConverted = fixDateFormat(nowTimeDate);


  var nowTime = String(nowConverted.split(' ')[1]);


  var iLocalHours = nowTime.split(':')[0];


  var nowTimeHour = parseInt(iLocalHours);


  var tskTime = sdt.split(' ')[1];


  var tskTimeHour = parseInt(tskTime.split(':')[0]);


  var now = new GlideDateTime(String(nowConverted));


  var tsk = new GlideDateTime(String(sdt));


  var dayTsk = tsk.getDayOfWeek();


  var daySub = now.getDayOfWeek();


  var dateDiff = parseInt(gs.dateDiff(nowConverted, sdt, true));


  var late = false;


  var emergency = false;


  var isLate = false;


  // Variables End



  function checkLate() {


    // Only check late if one of the given Business Entities and type is Normal or Emergency


    if((type == 'Emergency' || type == 'Normal') && !stop)


      {


      // Check if we have a submission date if not add one


      if(submitDate == ''){


        submitDate = nowTimeDate;


      }


      getDates();


    }


 


 


  }



  function getDates(){


    if(work_start == ''){


      // Do nothing we have no date to work with


    }


    else{


      if(dateDiff < 518400 /* 6 Days */ && dateDiff > 7200 /* 2 Hours - Emergency if less than this*/){


        resultShow();


      }


      if(dateDiff < 7200 /* 2 Hours - Emergency if less than this*/){


        isLate = true;


        emergency = true;


      }


    }


  }



  function resultShow(){


    // Submit and Task date both between Tuesday 9am & Thursday 2pm CABs


    if(((daySub > 2 || daySub < 4 ) || (daySub == 2 && nowTimeHour > 9) || (daySub == 4 && nowTimeHour < 14)) && (dayTsk > 2 || dayTsk < 4 || (dayTsk == 2 && tskTimeHour > 9) || (dayTsk == 4 && tskTimeHour < 14))) {


      late = true;


    }


    // Submit and Task date both between Thursday 2pm & Tuesday 9am CABs


    if(((daySub > 4 || daySub <=1 )|| (daySub == 2 && nowTimeHour < 9) || (daySub == 4 && nowTimeHour > 14)) && (dayTsk >= 4 || dayTsk <=1 || (dayTsk == 2 && tskTimeHour < 9) || (dayTsk == 4 && tskTimeHour == 14))){


      late = true;


    }


  }



  // Rearrange date/time format


  // FROM : dd-MMM-yyyy hh:mm:ss


  // TO : yyyy-MM-dd hh:mm:ss


  function fixDateFormat(date){


    if (date !== null){


      var time = date.split(' ')[1];


      var dateWhole = date.split(' ')[0];


      var year = dateWhole.split('-')[2];


      var mon = dateWhole.split('-')[1];


      var day = dateWhole.split('-')[0];


      var rmonth = '';


   


      if (mon.toLowerCase() == 'jan')


        rmonth = '01';


      if (mon.toLowerCase() == 'feb')


        rmonth = '02';


      if (mon.toLowerCase() == 'mar')


        rmonth = '03';


      if (mon.toLowerCase() == 'apr')


        rmonth = '04';


      if (mon.toLowerCase() == 'may')


        rmonth = '05';


      if (mon.toLowerCase() == 'jun')


        rmonth = '06';


      if (mon.toLowerCase() == 'jul')


        rmonth = '07';


      if (mon.toLowerCase() == 'aug')


        rmonth = '08';


      if (mon.toLowerCase() == 'sep')


        rmonth = '09';


      if (mon.toLowerCase() == 'oct')


        rmonth = '10';


      if (mon.toLowerCase() == 'nov')


        rmonth = '11';


      if (mon.toLowerCase() == 'dec')


        rmonth = '12';


   


      return year+'-'+rmonth+'-'+day + ' ' + time;


    }


    else


      {


      return '0';


    }


  }


  checkLate();



  return late + ',' + emergency;



},



type: 'CHGLateSub'


};




and my onSubmit client script...



var late = false,
      emergency = false,
      Ready2Submit = false,
      sub,
      ws = String(g_form.getValue('work_start')),
      be = String(g_form.getValue('u_affected_business_entities')),
      type = String(g_form.getValue('type'));


function onSubmit() {
      if(g_form.getActionName() == 'changeSubmit'){
              //alert('conditions passed');


              var gat = new GlideAjax('ClientDateTimeUtils');
              gat.addParam('sysparm_name', 'getNowDateTime');
              gat.getXMLWait();
  sub = gat.getAnswer();  
  g_form.setValue('u_submitted_date', sub);
  alert('submission time/date = ' + sub);
  ajaxReturn();
              alert('end on Submit');
      }
      else{
              return true;
      }
      if(Ready2Submit){return true;}
      else{return false;}
}
function ajaxReturn(){
      var ga = new GlideAjax('CHGLateSub');
      ga.addParm('sysparm_name','late');
      ga.addParm('sysparm_submit', sub);
      ga.addParm('sysparm_work_start', ws);
      ga.addParm('sysparm_be', be);
      ga.addParm('sysparm_type', type);
      alert('pre AJAX');
      ga.getXMLWait();
var answer = ga.getAnswer();
late = answer.split(',')[0];
      emergency = answer.split(',')[1];
      alert('late = ' + late + ' : Emergency = ' + emergency);
      check();
}


function check(){
      //Type appropriate comment here, and begin script below
      if(late == true || emergency == true){
              if(confirm('This Change Request has been submitted late, and will therefore miss Change Advisory Board (CAB) review. Normal, business-impacting changes should be submitted for review at the CAB meeting. Do you still wish to submit for this date/time?')){
                      g_form.setValue('u_submitted_late', true);
                      if(emergency == true){
                              g_form.setValue('type', 'Emergency');
                      }
                      submit = true;
              }
              else{
                      g_form.setValue('u_submitted_late', false);
                      submit = false;
              }
      }
      alert('end check');
}



I get 2 alerts from this the 'Conditions passed' one and 'end on Submit' but no other alerts are triggered.


Any thought's on were I've gone wrong?


On the script include, you should enable the checkbox 'client callable', otherwise it will not work.



The start of the script include should look something like:



var CHGLateSub = Class.create();


CHGLateSub.prototype =   Object.extendsObject(AbstractAjaxProcessor, {


        late: function () {



        },



        type: 'CHGLateSub'


});



You should also remove the initialize function, it is not needed in a client callable script include.



It would also be better to not have 2 separate ajax calls, but to combine the calls within a single script include. So you would call the   ClientDateTimeUtils script include from you own script include. (or in this case because it seems to be a fairly simply script include/return value; you could simply look at what the code is in the ClientDateTimeUtils script include and include that in your own script include.



It will probably return gs.nowDateTime(); http://wiki.servicenow.com/index.php?title=GlideSystem_Date_and_Time_Functions#nowDateTime.28.29


spotted my mistake, I had 'ga.addParm' when it should be 'ga.addParam'