Skip to main content

ServiceNow Code a 'cancel and copy to new change type' ui action

 typical usage:

- the change is at change manager approval stage, and the change manager wishes to reject the change due to the wrong change type having been selected. The original change will be cancelled, and the fields copied to a new change. As part of the copy the user will be presented with a dialog to select the new change

via UI Action

UI Action code:

condition: 

gs.hasRole('change_manager') && ( gs.getUser().isMemberOf('Change Management') || gs.hasRole('admin') ) && ChangeUtils.isCopyChangeEnabled(current)


script:

function confirmAction() { var gmConfirm = new GlideModal('confirm_change_cancel_copy'); gmConfirm.setTitle('Confirm your cancellation!'); gmConfirm.setPreference('title', getMessage('chg.cancel_copy_warning_msg')); gmConfirm.setPreference('warning', 'true'); gmConfirm.setPreference('onPromptComplete', OnCopyChangeClick); gmConfirm.render(); } function OnCopyChangeClick() { function addParam(form, name, val) { var inp = cel('textarea', form); inp.name = name; inp.value = val; } cancelCurrentChange(); var srcSysId = g_form.getUniqueValue(); var ga = new GlideAjax('ChangeUtils');//--might need to take a copy to add new function ga.addParam('sysparm_name', 'getChangeQueryParams'); ga.addParam('sysparm_src_sysid', srcSysId); ga.addParam('sysparm_new_chg_type', g_scratchpad.new_chg_type); ga.setWantSessionMessages(true); ga.getXMLAnswer(function (queryParam) { if (queryParam) { var gotoURL = new GlideURL('CopyChangeRelatedLists.do'); gotoURL.setEncode(false); gotoURL.addToken(); gotoURL.addParam('srcSysID', srcSysId); gotoURL.addParam('newSysID', '$sys_id'); gotoURL.addParam('sysparm_returned_action', '$action'); var form = cel('form', document.body); hide(form); form.method = "POST"; form.action = g_form.getTableName() + ".do"; if (typeof g_ck != 'undefined' && g_ck != "") addParam(form, 'sysparm_ck', g_ck); addParam(form, 'sys_id', '-1'); addParam(form, 'sysparm_query', queryParam); addParam(form, 'sysparm_goto_url', gotoURL.getURL()); form.submit(); } }); } function cancelCurrentChange() { // new ChangeRequestStateHandler(current).moveTo("canceled"); var ga = new GlideAjax('GlideRecordUtil'); //--might need to take a copy to add new function ga.addParam('sysparm_name', 'setFieldsInRecordInTable'); ga.addParam('sysparm_table_name', g_form.getTableName()); ga.addParam('sysparm_record_sys_id', g_form.getUniqueValue()); ga.addParam('sysparm_field_names', 'state,close_code,close_notes'); var close_notes = getMessage('chg.cancel_copy_close_notes'); ga.addParam('sysparm_field_values', '4,canceled,'+close_notes); // Set state of current change to 'Cancelled' ga.getXML(function(response) {}); }





UI Page: confirm_change_cancel_copy html
<style> /* dialog styles */ .dialog_content { width: 100%; vertical-align: top; min-width: 300px; padding: 0 0 20px 20px; } .dialog_buttons { display: inline; text-align: right; vertical-align:bottom; white-space: nowrap; } .dialog_buttons_row { text-align: right; } .dialog_left_icon { font-size: 300%; vertical-align: top; } .dialog_left_icon_warn { color: #81878E; } .dialog_left_icon_confirm { color: #6CE474; } .dropdown_menu { padding: 5px; margin-bottom: 10px; border-radius: 3px; } </style> <g:ui_form onsubmit="return invokePromptCallBack('ok');"> <g2:evaluate> var title = "${RP.getWindowProperties().get('title')}"; title = new GlideStringUtil().unEscapeHTML(title); var warning = "${RP.getWindowProperties().get('warning')}"; warning = new GlideStringUtil().unEscapeHTML(warning); </g2:evaluate> <table border="0" width="100%"> <tr> <td nowrap="true" style="vertical-align:top;"> <j2:switch on="$[warning]"> <j2:case value="true"> <i id="image_dialog" class="dialog_left_icon dialog_left_icon_warn icon-alert-triangle"/> </j2:case> <j2:default> <i id="image_dialog" class="dialog_left_icon dialog_left_icon_confirm icon-check-circle"/> </j2:default> </j2:switch> </td> <td> <table border="0" width="100%"> <tr> <td class="dialog_content">$[title]</td> </tr> <tr class="dialog_buttons_row"> <td class="dialog_buttons"> <select name="change_type_confirm" id="change_type_confirm" onChange="updateScratchpad(this.value);" class="dropdown_menu"> <option value="normal_major">Normal Major</option> <option value="normal_minor">Normal Minor</option> <option value="emergency_enhanced">Emergency</option> <!-- <option value="retrospective">Retrospective</option> --> </select> </td> </tr> <tr class="dialog_buttons_row"> <td class="dialog_buttons dropdown_menu"> <g:dialog_buttons_ok_cancel ok="invokePromptCallBack('ok');" ok_type="button" cancel="invokePromptCallBack('cancel')" cancel_type="button"/> </td> </tr> </table> </td> </tr> </table> </g:ui_form> Client script

function invokePromptCallBack(type) { var gdw = GlideDialogWindow.get(); if (type == 'ok') var f = gdw.getPreference('onPromptComplete'); else var f = gdw.getPreference('onPromptCancel'); if (typeof(f) == 'function') { try { f.call(gdw, gdw.getPreference('oldValue')); } catch(e) { } } gdw.destroy(); return false; } var gdw = GlideDialogWindow.get(); var focusButton = gdw.getPreference('defaultButton'); if (focusButton == null) focusButton = 'cancel_button'; gel(focusButton).focus(); hideOptionMatchingCurrentType(); getSelectedOptionFromList(); function hideOptionMatchingCurrentType() { var options_list = document.getElementById('change_type_confirm'); // var selected_value = options_list.options[options_list.selectedIndex].value; for (var i=0; i<options_list.options.length; i++) { if (options_list.options[i].value == g_form.getValue('type')) { options_list.options[i].disabled = true; options_list.options[i].selected = false; options_list.options[i].style.backgroundColor = 'lightgrey'; } } } function getSelectedOptionFromList() { var options_list = document.getElementById('change_type_confirm'); var selected_value = options_list.options[options_list.selectedIndex].value; updateScratchpad(selected_value); } function updateScratchpad(selected_value) { g_scratchpad.new_chg_type = selected_value; }


Script include functions code

ChangeUtils > perhaps take copy of the original script include


CHANGE_REQUEST: 'change_request', PROP_CHANGE_ATTRS: 'com.snc.change_request.copy.attributes', CHANGE_REQUEST_DEFAULT_ATTR_VALUE: 'category,cmdb_ci,priority,risk,impact,type,assignment_group,assigned_to,short_description,description,change_plan,backout_plan,test_plan', ajaxFunction_getChangeQueryParams: function(new_chg_type) { if (new_chg_type == undefined) new_chg_type = this.getParameter('sysparm_new_chg_type'); //read attributes list var attributesList = this._getCsvPropertyValue(this.PROP_CHANGE_ATTRS, this.CHANGE_REQUEST_DEFAULT_ATTR_VALUE); var srcSysId = this.getParameter('sysparm_src_sysid'); var gr = new GlideRecordSecure(this.CHANGE_REQUEST); if (gr.get(srcSysId)) { return this._getRecordValuesAsEncodedQuery(gr, attributesList, new_chg_type); } else { this.log.logErr('Invalid src change_request sysid provided = ' + srcSysId); }
},
//---------------------------------------------------

GlideRecordUtils > perhaps take copy of the original script include
setFieldsInRecordInTable: function(table_name, record_sys_id, field_names, field_values) { if (table_name == undefined) table_name = this.getParameter('sysparm_table_name'); if (record_sys_id == undefined) record_sys_id = this.getParameter('sysparm_record_sys_id'); if (field_names == undefined) field_names = this.getParameter('sysparm_field_names'); if (field_values == undefined) field_values = this.getParameter('sysparm_field_values'); var record = new GlideRecord(table_name); record.get(record_sys_id); if (field_names.indexOf(',') == -1) record.setValue(field_names, field_values); else { var field_names_list = field_names.split(','); var field_values_list = field_values.split(','); for (var i=0; i<field_names_list.length; i++) record.setValue(field_names_list[i], field_values_list[i]); } record.update(); },


Comments

Popular posts from this blog

URL link in addInfoMessage

var ga=new GlideAjax('gld_HR_ajax'); ga.addParam('sysparm_name', 'checkEmployeeNumber_hrProfile'); ga.addParam('sysparm_hrprofilenumber', g_form.getValue('number')); ga.addParam('sysparm_employeenumber', newValue); ga.getXMLAnswer(function(answer) { if (answer!='undefined' && answer!=''){ var navURL="<a style='text-decoration:underline;color:blue' href=hr_profile.do?sysparm_query=number=" + answer + ">" + answer + "</a><img width='3' src='images/s.gif'/>"; var sMsg='The employee number entered already exists on another HR Profile ' + navURL; //alert(sMsg); g_form.showErrorBox('employee_number', 'error - please check'); g_form.addInfoMessage(sMsg); } });

GlideRecord setValue

setValue(String name, Object value) Sets the specified field to the specified value. Normally a script would do a direct assignment, for example,  gr.category = value . However, if in a script the element name is a variable, then  gr.setValue(elementName, value)  can be used. When setting a value, ensure the data type of the field matches the data type of the value you enter. This method cannot be used on journal fields. If the value parameter is null, the record is not updated, and an error is not thrown https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_GlideRecord-setValue_String_Object