How to get the value in the date field using script include?

Yesh
Tera Contributor

Hi Team,

I have a requirement like I need to get the value of a date field using script include. Here the thing is it working for one set of code and it is not working for other set of code:

The following code is working fine:

var calendar = new GlideRecordSecure('x_calendar');
calendar.addQuery('u_legal_entity', LE);//this field is a reference field to another table
calendar.query();

while(calendar.next()) {
var year = calendar.getValue('u_year');
var month = calendar.getValue('u_month');
if(year == pyear && month == pmonth){
var start = new GlideDateTime(calendar.getValue('u_me_start_date'));
var end = new GlideDateTime(calendar.getValue('u_me_end_date'));
var diff = GlideDateTime.subtract(start,end);
var mdays = diff.getRoundedDayPart();
}
}

The following code is not working:

I have used the same query as above

var holiday = new GlideRecordSecure('x_holidays');
holiday.addQuery('u_legal_entity', LE); //this field is a reference field to another table
holiday.query();
while(holiday.next()) {    // It is not even getting inside the loop
hdate = holiday.getValue('u_holiday_date');
hmonth = hdate.substring(6,7);
}

Any suggestions are helpful.

I don't know where i have done a mistake. It seems to be correct  code. 

Thanks

15 REPLIES 15

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

if it's not going inside it means your query is wrong

can you share the complete script?

var holiday = new GlideRecordSecure('x_holidays');
holiday.addQuery('u_legal_entity', LE);
holiday.query();
while(holiday.next()) {    // It is not even getting inside the loop
hdate = holiday.getValue('u_holiday_date');
hmonth = hdate.substring(6,7);
}

Regards
Ankur

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

Hi,

Client Script:

function onSubmit() {

var LE = g_form.getValue('u_legal_entity');//returns the sysid of the reference table
var code = g_form.getValue('u_company_code');
var date = g_form.getValue('u_posted_date');
var year = date.substring(0,4);
var month = date.substring(6,7);

var workDay = new GlideAjax('JE_ClientUtil');
workDay.addParam('sysparm_name', 'calculateWorkday');
workDay.addParam('sysparm_le', LE);
workDay.addParam('sysparm_code', code);
workDay.addParam('sysparm_month', month);
workDay.addParam('sysparm_year', year);
workDay.getXML(getResponse);

}

Script include:

calculateWorkday: function() {

var LE = this.getParameter('sysparm_le');
var code = this.getParameter('sysparm_code');
var pyear = this.getParameter('sysparm_year');
var pmonth = this.getParameter('sysparm_month');
var week, days, hdate, hyear, hmonth, count=0;

var weekends = new GlideRecordSecure('x_legal_entity_master');
weekends.addQuery('u_legal_entity', LE);
weekends.addQuery('u_company_code', code);
weekends.query();

if(weekends.next()) {
week = weekends.getValue('u_weekend').toString();
days = week.length;
}

var holiday = new GlideRecordSecure('x_holidays'); //This query is not working
holiday.addQuery('u_legal_entity', LE);
holiday.query();

while(holiday.next()) {
gs.addInfoMessage(pyear);

hdate = new Date(holiday.getValue('u_holiday_date'));
hmonth = hdate.getMonth();

}

var calendar = new GlideRecordSecure('x_calendar');
calendar.addQuery('u_legal_entity', LE);
calendar.query();

while(calendar.next()) {
var year = calendar.getValue('u_year');
var month = calendar.getValue('u_month');
if(year == pyear && month == pmonth){
var start = new GlideDateTime(calendar.getValue('u_me_start_date'));
var end = new GlideDateTime(calendar.getValue('u_me_end_date'));
var diff = GlideDateTime.subtract(start,end);
var mdays = diff.getRoundedDayPart();
}
}
count = mdays - (days * 4);

}

This is the complete code.

Hi,

why are you using onSubmit to calculate?

Regards
Ankur

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

Hi,

The script not yet reached the requirement. Our main requirement is to get the working day of the selected date in the field by excluding holidays and weekends. And that working day should be displayed as field message on load of the form. I have to make it as onLoad. 

Thanks