Need help with converting date/time format in Script Include

Uttam Sai
Tera Expert

Hi Everyone,

 

I am using a Client Script and Script Include to check that the Outage Begin date should be with in the related change Window. This should work for all users in different time zones. The Client Script and Script Include works fine if the user's profile date format is yyyy-MM-dd , and doesn't work for any other date formats. Please let me know how to make the Client Script and Script Include work for all date formats.(Note: if the user's profile date format is not "yyyy-MM-dd" , the alert is triggering even when Outage Begin date is with in the related change Window)

Kindly help me with the issue.

1 ACCEPTED SOLUTION

Community Alums
Not applicable

I modified it to take the client side timezone into account. How does it work?

var Checkbegindate = Class.create();
Checkbegindate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	checkStartofChange: function(){
		
		var begin = new GlideDateTime();
		begin.setDisplayValue(this.getParameter('outageStartDate'));
		begin = begin.getNumericValue() - begin.getTZOffset();

		var changeId = this.getParameter('changeId'); //the changeID
		var changeGR = new GlideRecord('change_request');
		changeGR.get(changeId);
		
		var ChangeStart = new GlideDateTime();
		ChangeStart.setDisplayValueInternal(changeGR.start_date);
		ChangeStart = ChangeStart.getNumericValue();
		
		var ChangeEnd = new GlideDateTime();
		ChangeEnd.setDisplayValueInternal(changeGR.end_date);
		ChangeEnd = ChangeEnd.getNumericValue();
				
		if(begin < ChangeStart || begin > ChangeEnd){
			return false;
		}
		else{
			return true;
		}
	},
	type: 'Checkbegindate'
});

 

View solution in original post

6 REPLIES 6

Uttam Sai
Tera Expert

Hi @Community Alums ,
This Script Include is creating an issue , if an outage is created from an incident.
Could you please suggest a resolution that the Client Script or Script Include doesn't consider Incidents i.e., if task_number starts with 'INC'

Community Alums
Not applicable

If you want to run it only if the outage is related to change request, please try this client script.

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

    //check begin date only if this outage ralated to change request
    var number = g_form.getDisplayBox('task_number').value;
    if (number.startsWith('CHG')) {

        var ga = new GlideAjax('Checkbegindate'); //client callable script include
        ga.addParam('sysparm_name', 'checkStartofChange'); //function name on the script include
        ga.addParam('outageStartDate', newValue); // the begin date selected by the user
        ga.addParam('changeId', g_form.getValue('task_number')); //changeID - to query and compare with the change start date.
        ga.getXML(verifystartdate);


    }

    function verifystartdate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == 'false') {
            alert('Outage Begin date should be within the Change Planned Window');
            g_form.clearValue('begin');
        }
    }

}


Regards,