Inactivate catalog item with a scheduled job script

will_smith
Mega Guru

I have a simple scheduled job script that I want to inactivate a record producer catalog item at a specific time. Below is my script, but the active flag is still checked when I execute it. I have ran this through a background script by adding gs.print statements and it says the flag is flipping to false. Could someone please tell me what I am missing here?

var PwdResetCatItem = new GlideRecord('sc_cat_item');

PwdResetCatItem.addQuery('name','Password Reset');

PwdResetCatItem.addActiveQuery();

PwdResetCatItem.query();

while (PwdResetCatItem.next()) { // if the catalog item is still active, inactivate it

  PwdResetCatItem.setValue('active',false);

}

1 ACCEPTED SOLUTION

If you're just dealing with one Catalog Item, I'd probably not bother with a while statement at all.



var item = new GlideRecord('sc_cat_item');


item.get('<sys_id of Password Reset item>');


item.active = false;


item.update();


View solution in original post

13 REPLIES 13

will_smith
Mega Guru

So I kept reading and working on my script and I figured it out, but I don't understand WHY the change I made works... the changes I made are in bold, and I understand the update() - this updates the record with the changes made. What I'm confused on is the change from while (PwdResetCatItem.next()) to if (PwdResetCatItem.hasNext())???




var PwdResetCatItem = new GlideRecord('sc_cat_item');


PwdResetCatItem.addQuery('name','Password Reset');


PwdResetCatItem.addActiveQuery();


PwdResetCatItem.query();




if (PwdResetCatItem.hasNext()) { // if there is a record in the query


  PwdResetCatItem.setValue('active',false);


  PwdResetCatItem.update();


}


If you're just dealing with one Catalog Item, I'd probably not bother with a while statement at all.



var item = new GlideRecord('sc_cat_item');


item.get('<sys_id of Password Reset item>');


item.active = false;


item.update();


So I had it... and then I lost it. I updated my code to your suggestion Robert, and I see this stinkin' active flag is still checked. I am using the sys_id of the record producer.



Thoughts?


Would changing the Run from Once to OnDemand do anything?



I pressed Execute Now twice and now it's working? Am I not giving it enough time?