Add 3 months to date.

arobertson
Tera Guru

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();

  }

  }

}

24 REPLIES 24

HV1
Mega Guru

Hi Alex,



I updated your function code a little.   Please try this:



  1. DELSetEligibilityOnAssignment();  
  2.  
  3. function DELSetEligibilityOnAssignment() {  
  4.  
  5.   var gr = new GlideRecord('sys_user');  
  6.   gr.addQuery('sys_id', current.assigned_to);  
  7.   gr.query();  
  8.  
  9.   var gdt = new GlideDateTime(gr.u_eligibility_date.getDisplayValue());  
  10.   gdt.addMonths(3);  
  11.  
  12.   if (gr.next()) {  
  13.   if (current.u_reason_for_assigning == 'Lost') {  
  14.   gr.u_work_notes = "Eligibility date changed on " + current.asset_tag + " for the following reason: " + current.u_reason_for_assigning + ".";  
  15.   gs.log("Before :" + gr.u_eligibility_date.getDisplayValue());  
  16.   gr.u_eligibility_date = gdt.getValue();  
  17.   gs.log("After " + gr.u_eligibility_date.getDisplayValue());  
  18.   gr.u_last_upgrade_date = gs.now();  
  19.   gr.update();  
  20.   }  
  21.   }  
  22. }  


Regards,


Hardik Vora


arobertson
Tera Guru

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


Then instead of direct assignment, can you try


gr.setValue('u_eligibility_date',gdt.getValue());



Thanks.


Regards,


Hardik Vora


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


Tanaji Patil
Tera Guru

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.