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.

How to calculate difference between two dates and show it on DAYS?

reinaldo_vieira
Tera Contributor

Hello!

I'm working on a challenge that asks me to calculate the date difference in 2 fields of a Service Portal form and impose form blocks and error-type messages depending on the result.

 

Calculating difference between two dates - Service Catalog form.jpeg

The two fields in the form are of type Date (not Date/Time).

Can this be done directly via Client Script or is it better to use a combination of Script Include GlideAJAX Script + Catalog Client Script onChange?

IMPORTANT:
- The date in "data_inicio_emprestimo" CANNOT be less than the current date. If it is, the field will display an error message and the form cannot be submitted.

- The date difference between the fields (data_inicio_emprestimo / data_final_emprestimo) must be a maximum of 7 days. If the difference is greater than 7 days, the field will display an error text message and the form cannot be submitted.

8 REPLIES 8

reinaldo_vieira
Tera Contributor

So far, I've built up a Script Include AND a Catalog Client Script like this.

But an error message appears in the console and I don't know why.

 

SCRIPT INCLUDE:

 

var DifferDates = Class.create();
DifferDates.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    calcularDias: function() {
        var from = this.getParameter('sysparm_start');
        var to = this.getParameter('sysparm_end');
var duration = GlideDate.subtract(from, to);
 
gs.info(duration + '@Reinaldo'); //To catch logs
    },
 
    type: 'DifferDates'
});
 
CATALOG CLIENT SCRIPT:
 

function onChange(control, oldValue, newValue, isLoading) {    //Variable name = data_final_emprestimo
if (isLoading || newValue == '') {
return;
}

//Type appropriate comment here, and begin script below
var startDate = g_form.getValue('data_inicio_emprestimo');
var endDate = g_form.getValue('data_final_emprestimo');
var maxDays = 7;

var ga = new GlideAjax('global.DifferDates');
ga.addParam('sysparm_name', 'calcularDias');
ga.addParam('sysparm_start', startDate);
ga.addParam('sysparm_end', endDate);
ga.getXMLAnswer(callback);

function callback(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
if (answer > maxDays) {
g_form.showFieldMsg('data_final_emprestimo', 'Você ultrapassou o limite de 7 dias. Por favor, escolha um período mais curto', 'error');
} else {
alert('Deu errado...'); //to check if Script Include and Client Script are running ok
}

}

}

Hi @reinaldo_vieira,

Its seems your script is correct instead ga.getXMLAnswer(callback); you can directly call answer no need responsexml so that line change with  ga.getXML(callback); 

Please mark it helpful and solution proposed if it works.

Thanks,

Anand

 

Thanks,

Anand

Hello, @Anand Kumar P

 

I tried the getXML without the "Answer" but unfortunately it didn't work. I just did a search here and I think that getXMLAnswer fits more for the challenge by taking a simple Answer from the Script Include, while the other type filters an entire XML Document.

 

Thanks,

 

Reinaldo Vieira

var sgd1 = new GlideDate();
sgd1.setDisplayValue(this.getParameter('sysparm_start'));
var sgd2 = new GlideDate();
sgd2.setDisplayValue( this.getParameter('sysparm_end'));

var duration = GlideDate.subtract(sgd1, sgd2);
gs.print(duration.getDisplayValue());

 

Please mark useful if above code worked