- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2016 07:32 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2016 06:50 AM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2016 03:13 PM
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;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2016 06:50 AM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2016 06:57 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2017 02:12 AM
Hi Kevin,
I am not able to run the mentioned code for scoped application.
Thanks,
Uzma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2017 05:45 AM
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