Setting End date field to 30 days based on start date field date selection .

govi1
Giga Contributor

Hi Team,

I have two fields "start date " and "end date," when user selects start date automatically "end date" should be populated with 30 th day of start date.

 

I have written client script and script include , but script is not working I am getting null value in client script alert .Any help would be appreciated.

 

Client script : 

Type : onchange 

Field name : start_date 

 

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

//set variable for Valid to field
var cdt = g_form.getValue('start_date');
cdt = cdt.split("-");
cdt = cdt[2] + "-" + cdt[1] + "-" + cdt[0];
cdt = cdt.toString();
var addtime = 30; //The amount of time to remove
var addtype = 'day'; //The time type
//alert(cdt);
var ajax = new GlideAjax('RequestCompletionDate');
ajax.addParam('sysparm_name', 'addDateTimeAmount');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_addtime', addtime);
ajax.addParam('sysparm_addtype', addtype);
ajax.getXML(setReviewDate);
function setReviewDate(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
g_form.setValue('end_date', answer);
}
}

 

..........................................

Script include : 

script include name : RequestCompletionDate

function name : addDateTimeAmount

 

var RequestCompletionDate = Class.create();

RequestCompletionDate.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

addDateTimeAmount: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
var addTYPE = this.getParameter('sysparm_addtype'); //What to add - second (addSeconds()), minute (need to add conversion), hour (need to add conversion), day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add
var day = GlideDateTime(firstDT);

if(addTYPE == 'second'){day.addSeconds(addTIME);}
else if (addTYPE == 'minute'){day.addSeconds(addTIME*60);}
else if (addTYPE == 'hour'){day.addSeconds(addTIME*(60*60));}
else if (addTYPE == 'day'){day.addDays(addTIME);}
else if (addTYPE == 'week'){day.addWeeks(addTIME);}
else if (addTYPE == 'month'){day.addMonths(addTIME);}
else if (addTYPE == 'year'){day.addYears(addTIME);}
else {day.addDays(addTIME);}

//return "First Date: " + firstDT + " -Time to Add: " + addTIME + " -Add Type: " + addTYPE + " -Added Time: " + day;
day = day.getDisplayValue();
return day;
}

});

 

...................................................................................

Regards,

Govardhan

1 ACCEPTED SOLUTION

Hi,

Since your input field is only date and not date/time, make this 1 line change in your code and it will work.

var selected_date = new Date(getDateFromFormat(newValue, g_user_date_format));

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

 

View solution in original post

8 REPLIES 8

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi Govardhan,

 

From your code, it looks like we might add Seconds, Minutes, Hours, Days, Weeks, Months, or Years. First you will need to be sure that you've created a valid GlideDateTime object - in this case it appears you've forgotten your new operator. 

Assuming the date string you're passing is already in the system format:

var gdt = new GlideDateTime(firstDT);
if (addTYPE == 'day')
  gdt.addDays(addTime);

return gdt;

Hi Sheldon,

Thank you for your reply,

I have tried the way you have told , but still I am getting null value only in the alert and script include is not working  

 

Regards,

Govardhan

The complete solution would look something like this (pay close attention to field names [start_date, duration, end_date] in the client script, as I'm not sure what yours are).

 

Client Script

Type = onChange

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

    var ga = new GlideAjax('RequestCompletionDate');
    ga.addParam('sysparm_name', 'getEndDate');
    ga.addParam('sysparm_fdt', g_form.getValue('start_date'));
    ga.addParam('sysparm_addtime', g_form.getValue('duration'));
    ga.getXMLAnswer(setEndDate);
}

function setEndDate(answer) {
    g_form.setValue('end_date', answer);
}

 

Script Include

Client callable = true

var RequestCompletionDate = Class.create();
RequestCompletionDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getEndDate: function() {
        var firstDT = this.getParameter('sysparm_fdt');
        var addTIME = this.getParameter('sysparm_addtime');
        var addTYPE = 'day';
        var gdt = new GlideDateTime(firstDT);
        if (addTYPE == 'day')
            gdt.addDays(addTIME);

        return gdt;
    },

    type: 'RequestCompletionDate'
});

 

Note: addTYPE is hard-coded for this example.

asifnoor
Kilo Patron

Hi Govardhan,

I have written 1 article which is similar to your requirement and also contains sample code. Kindly try this.

https://community.servicenow.com/community?id=community_article&sys_id=a26dac761b4e8010a59033f2cd4bc...

Check #4 Add 5 days to the selected Date and set in a new field.

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP