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

Yep. I've tried it a couple of different ways now...so far no luck. On Before, After, Async (with changing script starts)...at Client at Server...with the condition script and without it. Both Insert and Update Checked. Even tried it with just half the script (!current.valid_to.nil()).



The magic combo hasn't been found yet.



Richelle


I got it. Under the "When to run," I have:


when to run.png



And I am using Run at: "client."



And under "Advanced," I have:


advanced.png



It works both on Insert (and Save) of new knowledge article and on Change of the Valid_to date in a knowledge article.



Thanks again for all of your help,



Richelle