Create Request from Incident by UI Action

andreas_holgers
Kilo Expert

My aim is to copy the information from an Incident to a new Request, and close the Incident, with an UI Action. Without going through the Service Portal (ours seems to have been set up in a strange way).

I have reused some of the code I have found in other threads about creating REQ from INC. It seems to do the thing I want, but I don't know if there is something missing in the code or there are any risks with the code. As I aren't that good at scripting/coding in ServiceNow I would like to have some feedback on my code.

2018-01-19_14-40-16.png2018-01-19_14-39-50.png

Thanks in advance for feedback on this.

/Andreas

3 REPLIES 3

sachin_namjoshi
Kilo Patron
Kilo Patron

HI Andreas,



Its better to use cart API to create request from incident.


Cart API is provided OOB by platform to create requests.



Service catalog script API



Please see below sample code of UI action which creates request from incident.



createServiceRequest();




function createServiceRequest() {


var cart = new Cart();


var item = cart.addItem('af695d47314610003e55ce5fcb8deb06');



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


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


cart.setVariable(item, 'sr_cmdb_ci', current.cmdb_ci);


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



var cartGR = cart.getCart();  


cartGR.requested_for = current.u_affected_person;


cartGR.update();  


var newSerReq = cart.placeOrder();


newSerReq.opened_by = current.caller_id;


newSerReq.update();




var activityLogMsg = 'Your Incident ' + current.number + ' has been cancelled and a Service Request ' + newSerReq.number + ' is created. No action is needed on your part';


current.state = 5;


current.active = false;


current.work_notes = activityLogMsg;


current.parent = getRITM(newSerReq.sys_id);


current.update();



var disMessage = 'Incident ' + current.number + ' has been cancelled and your work will be captured via the newly created Service Request ' + newSerReq.number;


gs.addInfoMessage(disMessage);


action.setRedirectURL(newSerReq);


action.setReturnURL(current);


}




function getRITM(serReq) {


var ritm = '';


var grRITM = new GlideRecord('sc_req_item');


grRITM.addQuery('request',serReq);


grRITM.query();


if(grRITM.next()) {


ritm = grRITM.sys_id;


}


return ritm;


}



Regards,


Sachin


Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi Andreas,



Hope you are doing well!



When i look at your code it seems to be good. I would like you to check if any insert business rules are configured on sc_request table. Which should not have further processing.



Also Want to ask one more thing, Will RITMs be added manually?



Regards,


Jaspal Singh
Mega Patron
Mega Patron

Hi Andreas,



Looking at your code it would just result in creation of Request & not Requested Item. So better to you code as suggested by sachin.namjoshi