How to update the created date ,2 days ago

Rajababu
Giga Guru

How to update the created date ,by past 2 days ago from current date on incident .

For example if incident0123  created 23-03-2021 - 2 days  =incident0123 created 21-03-2021

How to achieve this.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Rajababu 

you can use this script to set the Created Date/Time 2 days from it's current Created Date/Time

var inc = new GlideRecord('incident');

inc.addQuery('number', 'INC001');

inc.query();

if(inc.next()){

var gdt = new GlideDateTime(inc.sys_created_on);

gdt.addDaysUTC(-2);

inc.sys_created_on = gdt;

inc.autoSysFields(false);

inc.setWorkflow(false);

inc.update();

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Omkar Mone
Mega Sage

Hello

 

Please try this script in background script and let me know if it worked.

var dt = "23-03-2021";

var gdt = new GlideDate();
gdt.setDisplayValue(dt);
gdt.addDays(-2);
gs.print(gdt.getValue());

 

Regards,

Omkar.

Ravi Peddineni
Kilo Sage

Create a before business rule on insert of the record and use the below code:

var gdt = new GlideDate();
gdt.addDays(-2);
gs.print(gdt.getValue());
current.sys_created_on = gdt.getDisplayValue();
current.autoSysFields(false);

For updating System fields, you have use current.autoSysFields(false) to not get updated by system.

Ankur Bawiskar
Tera Patron
Tera Patron

@Rajababu 

you can use this script to set the Created Date/Time 2 days from it's current Created Date/Time

var inc = new GlideRecord('incident');

inc.addQuery('number', 'INC001');

inc.query();

if(inc.next()){

var gdt = new GlideDateTime(inc.sys_created_on);

gdt.addDaysUTC(-2);

inc.sys_created_on = gdt;

inc.autoSysFields(false);

inc.setWorkflow(false);

inc.update();

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

It's not updating for particular user .