How to force the sys_created_on time when creating a record

e_wilber
Tera Guru

The below script works with exception to manually forcing the created on date. What I am doing is converting Incidents to Cases by recreating a Case record and transferring all the data over (including the comments and work notes). All the individual pieces are moving over BUT it's taking the current date/time instead of the original one.

On this journal table I am able to double-click in list view to manually set the date to a previous date just fine. In my script, it isn't honoring what I'm telling it to do though. How do I go about setting this field?

var newJrn = new GlideRecord('sys_journal_field');
newJrn.initialize();
newJrn.name = 'sn_customerservice_case';
newJrn.element = 'comments';
newJrn.element_id = cs;
newJrn.sys_created_by = jrn.sys_created_by.toString();
newJrn.sys_created_on = jrn.sys_created_on;
newJrn.value = jrn.value.toString();
var createdJrn = newJrn.insert();

6 REPLIES 6

Mike Patel
Tera Sage

You can't specify the created on date time on insert but you can update it after creation with something like

newJrn.value = jrn.value.toString();
var createdJrn = newJrn.insert();

var gr = new GlideRecord('sys_journal_field');

gr.get(createdJrn);

gr.sys_created_on = jrn.sys_created_on;

gr.update();

Hi Mike,

I did try that but I didn't get any success. In my test print it is showing the date I am trying to set it to so I am locating the correct record. Am I doing something wrong in my update?

 

gs.log('Comment: ' + jrn.value.toString());
var newJrn = new GlideRecord('sys_journal_field');
newJrn.initialize();
newJrn.name = 'sn_customerservice_case';
newJrn.element = 'comments';
newJrn.element_id = cs;
newJrn.sys_created_by = jrn.sys_created_by.toString();
newJrn.sys_created_on = jrn.sys_created_on;
newJrn.value = jrn.value.toString();
var createdJrn = newJrn.insert();

var findJrn = new GlideRecord('sys_journal_field');
findJrn.addQuery('sys_id', createdJrn);
findJrn.query();

if (findJrn.next()) {
gs.print('Found jrn to update: ' + findJrn + ' to: ' + jrn.sys_created_on);
findJrn.sys_created_on = jrn.sys_created_on;
findJrn.update();
}

Than most likely ACL issue. Make sure you can go to that record and change created on field.

Oddly enough, in list view I can update the created time and it saves successfully. I just can't do it when trying to run it through a script.