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

Hi Richelle



Even if you have just a date and not date time should be ok, because you can simply use your date as string and add the time as ' 00:00:00'


The important thing is the format of the date. Must be YYYY-MM-DD.



For instance supposing valid_to has this format '2016-02-24'



var myValidTo = current.valid_to                                                 // '2016-02-24'


myValidTo = myValidTo.toString() + ' 00:00:00'; // '2016-02-24 00:00:00'


var gdt = new GlideDateTime(myValidTo);


gdt.addDays(30);


current.u_reminder_on = gdt.getDisplayValueInternal();



Did you check your date or date/time format ?



GlideDateTime method recognises only that kind of format and I suppose this is the reason why the code didn't work.


Anyway I tried the code before providing it to you on my instance so it should work.



Robo


My date field is DD-MM-YYYY.


Is there some code I can add to convert just this field? My users are used to seeing the date field in dd-mm-yyyy format and probably would reject me changing it system wide.



I found this code in another thread (Re: Convert LDAP date of dd/mm/yyyy to SN date of yyy-mm-dd) and am wondering if I could apply it somewhere in this script...



Richelle


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


It works! But only when I actually change that Valid_to date...so it's not working when a new article gets created and that field is being auto-populated with a date one year out. I'm betting that has something to do with this condition...



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



I tried it without the condition, but it still only worked when it was changed.



thanks much,



Richelle


Hi Richelle



Did you try to to change the business rule in order to be not just on UPDATE but also on INSERT ?


Maybe this is the reason why it does not work on INSERT.



Cheers


Robo