Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Planned start time and Planned end time should be same in change request.

babbi
Tera Expert

Hi Team,

Can you please help me - attached script what is the result of it.

if there is any possible to keep start time & end time should not accept. how add this step in below script.

Onchange clientscript:-

babbi_0-1677058575949.png

babbi_0-1677058671243.png

 

 

2 ACCEPTED SOLUTIONS

Hi @babbi ,

In the same onChange script add the below line of scripts and shown in image.

Screenshot 2023-02-22 at 5.45.28 PM.png

 

 var startDate = g_form.getValue('start_date');
    var endDate = g_form.getValue('end_date');
    if (startDate == endDate) {
        var errorMessage = "Planned end date must be after Planned Start Date";
        g_form.clearValue('end_date');
        g_form.showErrorBox("end_date", errorMessage);

        return false;
    }

 

 

Mark helpful and accept the solution if it helps in solving your query.

 

Regards,

Johns

View solution in original post

Shamma Negi
Kilo Sage
Kilo Sage

Try this

 

var start = g_form.getValue('start_date');
var end = g_form.getValue('end_date');
if(start == end){
var errorMessage = "Planned end date must be after Planned Start Date";
g_form.showErrorBox("end_date", errorMessage);
return false;
}

Regards,Shamma Negi

View solution in original post

8 REPLIES 8

Hi @Johns Marokky 

Thanks for you help.

 

End time should take 10mints difference to start time.

below 10mints difference it should through error.

 

How to acheive ? 

var startDateTime = new Date(startDate);
var endDateTime = new Date(endDate);
var timeDiff = Math.abs(endDateTime.getTime() - startDateTime.getTime());
var diffMinutes = Math.ceil(timeDiff / (1000 * 60));
if (diffMinutes < 10) {
var errorMsg = "End date must be at least 10 minutes after the start date";
//alert(12);
g_form.clearValue("end_date");
g_form.showErrorBox("end_date", errorMsg);
return false;

 

Like this i have written but not working

Hi @babbi ,

you cannot do this with by using client script alone. you need to use GlideDateTime and its a server side script, so here you need to use both Client script and script include.

 

Create a callable script include as per below pic.

Screenshot 2023-02-27 at 10.31.01 PM.png

Script include scripts:

changeDateValidation : function(){
		var startDate = this.getParameter('sys_start_date');
		var endDate = this.getParameter('sys_end_date');
		var glideStartDate = new GlideDateTime(startDate).getNumericValue();
		var glideEndDate = new GlideDateTime(endDate).getNumericValue();
		var duration = glideEndDate - glideStartDate;
		var minutes = duration/60000;
		return minutes;
	},

 

In the onChange client script do the modification in the script as per below image.

Screenshot 2023-02-27 at 10.33.36 PM.png

Full onChange Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '')
        return;

    var showErrorMsg = function(errorMsg) {
        g_form.showErrorBox("end_date", errorMsg);
    };

    g_form.hideFieldMsg("end_date", true);

    var startDate = g_form.getValue('start_date');
    var endDate = g_form.getValue('end_date');
    if (startDate <= endDate) {
		var ga = new GlideAjax('ValidateStartDateEndDate');
		ga.addParam('sysparm_name','changeDateValidation');
		ga.addParam('sys_start_date',startDate);
		ga.addParam('sys_end_date',endDate);
		ga.getXMLAnswer(callbackFunction);
        
    }

    if (validateStartDateBeforeEndDate("start_date", "end_date", showErrorMsg) && (typeof validateMaxDateDuration !== "undefined"))
        validateMaxDateDuration("start_date", "end_date", showErrorMsg);
}

function callbackFunction (answer){
	if(answer < 10){     //give the mins you want to validate
		var errorMessage = "Planned end date must be 10 minutes after Planned Start Date";
        g_form.clearValue('end_date');
        g_form.showErrorBox("end_date", errorMessage);
        return false;
	}
}

 

Hope this helps your query.

 

Mark helpful and accept the solution if it helps in solving your query.

 

Regards,

Johns

Shamma Negi
Kilo Sage
Kilo Sage

Try this

 

var start = g_form.getValue('start_date');
var end = g_form.getValue('end_date');
if(start == end){
var errorMessage = "Planned end date must be after Planned Start Date";
g_form.showErrorBox("end_date", errorMessage);
return false;
}

Regards,Shamma Negi