Add 3 months to date.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2015 06:16 AM
Hi All,
I'm struggling to add 3 months to an existing date field. The following code below should look up the user and add 3 months to their eligibility date.
DELSetEligibilityOnAssignment();
function DELSetEligibilityOnAssignment() {
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', current.assigned_to);
gr.query();
var gdt = new GlideDateTime(gr.u_eligibility_date.getGlideObject());
gdt.addMonths(3);
if (gr.next()) {
if (current.u_reason_for_assigning == 'Lost') {
gr.u_work_notes = "Eligibility date changed on " + current.asset_tag + " for the following reason: " + current.u_reason_for_assigning + ".";
gs.log("Before " + gdt + gr.u_eligibility_date);
gr.u_eligibility_date = gdt;
gs.log("After " + gdt + gr.u_eligibility_date);
gr.u_last_upgrade_date = gs.now();
gr.update();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2015 01:28 PM
Hi Alex,
I updated your function code a little. Please try this:
- DELSetEligibilityOnAssignment();
- function DELSetEligibilityOnAssignment() {
- var gr = new GlideRecord('sys_user');
- gr.addQuery('sys_id', current.assigned_to);
- gr.query();
- var gdt = new GlideDateTime(gr.u_eligibility_date.getDisplayValue());
- gdt.addMonths(3);
- if (gr.next()) {
- if (current.u_reason_for_assigning == 'Lost') {
- gr.u_work_notes = "Eligibility date changed on " + current.asset_tag + " for the following reason: " + current.u_reason_for_assigning + ".";
- gs.log("Before :" + gr.u_eligibility_date.getDisplayValue());
- gr.u_eligibility_date = gdt.getValue();
- gs.log("After " + gr.u_eligibility_date.getDisplayValue());
- gr.u_last_upgrade_date = gs.now();
- gr.update();
- }
- }
- }
Regards,
Hardik Vora
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2015 03:23 AM
Appreciate everyones help on this. hardik.vora your suggestion did not work, looks like it blanks out the date in the user profile and i see the following in the logs.
Before 19-Nov-2015
After
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2015 02:30 PM
Then instead of direct assignment, can you try
gr.setValue('u_eligibility_date',gdt.getValue());
Thanks.
Regards,
Hardik Vora
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2015 02:17 AM
Hi Alex,
if you want , we can develop this functionality in demo instance and show you.
Please check this link Client Script Date/Time Functions .
function onLoad() {
var cdt = g_form.getValue('startdate'); //Choose the field to add time from
var addtime = 36; //The amount of time to add //you need to use months here
var addtype = 'hour'; //The time type. Can be second, minute, hour, day, week, month, year.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'addDateTimeAmount');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_addtime', addtime);
ajax.addParam('sysparm_addtype', addtype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
//You could then take the new Date/Time answer and set the value of another field.
// g_form.setValue('expected_start', answer);
//g_form.enddate.setDisplayValue(answer);
alert("enddate :" +answer);
g_form.setValue('enddate', answer);
}
}
Thanks & Regards
Govind Kumar Sharma

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2015 01:26 AM
Hi Alex,
The first and the biggest issue I see in your code is in line-
var gdt = new GlideDateTime(gr.u_eligibility_date.getGlideObject());
you will get undefined from gr.u_eligibility as you have just queried on the table but have not done gr.next() yet(i.e. you are not on a specific record yet)
Thanks,
Tanaji Patil
--Mark correct/helpful if it help you solving your issue.