
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2024 02:22 PM
I have a UI action to move the change to an Implement state if the planned start date time is in the past. If not, then it needs to open a modal window. I'm having trouble evaluating the datetimes and moving on in the script. I am getting the gs. is not defined error.
UI Action:
Client: Checked
Onclick: moveToImplement();
Action Name: state_model_move_to_implement
//Initial function called. This is the Client-side 'onclick' function
function moveToImplement() {
var plannedStart = g_form.getValue('start_date');
if (typeof window == 'undefined')
determineIfInWindow(); //Determine if the current DateTime is after the Planned StartDateTime.
return plannedStart; //Return start date from the form.
}
//Function to Create the Modal
function showModal() {
var early_reason = g_form.getValue('u_early_release_justification');
var gm = new GlideModal("early_change_reason");
gm.setTitle("Why are you releasing before the planned start date?");
gm.setPreference("early_reason", early_reason);
//start html that will be used in GlideModal
gm.render();
}
//Function to determine if current date is passed the scheduled start date
function determineIfInWindow(plannedStart) {
gs.info('Planned Start' + plannedStart);
var nowDate = gs.nowDateTime();
gs.info('Now Date: '+ nowDate);
//current.start_date.getDisplayValue()>=gs.nowDateTime()
if (plannedStart < nowDate) {
//If the planned date is smaller than now, then proceed.
implementChange();
return true;
} else {
//If it is in the future, then open the modal.
showModal();
}
}
//Function to actually implement the Change
function implementChange() {
g_form.setValue("state", "-1");
gsftSubmit(null, g_form.getFormElement(), "state_model_move_to_implement");
current.update();
action.setRedirectURL(current);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2024 03:32 PM
Hi, I would suspect that your issue is you are subtracting strings, have you tried debugging\logging the typeof any variables created and the results of your comparison?
There are a few possible solutions based on client scripts used for date comparisions on change_request table.
/sys_script_client_list.do?sysparm_query=sys_class_name%3Dsys_script_client%5EnameLIKEdate%5Etable%3Dchange_request&sysparm_view=
One option could be this UI script to compare your dates
/nav_to.do?uri=sys_ui_script.do?sys_id=42518fd3672222004792adab9485ef76
Another possible option could be to compare unix time stamps like this script
/nav_to.do?uri=sys_script_client.do?sys_id=a99629f8ef011100521155aef5c0fb72
You could also use Ajax script and server side GlideDataTime to compare the 2 dates.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2024 03:32 PM
Hi, I would suspect that your issue is you are subtracting strings, have you tried debugging\logging the typeof any variables created and the results of your comparison?
There are a few possible solutions based on client scripts used for date comparisions on change_request table.
/sys_script_client_list.do?sysparm_query=sys_class_name%3Dsys_script_client%5EnameLIKEdate%5Etable%3Dchange_request&sysparm_view=
One option could be this UI script to compare your dates
/nav_to.do?uri=sys_ui_script.do?sys_id=42518fd3672222004792adab9485ef76
Another possible option could be to compare unix time stamps like this script
/nav_to.do?uri=sys_script_client.do?sys_id=a99629f8ef011100521155aef5c0fb72
You could also use Ajax script and server side GlideDataTime to compare the 2 dates.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2024 09:06 AM
@Tony Chatfield1 Thanks for your help! It actually led me down the getDateFromFormat function and was able to accurately made the determination that way. It ended up looking like this.
function determineIfInWindow() {
var date_number = getDateFromFormat(g_form.getValue('start_date'), g_user_date_time_format);
var plannedStart = new Date(date_number);
var nowDate = new Date();
if (plannedStart < nowDate) {
//If the planned date is smaller than now, then proceed.
implementChange();
return true;
} else {
//If it is in the future, then open the modal.
showModal();
}
}