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

Mike Patel
Tera Sage

I had similar request to add eligible date for PC so i created business rule; You can do the same.



find_real_file.png


find_real_file.png


Thanks Mike.



Unfortunately that does not work for me. Our date field is formatted as 19-Nov-2015


Can you send me the screenshot of the business rule that you created and field that you would like to change.


Hi Mike,



The code in shown above. Basically the field "u_eligibility_date" contains a date in the format of 19-Nov-2015. I just need to add 3 months to that.