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

I used Alan's approach in terms of an OnChange client script that calls the GlideAjax script. 'week_starts_on' is the name of my variable that the user selects. Clearly some cleanup can be done on the code but I'll just dump it hear so you can see it all. As you can see case 6 (sunday) just does nothing. You can easily find that ClientDateTimeUtils online.





var day


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


var dObj = new Date(selected_date);


var selected_day = dObj.getDay();



switch (selected_day) {


        case 0:


                day = "Monday";


                var cdt = g_form.getValue('week_starts_on'); //Choose the field to add time from  


                var addtime = -1; //The amount of time to add  


                var addtype = 'day'; //The time type.   Can be day, week, month, year.



                var ajax = new GlideAjax('ClientDateTimeUtils');  


                ajax.addParam('sysparm_name', 'addDateAmount');  


                ajax.addParam('sysparm_fdt', cdt);  


                ajax.addParam('sysparm_addtime', addtime);  


                ajax.addParam('sysparm_addtype', addtype);  


                ajax.getXML(doSomething);  


 


                function doSomething(response){        


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


                        g_form.setValue('week_starts_on',answer);}



                break;


        case 1:


                day = "Tuesday";


                var cdt = g_form.getValue('week_starts_on'); //Choose the field to add time from  


                var addtime = -2; //The amount of time to add  


                var addtype = 'day'; //The time type.   Can be day, week, month, year.



                var ajax = new GlideAjax('ClientDateTimeUtils');  


                ajax.addParam('sysparm_name', 'addDateAmount');  


                ajax.addParam('sysparm_fdt', cdt);  


                ajax.addParam('sysparm_addtime', addtime);  


                ajax.addParam('sysparm_addtype', addtype);  


                ajax.getXML(doSomething);    


 


                function doSomething(response){        


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


                        g_form.setValue('week_starts_on',answer);}



                break;


        case 2:


                day = "Wednesday";


                var cdt = g_form.getValue('week_starts_on'); //Choose the field to add time from  


                var addtime = -3; //The amount of time to add  


                var addtype = 'day'; //The time type.   Can be day, week, month, year.



                var ajax = new GlideAjax('ClientDateTimeUtils');  


                ajax.addParam('sysparm_name', 'addDateAmount');  


                ajax.addParam('sysparm_fdt', cdt);  


                ajax.addParam('sysparm_addtime', addtime);  


                ajax.addParam('sysparm_addtype', addtype);  


                ajax.getXML(doSomething);    


 


                function doSomething(response){        


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


                        g_form.setValue('week_starts_on',answer);}



                break;


        case 3:


                day = "Thursday";


                var cdt = g_form.getValue('week_starts_on'); //Choose the field to add time from  


                var addtime = -4; //The amount of time to add  


                var addtype = 'day'; //The time type.   Can be day, week, month, year.



                var ajax = new GlideAjax('ClientDateTimeUtils');  


                ajax.addParam('sysparm_name', 'addDateAmount');  


                ajax.addParam('sysparm_fdt', cdt);  


                ajax.addParam('sysparm_addtime', addtime);  


                ajax.addParam('sysparm_addtype', addtype);  


                ajax.getXML(doSomething);    


 


                function doSomething(response){        


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


                        g_form.setValue('week_starts_on',answer);}



                break;


        case 4:


                day = "Friday";


                var cdt = g_form.getValue('week_starts_on'); //Choose the field to add time from  


                var addtime = -5; //The amount of time to add  


                var addtype = 'day'; //The time type.   Can be day, week, month, year.



                var ajax = new GlideAjax('ClientDateTimeUtils');  


                ajax.addParam('sysparm_name', 'addDateAmount');  


                ajax.addParam('sysparm_fdt', cdt);  


                ajax.addParam('sysparm_addtime', addtime);  


                ajax.addParam('sysparm_addtype', addtype);  


                ajax.getXML(doSomething);  


 


                function doSomething(response){        


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


                        g_form.setValue('week_starts_on',answer);}



                break;


        case 5:


                day = "Saturday";


                var cdt = g_form.getValue('week_starts_on'); //Choose the field to add time from  


                var addtime = -6; //The amount of time to add  


                var addtype = 'day'; //The time type.   Can be day, week, month, year.



                var ajax = new GlideAjax('ClientDateTimeUtils');  


                ajax.addParam('sysparm_name', 'addDateAmount');  


                ajax.addParam('sysparm_fdt', cdt);  


                ajax.addParam('sysparm_addtime', addtime);  


                ajax.addParam('sysparm_addtype', addtype);  


                ajax.getXML(doSomething);  


 


                function doSomething(response){        


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


                        g_form.setValue('week_starts_on',answer);}


               


                break;


        case 6:


                day = "Sunday";


                break;


}


}


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);


}


That is certainly acceptable if you just want to throw an error and have them 'try again'. The switch method will actually reset whatever date they choose to the Sunday previous (which seemed to be your original intention). Glad you got what you needed for your usage though!


Hi Kevin,



I am not able to run the mentioned code for scoped application.



Thanks,


Uzma


Uzma,


Instead of his "current.week_starts_on" you just need to make sure to use whatever field you're checking (both line 1 and 4).   I don't see anything else that would stop you from running that in a scoped app as long as the business rule is in the same scope