The CreatorCon Call for Content is officially open! Get started here.

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

Joe could you please share how you did this?



Thanks


Makes sense but not sure on the coding (sorry coding newbie relatively).


You could also just make it change it to the preceding Sunday automatically if they choose another day.   If getDayOfWeekLocalTime() != 7, get the day of the week and subtract it that many... addDaysLocalTime(-(getDayOfWeekLocalTime()) or something because a Monday (1) will subtract a day making it Sunday, etc.


Will this work on a calendar field - the user can select any date they want.     We do this so they can create future time cards or enter one for last week because they forgot.   So If i chose Tuesday of last week I need it to either go to Sunday of last week or tell me I have to choose a Sunday.  


Yeah, using an OnChange client script that calls an asynchronous GlideAjax script include and passes the newValue to the server for it to figure out and return the previous Sunday should work.   It would probably run twice though, cause the first time will check if it's a Sunday and then make it a Sunday, and the onChange will run again to see if it's a Sunday and then be good and not make another change.