Wednesday, August 17, 2016

ServiceNow Function to Convert Incident to Service Request


function createRequest(incident) {
   var sc_req = new GlideRecord ("sc_request");
   sc_req.initialize();
   sc_req.u_requested_by = current.u_contact_person;
   sc_req.u_requested_by_phone = current.u_contact_phone;
   sc_req.location = current.location;
   sc_req.requested_for = current.caller_id;
   sc_req.phone = current.u_customer_phone;
   sc_req.u_computer_name = current.u_computer_name;
   sc_req.short_description = current.short_description;
   sc_req.description = current.description;
   sc_req.contact_type = current.contact_type;
   sc_req.assignment_group = current.assignment_group;
   sc_req.u_business_service = current.u_business_service;
   sc_req.category = current.category;
   sc_req.subcategory = current.subcategory;
   sc_req.cmdb_ci = current.cmdb_ci;
   sc_req.assigned_to = current.assigned_to;
   sc_req.parent = current.sys_id;
   sc_req.close_notes = current.close_notes;
   sc_req.u_phi_details= current.u_phi_details.getJournalEntry(-1);// -1 gets all journal entries as a string
   sc_req.work_notes = current.work_notes.getJournalEntry(-1);
   var reqSysID = sc_req.insert();

   GlideSysAttachment.copy('incident', current.sys_id, 'sc_request', reqSysID);
   gs.eventQueue("incident.to.request", sc_req, sc_req.number, incident.number);

   gs.addInfoMessage('Request Created: ' + sc_req.number);
   action.setRedirectURL(sc_req);
   
   if (incident.assigned_to.nil()) {
      incident.assigned_to = gs.getUserID();
   }
   incident.state = 7;
   incident.active = false;
   incident.category = 'Request';
   incident.close_code = 'Moved to Request';
   incident.close_notes = 'Moved to Request: ' + sc_req.number;
   incident.comments = 'Moved to Request: ' + sc_req.number;
   incident.parent = reqSysID;
   incident.update();
   gs.addInfoMessage('Incident Closed: ' + incident.number);
}