Blog

Copy Incident UI Action

Here is a nice way to copy an incident.  Using a GlideDialog window, it copies the incident information, allows a new caller, and also copies the attachments.

Screenshots

1. Source Incident (Click to expand)

2. Copy Incident UI Page (Click to expand)

3. New Incident (Click to expand)

UI Page: copy_incident

UI Page: copy_incident (HTML)

<?xml version="1.0" encoding="utf-8"?>
<j:jelly xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null" trim="false">
<form action="ui_page_process.do">
<input type="hidden" name="name" value="copy_incident"/>
<input type="hidden" id="cancelled" name="cancelled" value="false"/>
<g:evaluate>
var source_incident = '$';
</g:evaluate>
<table>
<tr>
<td nowrap="true" id="my_label" class="label" type="string" choice="0"><span id="status." class="mandatory label_description" mandatory="true" oclass="mandatory">$[SP]</span><label for="script_table_name" onclick="" dir="">New Caller:</label></td>
<td nowrap="true"><g:ui_reference id="caller_field_dialog" name="caller_field_dialog" table="sys_user" question_reference="sys_user"/></td>
</tr> 
<tr />
<tr>
<td align="right" colspan="2">
<g:dialog_buttons_ok_cancel ok="return validateForm();" cancel="return onCancel();"/>
</td>
</tr>
</table>
<input type="hidden" name="source_incident" id="source_incident" value="$"/>
</form>
</j:jelly>

UI Page: copy_incident (Client Script)

function onCancel() {
var c = gel('cancelled');
c.value = "true";
GlideDialogWindow.get().destroy();
return false;
}

function validateForm() {
if (gel('caller_field_dialog').value == '') {
alert("${JS:gs.getMessage('Please input Caller')}");
return false;
}
}

UI Page: copy incident (Processing Script)

if (cancelled == "false") {
var grIncident = new GlideRecord("incident");
grIncident.get(source_incident);
grIncident.state = 1;
grIncident.caller_id = caller_field_dialog;
grIncident.opened_at = gs.nowDateTime();
grIncident.opened_by = gs.getUserID();
grIncident.number = '';
var oldIncidentSysID = grIncident.sys_id.toString();
var newIncidentSysID = grIncident.insert();
//GlideSysAttachment.copy('incident', 'oldIncidentSysID', 'incident', 'newIncidentSysID');
//Copy Attachments
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id', oldIncidentSysID);
attachment.query();
while (attachment.next()) {
var attachCopy = new GlideRecord('sys_attachment');
attachCopy.table_sys_id = newIncidentSysID;
attachCopy.file_name = attachment.file_name;
attachCopy.content_type = attachment.content_type;
attachCopy.table_name = attachment.table_name;
attachCopy.size_bytes = attachment.size_bytes;
attachCopy.compressed = attachment.compressed;
attachCopy.size_compressed = attachment.size_compressed;
attachCopy.created = gs.nowDateTime();
attachCopy.created_by = gs.getUserID();
attachCopy.updated = attachment.updated;
attachCopy.updated_by = attachment.updated_by;
attachCopy.updates = attachment.updates;
//attachCopy.setWorkflow(false);
attachCopy.insert();
}
redirectTo(newIncidentSysID);
}

function redirectTo(sysID) {
var urlOnStack = "incident.do?sys_id=" + newIncidentSysID;
response.sendRedirect(urlOnStack);
}


UI Action: Copy Incident

UI Action: Copy Incident
Table: Incident [incident]
Form Context Menu: true
List Context Menu: true
Client: true
Show insert: false
Show update: true
Condition: gs.hasRole("itil")
OnClick: popCopyIncident()
Script:
function popCopyIncident() {
   var sysId = typeof rowSysId == 'undefined' ? gel('sys_uniqueValue').value : rowSysId;
   var gDialog = new GlideDialogWindow('copy_incident');
   gDialog.setPreference('sysparm_sysID', sysId);
   gDialog.setPreference('sysparm_table', "incident");
   gDialog.setTitle('Copy the Selected Incident');
   gDialog.render();
}