Date converstion using script include and client script

Saurabh Singh5
Tera Expert

Hi,

 

Would need help with converting a Date from any format of the user into the system format and then validate it for 3 business days from the present day.

 

We have tried a lot of thing but at one point it works and suddenly it stops working we tried ".getNumericValue()"

but that also doesn't seem to work for different formats we need it to be set to system format and only then it works very sporadically.

1 ACCEPTED SOLUTION

Okay so i was referring to this error and got an old community post which has helped us fix the issue. by using the script: var newString= String(oldString).valueOf();

 

So, this error comes because the value being returned is a Java string, and we are trying to process it as a javascript string.

 

Community post to fix the error Ljava.lang.String;@

View solution in original post

9 REPLIES 9

Bert_c1
Kilo Patron

I don't see how anyone can help here without seeing the client script and script include.

Here is the script, Thank you in advance, 🙂

Script include:

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

var userdateformat = gs.getUser().getDateFormat();

{

var time = new GlideDateTime().getTime().getDisplayValue();
var days = 3;
var prschedule = gs.getProperty('scheduled_24hrsX5');
var opened_st = new GlideDateTime();
var duration = new GlideDuration(60 * 60 * 24 * 1000 * days);
var schedule = new GlideSchedule(prschedule);
var duedate = schedule.add(opened_st, duration);
var gdt = new GlideDateTime(duedate);
var dateMil = gdt.getNumericValue();
var dateTi = this.getParameter('sysparm_date');
var dateTime = this.getParameter('sysparm_date').toString().split(' ');
var a1 = '/,-,.';
var a4 = a1.split(',');
//var dateFor = gs.getUser().getDateFormat().toString();
var dateFor;
var user = new GlideRecord('sys_user');
if (user.get(gs.getUserID()))
{
dateFor = user.date_format.toString();
}

var j;
for (var i = 0; i < a4.length; i++) {
if (dateFor.contains(a4[i])) {
j = a4[i];
break;
}
}
var date = dateTime[0];

var time1 = dateTime[1];
var rDate = {};
var dateCon = date.split(j.toString());
var dateFor1 = dateFor.toString().split(j.toString());


for (var k = 0; k < dateFor1.length; k++) {

rDate[dateFor1[k].toString()] = dateCon[k];

}

var rDateTime = rDate.yyyy + '-' + rDate.MM + '-' + rDate.dd + ' ' + time1;
var gdt1 = new GlideDateTime(rDateTime);
var startDateMil = gdt1.getNumericValue();

if (dateMil > startDateMil) {
return true;
} else {
return false;

}
}
},
type: 'getplanneddatevalues'
});

 

Client script:

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

var opened_date;

var ga = new GlideAjax('getplanneddatevalues');
ga.addParam('sysparm_name', 'getValue');
ga.addParam('sysparm_date',g_form.getValue('start_date'));
ga.getXML(updateexpedited);
function updateexpedited(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");

g_form.setValue('start_date', '');
g_form.showFieldMsg("start_date", "The Planned Start Date cannot be within 3 business days for a Normal change", "error");
}
}
}

Hi @Saurabh Singh5 ,
Try to check below code.
Script Include:

var GetPlannedDateValues = Class.create();
GetPlannedDateValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getValue: function() {
        var userDateFormat = gs.getUser().getDateFormat();
        var scheduleProperty = gs.getProperty('scheduled_24hrsX5');
        var openedDateTime = new GlideDateTime();
        var duration = new GlideDuration(60 * 60 * 24 * 1000 * 3); // 3 days
        var schedule = new GlideSchedule(scheduleProperty);
        var dueDate = schedule.add(openedDateTime, duration);
        
        // Get the planned due date and convert to numeric value
        var dueDateTime = new GlideDateTime(dueDate);
        var plannedDueDateMil = dueDateTime.getNumericValue();
        
        // Retrieve the user input date from the parameters
        var inputDateTime = this.getParameter('sysparm_date');
        var inputDateParts = inputDateTime.split(' ');
        
        var user = new GlideRecord('sys_user');
        if (user.get(gs.getUserID())) {
            userDateFormat = user.date_format.toString();
        }

        // Use RegExp to split the input date based on user's date separator (/,.-)
        var dateSeparator = this.getDateSeparator(userDateFormat);
        var dateParts = inputDateParts[0].split(dateSeparator);
        var timePart = inputDateParts[1];

        // Reconstruct the date as per the user's format
        var formattedDate = this.formatDate(userDateFormat, dateParts, timePart);
        var inputDateTimeGlide = new GlideDateTime(formattedDate);
        var inputDateMil = inputDateTimeGlide.getNumericValue();

        // Compare the planned due date with the user input date
        return plannedDueDateMil > inputDateMil;
    },

    getDateSeparator: function(dateFormat) {
        var separators = ['/', ',', '.', '-'];
        for (var i = 0; i < separators.length; i++) {
            if (dateFormat.indexOf(separators[i]) !== -1) {
                return separators[i];
            }
        }
        return '-'; // Default separator
    },

    formatDate: function(dateFormat, dateParts, timePart) {
        var dateObj = {};
        var dateFormatParts = dateFormat.split(this.getDateSeparator(dateFormat));

        // Map the parts to the corresponding date format
        for (var i = 0; i < dateFormatParts.length; i++) {
            dateObj[dateFormatParts[i]] = dateParts[i];
        }

        // Reconstruct the date in ISO format
        return dateObj.yyyy + '-' + dateObj.MM + '-' + dateObj.dd + ' ' + timePart;
    },

    type: 'GetPlannedDateValues'
});

 Client Script:

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

    var ga = new GlideAjax('GetPlannedDateValues');
    ga.addParam('sysparm_name', 'getValue');
    ga.addParam('sysparm_date', g_form.getValue('start_date'));

    ga.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (answer === 'false') {
            g_form.setValue('start_date', '');
            g_form.showFieldMsg("start_date", "The Planned Start Date cannot be within 3 business days for a Normal change", "error");
        }
    });
}

 

Kindly Mark it as correct/helpful! it it solves your issue

 

Best Regards,

Pooja

Thank you so much for the response. I have tested this and this is the same spot where we couldnt find the solution as well. So the problem is when ever we set the user profile to a date format such as dd.MM.yyyy we are not getting the expected output when we are splitting the passed dates from the client side for some reason,

 

// Retrieve the user input date from the parameters
        var inputDateTime = this.getParameter('sysparm_date');
        var inputDateParts = inputDateTime.split(' ');
gs.log('sauvalues25'+ inputDateParts);
The output of this inputdateparts seems to be coming out with weird output as per the logs screenshot attached:
SaurabhSingh5_1-1739901740215.png