Change User Role "sys_created_on" value

TerryC03
Tera Expert

Hello,

I'm trying to change for testing purposes the date that a user was given a role. For example, if a user has the role of 'itil', I would like to change the "sys_created_on" value for the role on the sys_user_has_role table to 120 days before today. This is what I have currently (this is done in a PDI). I'm also running this as a background script:

 

 
var grUserRole = new GlideRecord('sys_user_has_role');
grUserRole.addQuery('role.name', 'itil'); //Filter to only itil role
grUserRole.addQuery('user.name', 'Chuck Tomasi');
grUserRole.query();

var days = new GlideDateTime();
days.addDaysUTC(-120);

while (grUserRole.next()) {

    grUserRole.setValue('sys_created_on', days);
    grUserRole.autoSysFields(false); //Allow manual update of sys fields
    grUserRole.update();

    gs.info('Updated: ' + grUserRole.user.name + ' | Role: ' + grUserRole.role.name + ' | SysCreatedOn: ' + grUserRole.role.sys_created_on);
}
 
When I visit the User Role page for 'itil' under the user Chuck Tomasi, the date doesn't change for the Created On field, under the User Role title at the upper left header of the page. Any help would be appreciated. 
9 REPLIES 9

G Ponsekar
Mega Guru

Hi @TerryC03 ,

 

As this is for testing purpose, can you try adding

grUserRole.setWorkflow(false);
in your script and check
 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks, GP

Ankur Bawiskar
Tera Patron
Tera Patron

@TerryC03 

is this for a business requirement or for your learning purpose?

script seems fine to me

Did you add log and see what error came?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

OlaN
Giga Sage
Giga Sage

Hi,

In general I would advice not to alter the system generated fields.

 

If you really want to proceed, then the issue is that the field expects a string, not a GlideDateTime object.

Change one thing and it will work.

// change this line
grUserRole.setValue('sys_created_on', days);

// into this
grUserRole.setValue('sys_created_on', days.getValue());

my gs.info() log is printing:

*** Script: Updated: Chuck Tomasi | Role: itil | Role: 2005-02-19 01:12:11

But it's not showing that on the User Role page.