Background Script to update a field on HR Case Form

Rob Sestito
Mega Sage

Hello SN Comm!

I am in need of some help in writing a background script, in order to update a number of records for a specific field.

Records: HR Case

Table: sn_hr_core_case

Field: u_cancellation_reason

Reason needed:

We have had to use the transfer function A LOT. When an email comes into the system, it generates an HR Case under General Inquiry. Our Employee Resource Center (ERC) Reps then take on those General Cases. They then move those Cases into the proper COE along with its proper HR Service. The State of the Original Case then becomes 'Cancelled'. We have a Cancellation Reason Field on the HR Case Form, for when the Cancel UI Action is used (in other scenarios). However, when doing certain reporting, looking at Cancelled State, all those Transferred Cases fall into that bucket, with their Cancellation Reason choice, showing as blank of course. It has been requested by the business, for me to make it that when a Case is Transferred, the Cancellation Reason Field auto-populate as a new choice (that I added in to the field), to be labeled 'Transferred'. While that is all done now from the UI Page 'Transferred Case', I now need to make all the old HR Cases that were transferred, to populate with the choice as Transferred. We have a little over 3k Cases that need to be updated.

What is needed:

With the information provided above, can anyone help me out and configuring a background script that I could run (to test in a non-prod instance), in order to update those records that need to be updated? And if any other info is needed please let me know.

Thanks so much in advance!

-Rob

1 ACCEPTED SOLUTION

Rob Sestito
Mega Sage

Hey Tyler,

Your help definitely got me in the right direction to say the least. I greatly appreciate your help on this. Just in case others come across this question and they too are using HR Case table, I am going to post the script here and mark this as correct and mark your replies as helpful.

Again, Thank you so much!

 

Using Fix Scripts: I set a case limit for testing purposes, otherwise it is not needed.

Setting the value over the label is the way to go (unless you have made your values the same as your labels).

Be sure that system fields such as 'Updated' and 'Updated By' will update. If you don't want that to happen (as I don't), you would need to add that to your script

Also, add in a line if you do not want any business rules to run (just in case you have have some and are not 100% when they might be triggered)

 

var hrcase = new GlideRecord('sn_hr_core_case');

hrcase.addQuery('state', '7');

hrcase.addNullQuery('u_cancellation_reason');

hrcase.setLimit(15); // Set limit for testing purpose. Comment out after testing has been confirmed successful

hrcase.query();

while(hrcase.next()) {

        hrcase.u_cancellation_reason = '7';

        hrcase.setWorkflow(false); // do not trigger business rules

        hrcase.autoSysFields(false); // do not update system fields

        hrcase.update();

}

 

Thanks to Tyler.Hoge for getting me in the right direction! And I hope this helps for anyone else that may need it!

Cheers!

-Rob

View solution in original post

7 REPLIES 7

Tyler Hoge - Gl
Tera Guru

This should get you in the right direction. you can add or change the state query that I put in to line up with the records in your instance.

 

var case = new GlideRecord('sn_hr_core_case');
case.addQuery('state', 'Cancelled');
case.addQuery('u_cancellation_reason', '');
case.query()
while(case.next()){
    case.u_cancellation_reason = 'put your reason here';
    case.update();
}

Thanks for replying Tyler!

I will give this a shot and test in my non-prod. I started trying to code this myself, and was somewhat close to what you have. But, yours looks more correct for sure.

I will keep you posted!

Thank you, 

-Rob

Hey Tyler,

Your state line is correct with our instance.

 

I ran the script and received the following error back.

JavaScript compiler exception: missing variable name (null.null.script; line 1) in:

var case = new GlideRecord('sn_hr_core_case');

case.addQuery('state, 'Cancelled');

case.addQuery('u_cancellation_reason' , ' ');

case.setLimit(10); // (I added in this line for testing purposes)

case.query()

while(case.next()) {

case.u_cancellation_reason = 'Transferred';
case.update():

}

 

I then tried changing case.addQuery to case.addNullQuery(u_cancellation_reason);

And I received the same exact message..

Thoughts?

Thanks,

-Rob

Having a conversation about this to a co-worker. It might be due to how background scripts run in global, and I am trying to mess with hr core case stuff..

i am going to try this in fix script which is on the hr table.

thanks,

Rob