Disable a form or prevent submission during working hours

xhensilahad
Tera Expert

What is the best way to disable a form, or prevent submission during working hours?

The forma should only be visible or allowed submission from after 5pm until 8am of the next day.

 

Thank you

1 ACCEPTED SOLUTION

Ashish Parab
Mega Sage

Hello @xhensilahad 

 

You can create an onSubmit client script to prevent form submission.

script - 

function onSubmit() {
    // Get the current time
    var currentTime = new Date();
    var currentHour = currentTime.getHours();

    // Define the allowed time range (5 PM - 8 AM)
    if (currentHour >= 8 && currentHour < 17) { // you can adjust time as per your requirement
        // If the time is between 8 AM and 5 PM, prevent submission
        g_form.addErrorMessage('This form can only be submitted after 5 PM and before 8 AM.');
        return false; // Prevent form submission
    }
    
    return true; // Allow form submission
}

 

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ashish

View solution in original post

3 REPLIES 3

Harsh_Deep
Giga Sage
Giga Sage

Hello @xhensilahad 

 

You can use before BR and use glideDateTime(); and get the time and use current.setAbortAction(true);

 

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

Ashish Parab
Mega Sage

Hello @xhensilahad 

 

You can create an onSubmit client script to prevent form submission.

script - 

function onSubmit() {
    // Get the current time
    var currentTime = new Date();
    var currentHour = currentTime.getHours();

    // Define the allowed time range (5 PM - 8 AM)
    if (currentHour >= 8 && currentHour < 17) { // you can adjust time as per your requirement
        // If the time is between 8 AM and 5 PM, prevent submission
        g_form.addErrorMessage('This form can only be submitted after 5 PM and before 8 AM.');
        return false; // Prevent form submission
    }
    
    return true; // Allow form submission
}

 

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ashish

Tai Vu
Kilo Patron
Kilo Patron

Hi @xhensilahad 

You can use the GlideTime API and getHourOfDayUTC() to retrieve the hour in UTC and restrict user submissions to your working hours (8 AM to 5 PM).

getHourOfDayUTC()

Returns the hours part of the time using the UTC time zone. The number of hours is based on a 24 hour clock.

 

Sample business rule for table record (UTC)

(function executeRule(current, previous /*null when async*/) {

	var gt = new GlideTime();
	var currentHour = gt.getHourOfDayUTC();
	if(currentHour >= 8 && currentHour <= 17){
		return;
	}
	gs.addErrorMessage('you are only allowed to submit from 8AM to 5PM');
	current.setAbortAction(true);

})(current, previous);

 

Sample client script for catalog item (UTC)

Client script

function onSubmit() {
    if (g_scratchpad.isFormValid) {
        return true;
    }
    var actionName = g_form.getActionName();

    var ga = new GlideAjax('SubmissionValidation');
    ga.addParam('sysparm_name', 'validateHour');
    ga.getXMLAnswer(function(response) {
        if (response >= 8 && response <= 17) {
            g_scratchpad.isFormValid = true;
            g_form.submit(actionName);
        }else{
			g_form.addErrorMessage('you are only allowed to submit from 8AM to 5PM');
		}
    });
    return false;
}

Script Include Client Callable

var SubmissionValidation = Class.create();
SubmissionValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	validateHour: function(){
		var gt = new GlideTime();
		return gt.getHourOfDayUTC();
	},

    type: 'SubmissionValidation'
});

 

Cheers,

Tai Vu