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

david_legrand
Kilo Sage

I think it's because your gdt IS the datefieldA object.



try by using var gdt = new GlideDateTime(current.dateFieldA) (I'm saying it by memory) so you'll have a "completely new gdt GlideDateTime object" to manipulate



regards


Hi David,


Thanks for the suggestion, I had already tried that as well with no positive results...


Had also tried:


var gdt = new GlideDateTime(current.dateFieldA)


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


current.dateFieldB = gdt.getDate();




Didn't work either... any other suggestions?


if you are copy, paste the above code then you are missing semi colon (;) in first instruction .Also date field accept time also and in last instruction ... assign gdt



var gdt = new GlideDateTime(current.dateFieldA) ;


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


current.dateFieldB = gdt ;        


//   if its not working ... try to replace last instruction with             current.dateFieldB = gdt.toString()
;  


Hi Gurpreet,


The first was not copy paste... double checked just in case


Tried amending the last instruction and still nothing shows up on the empty date field.


Checked for typos as well...


Tried with before and after update / insert BRs...   still same result.



Other suggestions?