The CreatorCon Call for Content is officially open! Get started here.

Calculate the difference between start date and end date and populate the difference between them in another field

test1231998
Tera Contributor

I have two fields start_date and end_date.I wanted to writ a client script so that when i select both dates the difference should be populated in another field called no_of_days.

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Deepika,

you will have to write onchange client script on both start_date and end_date fields

Sample script below

Script Include: It should be client callable

var DateCalculations = Class.create();
DateCalculations.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getDifference: function(){

		var startDate = new GlideDateTime(this.getParameter('sysparm_startDate'));	
        var endDate = new GlideDateTime(this.getParameter('sysparm_endDate'));		
		var diffSeconds = gs.dateDiff(startDate.getDisplayValue(), endDate.getDisplayValue(), true);
		var days = parseInt(diffSeconds)*60*24;
        return  days; 		
	},
	
    type: 'DateCalculations'
});

Client Script: onChange of Start; similarly you need to have onChange of End

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }

if(newValue == ''){
	g_form.clearValue('no_of_days');
}

if(oldValue != newValue){

var ga = new GlideAjax('DateCalculations');
    ga.addParam('sysparm_name', "getDifference");
    ga.addParam('sysparm_startDate', g_form.getValue('start_date'));
	ga.addParam('sysparm_endDate', g_form.getValue('end_date'));
    ga.getXMLAnswer(function(answer){
        if(answer != ''){
            g_form.setValue('no_of_days', answer); // give proper field name here
        }
    });
    //Type appropriate comment here, and begin script below
}
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Willem
Giga Sage
Giga Sage

Make sure no_of_days field is of type duration and use below script in a Before Insert Business rule:

getTimeDiff();

function getTimeDiff() {
    var startDate = current.u_start_date.getGlideObject();
    var endDate = current.u_end_date.getGlideObject();

    current.no_of_days = gs.dateDiff(startDate.getDisplayValueInternal(), endDate.getDisplayValueInternal(), false);


}

 

reference:

https://community.servicenow.com/community?id=community_question&sys_id=fa3dc7a9db9cdbc01dcaf3231f96...

asifnoor
Kilo Patron

Hi,

Please refer to this link where i have provided sample code on how to do date validations using client script.

https://community.servicenow.com/community?id=community_article&sys_id=a26dac761b4e8010a59033f2cd4bc...

Mark the comment as a correct answer and helpful if this helps.

Hi Deepika,

I thought you wanted to do validation using client scripts. Did you check the article that i have provided above?

Bhagyashri Sort
Kilo Guru

Hi,

Please refer below link, it might helpful to you.

https://community.servicenow.com/community?id=community_question&sys_id=a67503eddbd8dbc01dcaf3231f96...

 

Mark it correct and helpful.

Thanks

Bhagyashri Sorte.