The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Adding Integer to Date field to populate another Date field

Luis Nunez
Tera Expert

Hello all,

Need a little help, I am trying to populate date field from another date field which adds an integer.

I have tried business rules, client scripts, calculations on the field itself and nothing seems to work.

Scenario:

I have a set DateFieldA, an IntegerField with values 30, 60, 90 and an empty DateFieldB.

DateFieldB must be the result of DateFieldA minus IntegerField.

The only Business rule that works halfway is the below, however it also changes the date on the DateFieldA (which shouldn't) so at the end, DateFieldA and DateFieldB are the same.

DateFieldA = 30.05.2015

IntegerField = 30 Days (Value is 30)

DateFieldB = should result in 30.04.2015

Halfway working BR:

  1. var gdt = current.DateFieldA.getGlideObject();  
  2. var minus=current.IntegerField; //substitute your integer column names here  
  3. minus=minus*-1;  
  4. gdt.addDays(minus);  
  5. gs.addInfoMessage(gdt);  
  6. DateFieldB = gdt;

Any help will be highly appreciated!

Cheers,

Luis

1 ACCEPTED SOLUTION

Gurpreet07
Mega Sage

Let's do this in another way . You remember that yours partial working business rule was updating both of the dates . Use that kind of logic in following way



  1. current.DateFieldB = current.DateFieldA ;
  2. var gdt = current.DateFieldB.getGlideObject();
  3. var minus=current.IntegerField; //substitute your integer column names here
  4. minus=minus*-1;
  5. gdt.addDays(minus);

View solution in original post

10 REPLIES 10

I would like to add a log statement after addDays ... i.e.     gs.addInfoMessage(' Date Field B   : '+gdt).


Nothing worked... tried even gs.log also nothing logged. Something must be wrong.


Deleted business rule...


Will start from scratch. Could you try reproducing this on your end?


var gdt = new GlideDateTime(current.u_datea);


gdt.addDays(-1 * current.u_integerfield);


current.u_dateb = gdt.getDate();



Used the same script on before update business rule.. could not find any issues. I also changed integer field to string... and the date field to date/time. I am getting correct value all the time


Hi Luis,



Your following code is working for me as well in before business rule:



var gdt = new GlideDateTime(current.dateFieldA)


gdt.addDays(-1 * current.IntegerField);


current.dateFieldB = gdt.getDate();






Gurpreet07
Mega Sage

Let's do this in another way . You remember that yours partial working business rule was updating both of the dates . Use that kind of logic in following way



  1. current.DateFieldB = current.DateFieldA ;
  2. var gdt = current.DateFieldB.getGlideObject();
  3. var minus=current.IntegerField; //substitute your integer column names here
  4. minus=minus*-1;
  5. gdt.addDays(minus);