Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

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

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'


});


divvi_vamsi
Mega Expert

I have a similar issue, but confirmation should occur only after script include returns soemthing, else it shouldn't. Is there a way to achieve this?



Thanks