How can I force a date field to be a Sunday?

kevinfrost471
Mega Expert

I have a date field on my time card form that is a "Week Starts on Date".   This needs to be a Sunday however right now the end user can chose any date.     What I want to do is to force them to use a Sunday.     (It can be a Monday if necessary).       If a user chooses a Tuesday I want the field to populate with the Sunday of that week and notify the users that we have done that.   Any help is greatly appreciated.

1 ACCEPTED SOLUTION

kevinfrost471
Mega Expert

Thank you to all!  



Here is the business rule I ended up putting in place - the is a before rule for insert and update:



var gdt = new GlideDateTime(current.week_starts_on);


if(gdt.getDayOfWeek()!=6){


      //gs.addInfoMessage('Week Starts On Date Must Be a Sunday');


      current.week_starts_on.setError('Week Starts On Date Must Be a Sunday');


      current.setAbortAction(true);


}


View solution in original post

19 REPLIES 19

Mike Allen
Mega Sage

You can do an AJAX call that returns the day of the week:



onSubmit Client Script:



//Type appropriate comment here, and begin script below


  var date = g_form.getValue('week_starts_on');


  var getDay = new GlideAjax('getDay');


  getDay.addParam('sysparm_name', 'getDayOfWeek');


  getDay.addParam('sysparm_date', date);


  getDay.getXML(DayResponse);



  function DayResponse(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");


            if(answer != 0){


                      alert('Please make week starts on a Sunday.');


                      return false;


                      }


            }



Script Include



var getDay = Class.create();


getDay.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  getDayOfWeek: function(){


  var date = new GlideDateTime(this.getParameter('sysparm_date'));


  var dow = date.getDayOfWeekLocalTime();



  return dow;


  },


  type: 'getDay'


});




This will not allow someone to submit the form unless that field is a Sunday.


Mike this looks great - could that be run as an onChange script on that Week Starts on field?   I would like to catch it as they select the date.  


onChange results in an infinite loop, as it changes when the user changes, then changes when the script changes, then changes again, and on and on...


Thank you Miek - yeah I can see how that would loop....here is my code as it sits but it is not working at the moment - it is not stopping me from entering for any day of the week.



function onSubmit() {



  var date = g_form.getValue('time_card.week_starts_on');  


  var getDay = new GlideAjax('getDay');  


  getDay.addParam('sysparm_name', 'getDayOfWeek');  


  getDay.addParam('sysparm_date', date);  


  getDay.getXML(DayResponse);  


 


  function DayResponse(response) {  


  var answer = response.responseXML.documentElement.getAttribute("answer");  


            if(answer != 0){  


                      alert('Please make week starts on a Sunday.');  


                      return false;  


                      }  


            }


}