Fix Script Changes not Committing
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-12-2013 06:19 PM
I've been testing this for a few days and am wondering if I'm missing something simple like .update() because the record states are not actually changed. A couple of our workflows closed RITMs with the wrong state. These are relatively simple, with only one task, so the fix script should check if the task is closed complete and also sets the parent RITM to closed complete. It seems to run, and reports the corrected values, but when I browse the sc_req_item records, the state has not updated. (I also added notes to the work log which commit successfully.) Suggestions appreciated on why this doesn't seem to be committing in the actual sc_req_item record or if I should be taking another approach?
function fixRITMsSkippedwithCompleteKids() { var brokenRequests = []; var tskrec = new GlideRecord("sc_task"); // Search Catalog Tasks tskrec.addQuery('active', false); tskrec.addQuery('request_item.state', 7); //with Closed Skipped parent RITM tskrec.addQuery('request_item.sc_cat_item', 'IN', '11154eb787962000825e10c56a434dd5,1a14cc5ef9d9e400825ec10e8623a8fb'); //Only certain types of catalog requests, the ''ITIL only request'' and the all users 'Submit a Request' tskrec.addQuery('state', 3); // and Closed Complete Catalog Task tskrec.setLimit(10); //Limit the damage if this goes awry tskrec.query(); while (tskrec.next()) { gs.log(tskrec.request_item.number + ' Before Rec# ' + tskrec.number + ' Task State: ' + tskrec.state + ' parent RITM state: ' + tskrec.request_item.state + ' ' + tskrec.request_item.state.getDisplayValue()); brokenRequests.push(tskrec.request_item.sys_id); //Add each request item to the list of broken request item records } //Iterate through the list (array) of broken RITM records and set each to closed complete state (3) for (var i = 0; i < brokenRequests.length; i++) { var rec = new GlideRecord("task"); //Tasks — get the same result using sc_req_item rec.addQuery('sys_id', brokenRequests<i>); rec.query(); while (rec.next()) { rec.setworkflow(false); //Should skip business rules for this update - works in workflow; seems not in fix scripts since if I run this repeatedly, it says something about slow business rules . . . rec.state = 3; rec.work_notes = ("Closing Complete due to closed complete task. (" + '<pre __default_attr="plain" __jive_macro_name="code" class="jive_text_macro jive_macro_code"><p><a href="nav_to.do?uri=incident.do?sysparm_query=number=INC0028576' + '" target=newProdINC0028576' + ' style="color:003366" "> <u>INC0028576</u> ' + '</a></p></pre>)'); rec.update(); //This commits the update but does not seem to be rec.setworkflow(true); gs.log(rec.number + ' After ' + rec.number + ' state: ' + rec.state + ' ' + rec.state.getDisplayValue()); } } } fixRITMsSkippedwithCompleteKids();
Maddening thing is, it suggests it's working (plus the link shows up in the work notes to say it's being fixed, but the commit doesn't seem to 'stick' for the state as it does for the work notes update). Thanks in advance for any suggestions!
-mg

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-12-2013 10:24 PM
A couple of things.
setworkflow should actually be setWorkflow(false) (There is a capital W in workflow in the correct method).
You also probably don't need to do a setWorkflow(true) when you're done.
For the line, brokenRequests.push(tskrec.request_item.sys_id), you may want to log this out, but I'd suspect you have an array with the same sys_id in each element. If this is the case, cast it toString() or add + "" to the end to fix the issue and you're actually updating like you think you should be, but only the record with the sys_id repeated in the array.
Example: brokenRequests.push(tskrec.request_item.sys_id + ""); or brokenRequest.push(tskrec.request_item.sys_id.toString());
Lastly, I do see you have a gs.log in the first part of your code, if that is logging out correctly, comment it out and add extra logging under it and see if you're actually getting the output you'd expect. Sometimes stepping through each block and adding a gs.log or gs.print will help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-13-2013 07:43 AM
Thanks! That did the trick! 🙂
function fixRITMsSkippedwithCompleteKids(){
var brokenRequests=[];
var tskrec = new GlideRecord("sc_task"); //Catalog Tasks
tskrec.addQuery('active', false);
tskrec.addQuery('request_item.state',7); //Closed Skipped parent RITM
tskrec.addQuery('request_item.sc_cat_item','IN','11154eb787962000825e10c56a434dd5,1a14cc5ef9d9e400825ec10e8623a8fb'); //Only certain types of catalog requests, the ITIL only request and the all users Submit a Request
tskrec.addQuery('state',3); //Closed Complete Catalog Task
tskrec.setLimit(10);
tskrec.query();
while (tskrec.next()) {
// gs.log(tskrec.request_item.number+' Before Rec# '+tskrec.number+' Task State: '+tskrec.state+' parent RITM state: '+tskrec.request_item.state+' '+tskrec.request_item.state.getDisplayValue());
brokenRequests.push(tskrec.request_item.sys_id.toString());
}
for (var i=0; i< brokenRequests.length; i++) {
var rec = new GlideRecord("task"); //Tasks
rec.addQuery('sys_id', brokenRequests<i>);
rec.query();
while (rec.next()) {
rec.setWorkflow(false);
rec.state=3;
rec.work_notes=("Closing Complete due to closed complete task. (" +'<pre __default_attr="plain" __jive_macro_name="code" class="jive_text_macro jive_macro_code"><p><a href="nav_to.do?uri=incident.do?sysparm_query=number=INC0028576'+'" target=newProdINC0028576'+' style="color:003366" "> <u>INC0028576</u> '+'</a></p></pre>)');
rec.update(); //This commits the update
gs.log(rec.number+' After '+rec.number+' state: '+rec.state+' '+rec.state.getDisplayValue());
}
}
} fixRITMsSkippedwithCompleteKids();