Convert Incident to Request via UI Action
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2016 05:36 PM
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);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2016 06:40 PM
I would not insert a record like thAt. I woul use the catalog API. http://wiki.servicenow.com/index.php?title=Service_Catalog_Script_API
Benefit it being it makes a item and req like all others so works with approvals and all that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2016 05:08 AM
I have the cart API part functioning correctly now, thanks.
From the wiki I don't see how I can redirect to the new sc_request or item. I mean I know how to redirect, I just don't know how to reference the new request or item.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2016 10:01 AM
You should be able to just prompt the UI Action the with the new item if you are using the cartAPI:
var rc = cart.placeOrder();
action.setRedirectURL(rc);
gs.addInfoMessage(gs.getMessage('Created new Request ' + rc.number));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2016 05:24 PM
Hi Jacob, the server script is working thanks to Jace's help. But I still cannot get the both the client/confirm box and the server script to work together. If I comment out the confirm, the server script works fine. So I know the issue is somewhere there. Here is my new updated UI Action:
In the onClick box in the form: createRequest()
--------------------------------------------------------------------------
function createRequest(){
if(confirm("This will create a request from this incident. Are you sure you want to do this?"));{
gsftSubmit(null, g_form.getFormElement(),'46ac418ddbea660038b272fc0f96198');
}
else{
return false;
}
}
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined'){
CreateRequest();
}
function CreateRequest(){
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 in place 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());
}
}