Convert Incident to Request via UI Action

_knightofmirror
Kilo Contributor

Hello,

I am trying to add a form button for a UI action that would convert an incident to a request.   I know there is an OOB UI action to do this, but it currently brings you to the catalog to select the service and then you have to fill out all the variables for it.   I have pieced together this script from other people who had similar questions on this forum and from the wiki.   Currently it creates the request and it populates it with some of the incident fields like I want.   I'm not great with JavaScript and I want to know if this is how I should even be going about this/thinking about this.   I could have the UI action create a request item instead of a request, if that is a better idea.   Can anyone please check this script and help advise.1

Requirements:

1. Create a Request from an Incident in one click

2. Prompt with notification for continue or not

3. Create request or request item (advise on this please)

4. Resolve current incident

5. Populate request with incident field values (at least important ones)

Thanks,

Sean

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

function createRequest(){  

var answer = confirm(getMessage("This will create a request from this incident.   Are you sure you want to do this?") );

  if(answer == true){  

      gsftSubmit(null, g_form.getFormElement(), 'ebc381cfdb5ae20038b272fc0f961923');   //Set this to your Action ID  

    }  

    else{  

          return answer;  

    }  

}  

//Ensure call to server-side function with no browser errors  

if (typeof window == 'undefined')  

    serverResolve();  

 

 

function serverResolve(){  

var req = new GlideRecord("sc_request");

req.short_description = current.short_description;

req.cmdb_ci = current.cmdb_ci;

req.description = current.description;

req.comments = current.comments;

req.priority = current.priority;

req.company = current.company;

req.sys_domain = current.sys_domain;

var sysID = req.insert();

current.request_id = sysID;

var mySysID = current.update();

//Close the current incident upon request creation

current.incident_state = IncidentState.RESOLVED;

current.update();

current.resolved_by = gs.getUserID();

gs.addInfoMessage("request " + req.number + " created");

gs.addInfoMessage("Incident" + incident.number + "has been resolved");

action.setRedirectURL(req);

action.setReturnURL(current);

}

16 REPLIES 16

On your confirm it should be the name of the action and not the sys_id.



gsftSubmit(null, g_form.getFormElement(),'46ac418ddbea660038b272fc0f96198');



Change to:



gsftSubmit(null, g_form.getFormElement(),'<action_name>');


I've tried both with the same result.


_knightofmirror
Kilo Contributor

This code is still breaking somewhere between the client confirm and the rest of the server side script.   This is the current iteration:





function createRequest(){




if (typeof window == 'undefined'){


    CreateRequest();


} else {


    var confirmed = confirm("This will create a request from this incident.   Are you sure you want to do this?");


    if (confirmed) {


          gsftSubmit(null, g_form.getFormElement(),'9a826665dba6a600c33c7bec0f961957');  


    }


    return false;


}


}




var sysidOfItem = '51c88100db96220038b272fc0f961954';


var cartId = GlideGuid.generate(null);


var cart = new Cart(cartId);


var item = cart.addItem(sysidOfItem);


cart.setVariable(item, 'title', current.short_description);


cart.setVariable(item, 'requested_for' , current.caller_id);


cart.setVariable(item, 'assignment_group', current.assignment_group);


var rc = cart.placeOrder();






message += 'Request ' + rc.number + ' created.<br />';






current.incident_state = '8';


current.resolved_by = gs.getUserID();


current.close_notes = "Request ("+ rc.number +") created in place of this incident";


current.work_notes = "Request ([code]<a href='/sc_request.do?sysparm_query=number="+ rc.number +"' target='_blank'>"+rc.number+"</a>[/code]) created inplace of this incident";


current.close_code = "Solved (Permanently)";


current.update();




message += 'Incident <a target="_blank" href="/incident.do?sysparm_query=number=' + current.number + '">';


message += current.number+ '</a> has been resolved.';


gs.addInfoMessage(message);






var request = new GlideRecord('sc_request');


if(request.get('number', rc.number)){


action.setRedirectURL(request.getLink());


}


action.setRedirectURL(getLink("rc.number"));


Hello Sean,


Di you got the solution?



For me the server side code is not working


Community Alums
Not applicable

Sean your script worked for me thank you so much.