UI Action to Convert RITM to INC "Randomly" Fails to Cancel RITM

Dan13
Tera Expert

We created a button (UI Action) on the RITM form to quickly convert a request to an INC

Dan13_0-1696435063471.png

 

When we click the button, the stage is set to "Request Cancelled", the State is set to "Closed Incomplete" and the flow is cancelled.

Dan13_2-1696435497494.png

Dan13_3-1696435569770.png

This example closed/cancelled successfully.

 

The issue that we run into is when the flow doesn't stop right away and generates a bunch of open tasks before cancelling the flow. I've had this happen to the same person on the same ticket type where the button it works one time but not the other.

 

The only thing I can see that is consistent on tickets that generate tasks by mistake is the cancellation of the flow is run as Guest but I can't figure out why.

Dan13_4-1696437147175.png

Dan13_5-1696437356013.png

 

 

 

 

Here is the UI Action settings

Dan13_1-1696435162510.png

and the Script below that:

 
function cancelIncident() {
    var answer = confirm("Are you sure you want to Convert to Incident?");
    if (answer == true) {
        gsftSubmit(null, g_form.getFormElement(), 'convert_to_incident'); 
    } else {
        return false;
    }
}
 
var rejectconfirmDialog;
 
function loadConfirmDialog() {
var dialogClass = window.GlideModal ? GlideModal : GlideDialogWindow;
rejectconfirmDialog = new dialogClass("change_confirm_cancel", false, 648, 250);
rejectconfirmDialog.setTitle(new GetMessage().getMessage("Reject Task")); //Modify title to display on popup
rejectconfirmDialog.render();
}
 
function moveToCancel(notes) {
//g_form.setValue("state",1) Possible to set the state here.
g_form.setValue("work_notes", notes); //Pass reason worknotes to a field
rejectconfirmDialog.destroy();
gsftSubmit(null, g_form.getFormElement(), "UI action name"); // Call UI action to run server script
 
}
 
if (typeof window == 'undefined')
   setRedirect();
 
function setRedirect() {
    current.update();
    action.setRedirectURL(current);
}
 
current.state = 4;
current.stage = "Request Cancelled";
//current.work_notes="Converting to an Incident";
 
current.update();
 
var task = new GlideRecord('sc_task');
task.addQuery('request_item',current.sys_id);
task.query();
while(task.next())
{
task.state = 4;
task.update();
}
 
convert();
 
function convert() {
    var inc = new GlideRecord('incident');
    inc.initialize();
    inc.short_description = current.short_description;
    inc.description = current.description.getHTMLValue();
    inc.contact_type = 'self-service';
    inc.company = current.company;
    inc.opened_by = current.opened_by;
    inc.assignment_group = current.assignment_group;
    inc.assigned_to = current.assigned_to;
    inc.watch_list = current.watch_list;
    //inc.caller_id = current.request.requested_for;
inc.caller_id = current.requested_for;
    inc.location = current.request.requested_for.location;
    inc.impact = current.impact;
    inc.urgency = current.urgency;
    inc.priority = current.priority;
    //inc.comments = current.comments.getJournalEntry(-1);
    inc.work_notes = "Additional Comments from RITM" + current.comments.getJournalEntry(-1);
    inc.cmdb_ci = current.configuration_item;
    inc.business_service = current.business_service;
   // inc.description = current.variables.additional_details;
    
 inc.u_ritm = current.sys_id;
var variables = current.variables.getElements();
 
var str = '';
for (var i=0;i<variables.length;i++) {
var question = variables[i].getQuestion();
var variableLabel = question.getLabel();
var variableValue = question.getDisplayValue();
str = str + variableLabel + ' :- ' + variableValue + '\n';
}
 
inc.description = str;
 
    var sysID = inc.insert();
    gs.addInfoMessage("Request Item " + current.number + " converted to Incident");
    current.u_coverted_to_incident = sysID;
 
    if (current.hasAttachments()) {
        GlideSysAttachment.copy(current.getTableName(), current.sys_id, inc.getTableName(), sysID);
    }
    // inc.impact = current.impact;
    // inc.urgency = current.urgency;
    // inc.priority = current.priority;
    var url = inc.getTableName() + '.do?sys_id=' + sysID;
    gs.setRedirect(url);
}
 
 
2 REPLIES 2

Allen Andreas
Administrator
Administrator

Hi,

From initially looking at this, I would recommend that you consider cleaning up your code a little bit. There appears to be unused functions in this script that aren't called anywhere else, some of this could probably benefit from being in a function that gets called and I wouldn't recommend executing a current.update() until you truly need to (it's in there a couple of times -- unless that's on purpose). Also you have the button set to display on Insert of a brand new record too?


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Sorry, I am not the author of this script and am very new to writing/editing scripts. We had a managed service provider set this up for us, but we no longer have a contract with them to assist with these types of issues.

 

Maybe this should be the only time current.update() is used? Would this affect the flow unintentionally kicking off tasks after it has been cancelled?

current.state = 4;
current.stage = "Request Cancelled";
//current.work_notes="Converting to an Incident";
 
current.update();
 
Based on what you said around the functions, "convert" might be the only one being called? I don't see anything calling the 3 functions at the beginning of the script. Are those critical to the flow stopping when the button is pressed to convert the RITM to an INC and not generating more unintended SCTASKs on the RITM?
 
"Also you have the button set to display on Insert of a brand new record too?"
I'm not sure if you are asking if we are doing this or why we are doing this. I can't tell where in the code it would do that or why we wouldn't want that to happen if you mean the latter.
 
Thanks for your help!