Scheduled job for updating user data in incident, problem and change application

P-Rudenko-SN
ServiceNow Employee
ServiceNow Employee

Dear all,

I need to set up a conditional Scheduled job for updating user data in incident, problem and change application. The job has to check the following conditions: if incident, problem and change records is closed and it's more then 12 month already after the record is closed, then a script must run.

The script has to look for the user records (user id, caller id, first, last name, email etc.) in inc, prob, change applications and update these fields with a constant value called "hidden".

As I don't yet have a huge experience with scripting in SNow, would someone please help me to build a needed scripts.

As a beginning, I've created a scheduled job which is running dayli with a following condition:

var ga = new GlideAggregate('incident');

ga.addAggregate('COUNT');

ga.addQuery('active', 'true');

ga.addQuery('resolved_at', '<', gs.daysAgo(1));

ga.query();

ga.next();

ga.getAggregate('COUNT') !== '0';

For now I've put 1 day rather then 365 in order to test the script. I aslo haven't found Incident Closure date, therefore choosed "resolved at" field.

The following script I've set to run:

var inc = new GlideRecord('incident');

inc.get(inc_sys_id);   // Looking up an existing incident record where 'inc_sys_id' represents the sys_id of a incident record

inc.caller_id.first_name = 'Hidden';

inc.caller_id.last_name = 'Hidden';

inc.updateWithReferences();

I'd be appreciated for any help with the scripts for building it in a proper manner.

Thanks i advance,

Pavlo

1 ACCEPTED SOLUTION

zica
Giga Guru

Let us know if you have further info,


If not, please if you don't mind, mark the answer as helpful / correct so other people with the same query could find it easely


View solution in original post

16 REPLIES 16

Guys, is there any clear reference to see, which related fields are impacted in case of update/deletion?


zica
Giga Guru

I've tried Gurpeet, The caller field is waiting for a sys_id. If the sys_id does not exist, it does not take into account and does not update the caller field.


There is no way to update the sys_user table ...


Try it, try to put a string like 'testingtoUpdateRecord' and try to update an incident from a background script and see if the record gets updated ....


the incident wont be updated and sys_user table neither


use updateWithRererences()


zica
Giga Guru

Pavlo,



You can use the script I provided above, if you want to ensure that you are retrieving the correct field, please comment out the update row and instead, try to put a gs.log('incident ....' + incident_caller.number) which will display the incident number that will be updated.



I assume that 'hidden' is a user that exist in your sys_id table. Caller_id is a referenced field. It means that the value you give to this field will be checked first under sys_user table. If the sys_id does not exist, the incident WILL NOT be just updated. Try to put a ramdon string under caller field in an open incident and try to save the incident, you will see that it wont be saved and neither the caller name will be changed.



If the sys_id Exist, the caller name will be replaced with the new sys_id... In other words, the previous caller name WILL NOT be repalced in SYS_USER TABLE, but only will be replaced by the NEW caller name within incident table


P-Rudenko-SN
ServiceNow Employee
ServiceNow Employee

Thank you both so much, guys,



I will work on this on an probably come back with an additional questions.



Regards,


Pavlo