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

Hello Rajini,



Can you share me the code


Community Alums
Not applicable

Saranya,



Here is the one I was trying..But did not implement it because our requirements have changed and not finalized. Hope this will help you to get started at least.



UI Action: Create Request



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(), "create_request_from_incident");


      }


      else{


          return false;


      }


}



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


if (typeof window == 'undefined'){


    CreateRequestNow();


}



function CreateRequestNow(){


      var message ='';


      var sysidOfItem = 'fafb7b61370c220006c11f9543990e61'; //SysID of a form. (For Example: Service Desk Team Service Request Form)


      var cartId = GlideGuid.generate(null);


      var cart = new Cart(cartId);


      var item = cart.addItem(sysidOfItem);


      cart.setVariable(item, 'vs_application', current.u_applicationitem);


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


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


      cart.setVariable(item, 'req_description', current.description);


      var rc = cart.placeOrder();


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



      current.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.u_reason_code = 'User Error';


      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());


      }


}




UI Action: Create Incident



function createIncident(){


      if(confirm("This will create an incident from this request.   Are you sure you want to do this?")){


              gsftSubmit(null, g_form.getFormElement(), "create_incident_from_request");


      }


      else{


          return false;


      }


}



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


if (typeof window == 'undefined'){


    CreateIncidentNow();


}



function CreateIncidentNow(){


      var message ='';


      var inc = new GlideRecord('incident');


      inc.initialize();


      inc.caller_id = current.u_requested_for;


      inc.u_applicationitem = 'servicenow';


      inc.short_description = 'This is short description';


      inc.description = 'This is description';


     


      var sctask = new GlideRecord('sc_task');


      sctask.addQuery('request_item', current.sys_id);


      sctask.query();


      if(sctask.next()){


              inc.assignment_group = sctask.assignment_group;


              inc.assigned_to = sctask.assigned_to;


      }


      inc.insert();


      message += 'Incident ' + inc.number + ' created.<br />';


     


      var sc_task = new GlideRecord('sc_task');


      sc_task.addQuery('request_item', current.sys_id);


      sc_task.addQuery('state', '<', '3');


      sc_task.query();


      while(sc_task.next()){


              sc_task.state = '4'; //Closed Incomplete


              sc_task.update();


      }      


     


      inc.work_notes = "Incident ([code]<a href='/incident.do?sysparm_query=number="+ inc.number +"' target='_blank'>"+inc.number+"</a>[/code]) created in place of this request";      


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


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


      gs.addInfoMessage(message);



      var incident = new GlideRecord('incident');


      if(incident.get('number',inc.number)){


              action.setRedirectURL(incident.getLink());


      }


}





Thanks,


Rajini


Thank you very much Rajini


Hi Rajini,



I have same requirement so we need to give any condition for this please advice.



Regards,


Ram


Community Alums
Not applicable

As I said above, I did not implement this so not sure of the end result. My guess it should work and any enhancements/upgrades you should be able to handle. It would be easy..



Try. If you have any hassles, developers here will help you to fix.



Thanks,