- 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
01-12-2016 08:47 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2016 09:05 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2016 09:15 AM
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2016 01:24 PM
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;
}
}
}