Reloading a form after business rule executes

John VanBruggen
Giga Guru

I have a question.   I am wondering if it is possible to reload a form after a business rule executes.

As an example, if a user attaches a file to a record and a business rule executes that moves that attachment to a new location, can I force the form to refresh so that it no longer shows the attachments that were added to the record?

I am assuming it would be client side, I just am not sure on how to code it.

Thanks!

Check out my Consultant's Survival Guide
https://youtube.com/watch?v=zYi8KhP9SUk
21 REPLIES 21

jorgelalmodovar
Kilo Contributor

Agreed, it should stay an on before business rule.


jorgelalmodovar
Kilo Contributor

Ummm, try this I made a modification to your verifyHoursIncrement function and your evalHours function. On the evalHours function you need to add add the name of your u_weekly_timesheet table in the GlideRecord if that is not the correct one:



evalHours();



function evalHours(){


                              if (!verifyHoursIncrement()){; //first let's see if the value entered is an increment of .25


        gs.addErrorMessage("Only increments of .25 are permitted. Please click Save to refresh the totals on this form.");


                                      current.setAbortAction(true);


      }else if (verifyDailyTotal()){//next check if the total for the day across all entries for that day are not > 24                              


        var timesheet = new GlideRecord("u_weekly_timesheet");


        if (timesheet.get(current.u_weekly_time_entry.u_weekly_timesheet)){      


                                                              action.setRedirectURL(timesheet);


                                                              current.setAbortAction(true);


        }


                              }


}



function verifyHoursIncrement(){


      var rtnValue = true;


                              var hrs = parseFloat(current.u_hours);


                              var hrs_int = parseInt(current.u_hours);


                              var dif = hrs-hrs_int;


                              var dif_inc = dif/0.25;


                              //if not an increment of .25 then no need to proceed


                              if (!(dif_inc==0 || dif_inc==1 || dif_inc==2 || dif_inc==3)){


                                                              rtnValue = false;


                              }


      return rtnValue;


}



function verifyDailyTotal(){


                              var prev = previous.u_hours;


                              var total_daily_hours = current.u_hours;


                              var wte = current.u_weekly_time_entry;


                              var wts = current.u_weekly_time_entry.u_weekly_timesheet; //Weekly Timesheet ID



                              //use the Weekly Timesheet ID to get all the Weekly Time Entries for that timesheet


                              var wter = new GlideRecord("u_weekly_time_entry");


                              wter.addQuery("u_weekly_timesheet", wts);


                              wter.addQuery("sys_id", "!=", wte); //let's not double count the current record we are updating


                              wter.query();



                              //for each Weekly Time Entry, go get Daily Time Entry hours for the same day


                              while (wter.next()) {


                                                              var dter = new GlideRecord("u_daily_time_entry");


                                                              dter.addQuery("u_worked_on", current.u_worked_on);


                                                              dter.addQuery("u_weekly_time_entry", wter.sys_id); //let's not double count the current record we are updating


                                                              dter.query();


                                                              while (dter.next()) {


                                                                                              total_daily_hours = total_daily_hours + dter.u_hours;


                                                              }


                              }



                              if (total_daily_hours>24){


                                                              current.u_hours = prev;


                                                              gs.addErrorMessage("Your update could not be applied because the total number of hours for the day cannot exceed 24. Please click Save to refresh the totals on this form.");


                                                              return true;


                              }


                              else


                                                              return false;



}


I made the changes and still the form will not reload automatically. I have to click save for the column totals to "re-set" when an invalid value is entered in one of them. Here is the latest:



evalHours();



function evalHours(){


//first let's see if the value entered is an increment of .25


if (!verifyHoursIncrement()) {


gs.addErrorMessage("Only increments of .25 are permitted. Please click Save to refresh the totals on this form.");


current.setAbortAction(true);


}


//next check if the total for the day across all entries for that day are not > 24


else if (verifyDailyTotal()){


var timesheet = new GlideRecord("u_weekly_timesheet");


if (timesheet.get(current.u_weekly_time_entry.u_weekly_timesheet)){


action.setRedirectURL(timesheet);


current.setAbortAction(true);


}


}


}



function verifyHoursIncrement(){


var rtnValue = true;


var hrs = parseFloat(current.u_hours);


var hrs_int = parseInt(current.u_hours);


var dif = hrs-hrs_int;


var dif_inc = dif/0.25;


//if not an increment of .25 then no need to proceed


if (!(dif_inc==0 || dif_inc==1 || dif_inc==2 || dif_inc==3)){


rtnValue = false;


}


return rtnValue;


}



function verifyDailyTotal(){


var prev = previous.u_hours;


var total_daily_hours = current.u_hours;


var wte = current.u_weekly_time_entry;


var wts = current.u_weekly_time_entry.u_weekly_timesheet; //Weekly Timesheet ID



//use the Weekly Timesheet ID to get all the Weekly Time Entries for that timesheet


var wter = new GlideRecord("u_weekly_time_entry");


wter.addQuery("u_weekly_timesheet", wts);


wter.addQuery("sys_id", "!=", wte); //let's not double count the current record we are updating


wter.query();



//for each Weekly Time Entry, go get Daily Time Entry hours for the same day


while (wter.next()) {


var dter = new GlideRecord("u_daily_time_entry");


dter.addQuery("u_worked_on", current.u_worked_on);


dter.addQuery("u_weekly_time_entry", wter.sys_id); //let's not double count the current record we are updating


dter.query();


while (dter.next()) {


total_daily_hours = total_daily_hours + dter.u_hours;


}


}



if (total_daily_hours>24){


current.u_hours = prev;


gs.addErrorMessage("Your update could not be applied because the total number of hours for the day cannot exceed 24. Please click Save to refresh the totals on this form.");


return true;


}


else


return false;


}


jorgelalmodovar
Kilo Contributor

Hi Cynthia,



I tried this on my personal instance and is not working either however if you remove the action.setRedirectURL line the current.setAbortAction(true) should prevent the parent record from being updated.





function evalHours(){


                              if (!verifyHoursIncrement()){; //first let's see if the value entered is an increment of .25


        gs.addErrorMessage("Only increments of .25 are permitted. Please click Save to refresh the totals on this form.");


                                      current.setAbortAction(true);


      }else if (verifyDailyTotal()){//next check if the total for the day across all entries for that day are not > 24                              


                                                                                current.setAbortAction(true);


     


                              }


}


Hi Jorge,


I tried commenting out the extra lines and still the total on the column increases even though the value saved to the DB does not change (since the save was aborted).


I might just open a ticket with ServiceNow on this one ☹


Thank you so much for your help!!