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

zica
Giga Guru

Rudenko,



HEre is a sample of the script that you can use for incident. The same can be user for the other table by changing the name of the table_name within a GlideRecord :



   


ApprovalReminder ();  
function ApprovalReminder ()   {  
var approval = new GlideRecord("incident");   // change the table name here for pb and change
approval.addQuery('resolved_at' , '<=' , gs.daysAgo365)); // 365 days => 1year
approval.query();      
while (approval.next()) {    
            approval='Hidden';
          approval.update();


}}


First you have to use a GlideREcord and not GlideAgregate which is proper to count record.



You cannot use active = true within the GlideRecord because once the record is resolved, it is set to false


P-Rudenko-SN
ServiceNow Employee
ServiceNow Employee

Thanks, Akb,



But why should we use approval here? I need to update a fields like caller id, first name, last name, email.



Regards,


Pavlo


zica
Giga Guru

approval is the variable name of the glide record. You can change it with the name you would like to;



For the approval within the while statement, if was a mistake , i went to quickly and copied and pasted. Sorry for the mistake



changeCallerNameIncident();  
function changeCallerNameIncident ()   {  
var incident_caller = new GlideRecord("incident");   // change the table name here for pb and change
incident_caller.addQuery('resolved_at' , '<=' , gs.daysAgo365)); // 365 days => 1year
incident_caller.query();      
while (incident_caller.next()) {    
            incident_caller.caller_id='Hidden';
          incident_caller.update();


}}


zica
Giga Guru

you can add all filds name you want to update within the while statement


incident_caller.email = 'www@aaa.com"


incident_caller.location = "aaaa"


incident_caller.first_name="bbb"