Set one date field to 30 days ahead of another date field

richelle_pivec
Mega Guru

I am working on setting up a reminder notification that will be triggered when a scheduled job (that runs daily) finds today's date in a specific date field.

My first step is to get a field to have a date that is 30 days ahead of the "Valid_To" field. This field can be calculated by client script that updates whenever the record is saved. (So if someone changes the Valid_To field, this new field will change as well to a different date (now 30 days ahead of the updated Valid_To date).

Any ideas on how to set that script up? Or is there a better way to do this? I checked out the GlideSystem Date and Time Functions - ServiceNow Wiki   and none of them really do this.

thanks,

Richelle

1 ACCEPTED SOLUTION

Hi Richelle



Apologies. I've checked the code again against a simple date field and does not work if the field has that format


Try this one.



    var myValidTo = current.valid_to;                                                


    myValidTo = myValidTo.toString() + ' 00:00:00';


    var gdt = new GlideDateTime(myValidTo);


    gdt.addDays(30);


    var gdtStr = gdt.toString();


    var reminderOnStr = gdtStr.substring(8, 10) + '-' + gdtStr.slice(5,7) + '-' + gdtStr.substring(0, 4);


    current.u_reminder_on = reminderOnStr;


   


Apparently you need to reformat the date as requested by the system.


Robo


View solution in original post

11 REPLIES 11

Ivano B
ServiceNow Employee
ServiceNow Employee

HI Richelle



Please have a look to this http://wiki.servicenow.com/index.php?title=GlideDateTime#addDays.28int.29


Probably this is what you need to use.



Cheers


Robo


That looks like it might be what I need. Do I make it a client script and how do I put in the one field (valid_to) as the field to look at and the other field (days_to_warn) to populate?


I tried a couple of things, but my scripting isn't all that strong.



var gdt = new GlideDateTime("2011-08-31 08:00:00"); 
gdt.addDays(-1);
gs.print(gdt.getDate());

thanks,


Richelle


Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Richelle



GlideDateTime is a server side code and must be used through 'business rule' or something similar.


Instead of using a client script, you can use a 'BEFORE' business rule on UPDATE where the condition reflects the changes to the field


For example.



current.valid_to.changes() && !current.valid_to.nil()



It is also important that 'valid_to' is a date/time field with format (YYYY-MM-DD hh:mm:ss)


If i'm not wrong, something like this will work (assumption your new field is named u_reminder_on)



var myValidTo = current.valid_to;


var gdt = new GlideDateTime(myValidTo);


gdt.addDays(30);


current.u_reminder_on = gdt.getDisplayValueInternal();



I hope this will help.



Cheers


Robo


Alas, that valid_to field is just a date field. I played around with converting it to a date/time field, but it says it cannot be done. I also have a default value script running on that field that automatically populates it with 1 year from the date is is created. This is our preferred "Valid To" for creating new knowledge, though we allow that it might be updated...otherwise I could do something similar with this other field that needs to be 30 days/1 month ahead of that. But, if they change the "valid_to" date, this date needs to change too.



Thank you for your advice. It seems to be getting me in the right direction. I tried removing the "Time" from the GlideDateTime on the off chance that it might work, but I got this error:



Error running business rule 'Notification Date' on kb_knowledge:KB00392, exception: org.mozilla.javascript.EvaluatorException: Java constructor for "com.glide.glideobject.GlideDate" with arguments "string" not found. (sys_script.f960362d9835120095650134b11c9c07; line 5)



I also wasn't sure if the new field should be a date field, a date/time field, or a string field. I tried it each way and it didn't work for any of them. I also had the most success (i.e., got the error) when I did Run @ Client, and Before and checked Update...and added your condition script in.



Richelle