Script Date Validation Issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2023 05:38 PM
Hello everyone!
In the change process, there is a mandatory field 'CAB Date' with a script validating whether the selected date is a valid date registered in the CAB meetings. Allowing the selection of this date until 11:59PM of the day before the CAB meeting date. However, there is an error in this validation.
Eg: if today is 07/25 and there is a CAB meeting registered on 07/26, it should be possible to select in the 'CAB Date' field the date 07/26, until 11:59PM on 07/25 today. But from 09:00PM it is informed that it is not possible to select the current day of the CAB, as if it were already on the 26/07.
This is the script:
var CABUtilsClient = Class.create();
CABUtilsClient.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateCabDate: function() {
// Recebe paramentros do client
var type = this.getParameter("sysparm_type");
var date = this.getParameter("sysparm_date");
// declaração do JSON de resposta
var response = {
"message": "",
"dateValidated": false
};
// Configura variáveis que serão usadas na query
var data_inicial = date + " 00:00:00";
var data_final = date + " 23:59:59";
// Realiza o tratamento das datas
data_inicial = data_inicial.replace(/\//g, "-");
data_final = data_final.replace(/\//g, "-");
// Instância datas como objetos de data.
var start = new GlideDateTime();
start.setValueUTC(data_inicial, 'dd-MM-yyyy HH:mm:ss');
var end = new GlideDateTime();
end.setValueUTC(data_final, 'dd-MM-yyyy HH:mm:ss');
// Realiza GlideRecord na cab_meeting
var gr_meet = new GlideRecord("cab_meeting");
gr_meet.addQuery('state', 'pending');
gr_meet.addQuery("cab_definition.cab_type", type);
gr_meet.addQuery("end", ">", start.getValue());
gr_meet.addQuery("end", "<", end.getValue());
gr_meet.orderBy('end');
gr_meet.setLimit('1');
gr_meet.query();
// Se achou o registro, entra no bloco
if (gr_meet.next()) {
// Busca data atual e realiza os devidos tratamentos
var data_atual = gs.now();
data_atual += " 23:59:59";
data_atual = data_atual.replace(/\//g, "-");
var atual = new GlideDateTime();
atual.setValueUTC(data_atual, 'dd-MM-yyyy HH:mm:ss');
// Se a data atual for maior que a data encontrada. Não vai deixar registrar.
if (atual.getValue() >= gr_meet.end) {
var ontem = atual;
ontem.addDays(-1);
// Se a data procura for maior do que o dia anterior ao atual, procurar o CAB mais próximo.
if (gr_meet.end > ontem.getValue()) {
// GlideRecord da busca
var gr_busca = new GlideRecord("cab_meeting");
gr_busca.addQuery("cab_definition.cab_type", type);
gr_busca.addQuery("end>javascript:gs.endOfToday()");
gr_busca.orderBy('end');
gr_busca.setLimit('1');
gr_busca.query();
// Se achou o registro
if (gr_busca.next()) {
var data_recuperada = gr_busca.end;
var date_semana = new GlideDate(); //replace new GlideDate() with the incoming date
date_semana.setValue(data_recuperada);
var date_recuperada = date_semana.getByFormat('dd/MM/yyyy'); //now use the date2 to set the field value
response.message = "Data do CAB não pode ser selecionada no dia atual. Próxima data do CAB: " + date_recuperada;
}
// Se não for maior que a data do dia anterior, CAB já passou.
} else {
response.message = "Data selecionada para o CAB é retroativa.";
}
// Se a data atual é menor que a data encontrada. Deixa registrar.
} else {
response.dateValidated = true;
response.message = "Data do CAB selecionada é válida.";
}
// Se não achou CAB, exibir resposta
} else {
response.message = "Nenhum CAB registrado para a data selecionada.";
}
// Returna o JSON
return JSON.stringify(response);
},
getStartDateHour: function() {
var response = {
"retorno": ""
};
var date = this.getParameter("sysparm_date");
var data_inicial = date + " 00:00:00";
var data_final = date + " 23:59:59";
data_inicial = data_inicial.replace(/\//g, "-");
data_final = data_final.replace(/\//g, "-");
// Instância datas como objetos de data.
var start = new GlideDateTime();
start.setValueUTC(data_inicial, 'dd-MM-yyyy HH:mm:ss');
var end = new GlideDateTime();
end.setValueUTC(data_final, 'dd-MM-yyyy HH:mm:ss');
// Realiza GlideRecord na cab_meeting
var gr_meet = new GlideRecord("cab_meeting");
gr_meet.addQuery('state', 'pending');
gr_meet.addQuery("cab_definition.cab_type", type);
gr_meet.addQuery("end", ">", start.getValue());
gr_meet.addQuery("end", "<", end.getValue());
gr_meet.orderBy('end');
gr_meet.setLimit('1');
gr_meet.query();
// Retorna a data e hora do cab
if (gr_meet.next()) {
response.retorno = gr_meet.getDisplayValue('end');
return JSON.stringify(response);
}
},
type: 'CABUtilsClient'
});
I suppose it's something related to the time zone, since we use the one in Brazil. And the default is American. And the difference between them is exactly three hours (GMT+3).
I'm new to scripting so I couldn't fix this problem. Thank you if you can help.