Skip to content

Instantly share code, notes, and snippets.

@devtuitt
Forked from jmbauguess/CopyARequest.js
Created February 4, 2020 14:54
Show Gist options
  • Save devtuitt/80e123e68de892f2a2f04a64a57f3373 to your computer and use it in GitHub Desktop.
Save devtuitt/80e123e68de892f2a2f04a64a57f3373 to your computer and use it in GitHub Desktop.
Back end functionality to copy variables from one requested item to another
/**
* @description Allows users to copy a requested item on the form
* @extends {AbstractAjaxProcessor}
* @type {Class}
*/
var CopyARequest = Class.create();
CopyARequest.prototype = Object.extendsObject(AbstractAjaxProcessor, {
/**
* @description ServiceNow's ArrayUtil
* @type {ArrayUtil}
*/
AU: new ArrayUtil(),
/**
* @description Returns a map of variables to be populated by a requested item
* @memberOf CopyARequest
* @param {String} request_item The sys_id of a request item
* @param {String} exclusions A list of variables to exclude
*/
getRelatedVariables: function(request_item, exclusions) {
var mtomReference = new GlideRecord('sc_item_option_mtom'),
exclusionsArray = [],
exclusion;
if (exclusions) {
exclusions = exclusions.split(',');
}
for (exclusion in exclusions) {
exclusionsArray.push(exclusions[exclusion] + '');
}
mtomReference.addQuery('request_item.sys_id', request_item);
mtomReference.query();
while (mtomReference.next()) {
if (!this.AU.contains(exclusionsArray,
mtomReference.sc_item_option.item_option_new.name)) {
this.createVariable(mtomReference);
}
}
},
/**
* @description AJAX wrapper for getting related variables for a requested item
*/
getRelatedVariablesAJAX: function() {
this.getRelatedVariables((this.getParameter('sysparm_request_item')),
this.getParameter('sysparm_exclusions'));
},
/**
* @description Creates a variable object to be accessed on the client
* @memberOf CopyARequest
* @param {GlideRecord} mtomReference The current Variable Ownership
*/
createVariable: function(mtomReference) {
var variable = this.newItem("variable");
variable.setAttribute("name",
mtomReference.sc_item_option.item_option_new.name);
variable.setAttribute("value", mtomReference.sc_item_option.value);
variable.setAttribute("type", mtomReference.sc_item_option.item_option_new.type);
},
/**
* @description A reference qualifier for the copy_from variable
* @param {GlideRecord} current The current requested item
* @return {String} An encoded query for the current catalog item
*/
refQual: function(current) {
return 'cat_item=' + current.cat_item.sys_id;
},
type: 'CopyARequest'
});
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('CopyARequest');
ajax.addParam('sysparm_name', 'getRelatedVariablesAJAX');
ajax.addParam('sysparm_request_item', newValue);
ajax.addParam('sysparm_exclusions', "copy_from");
ajax.getXML(fillOutForm);
function fillOutForm(response) {
var variables = response.responseXML.getElementsByTagName("variable"),
variable;
for (variable in variables) {
if (variables.hasOwnProperty(variable)) {
var name = variables[variable].getAttribute("name"),
value = variables[variable].getAttribute("value"),
type = variables[variable].getAttribute("type"),
leftBucket = gel(name + '_select_0'),
rightBucket = gel(name + '_select_1');
if (type == '21') {
var values = value.split(','),
select = document.getElementById(name + '_select_0'),
options,
option,
moveThese = [];
options = select.getElementsByTagName('option');
for (option in options) {
if (options.hasOwnProperty(option)) {
if (values.indexOf(options[option].value) > -1) {
moveThese.push(option);
}
}
}
moveSelectedOptions(moveThese, leftBucket, rightBucket, '--None--');
sortSelect(leftBucket);
} else {
g_form.setValue(variables[variable].getAttribute("name"),
variables[variable].getAttribute("value"));
}
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<unload unload_date="2015-11-10 19:39:45">
<sys_remote_update_set action="INSERT_OR_UPDATE">
<application display_value="Global">global</application>
<application_name>Global</application_name>
<application_scope>global</application_scope>
<application_version/>
<collisions/>
<commit_date/>
<deleted/>
<description/>
<inserted/>
<name>CopyRequest</name>
<origin_sys_id/>
<release_date/>
<remote_sys_id>6df14c574feb4600380f3879b110c7af</remote_sys_id>
<state>loaded</state>
<summary/>
<sys_created_by>admin</sys_created_by>
<sys_created_on>2015-11-10 19:39:23</sys_created_on>
<sys_id>545240974feb4600380f3879b110c71a</sys_id>
<sys_mod_count>0</sys_mod_count>
<sys_updated_by>admin</sys_updated_by>
<sys_updated_on>2015-11-10 19:39:23</sys_updated_on>
<update_set display_value=""/>
<update_source display_value=""/>
<updated/>
</sys_remote_update_set>
<sys_update_xml action="INSERT_OR_UPDATE">
<action>INSERT_OR_UPDATE</action>
<application display_value="Global">global</application>
<category>customer</category>
<comments/>
<name>item_option_new_set_9e3044574feb4600380f3879b110c71f</name>
<payload><![CDATA[<?xml version="1.0" encoding="UTF-8"?><record_update table="item_option_new_set"><item_option_new_set action="INSERT_OR_UPDATE"><description/><display_title>false</display_title><layout>normal</layout><name>Copy Request</name><order>100</order><sys_class_name>item_option_new_set</sys_class_name><sys_created_by>admin</sys_created_by><sys_created_on>2015-11-10 19:30:19</sys_created_on><sys_customer_update>true</sys_customer_update><sys_id>9e3044574feb4600380f3879b110c71f</sys_id><sys_mod_count>0</sys_mod_count><sys_name>Copy Request</sys_name><sys_package display_value="Global" source="global">global</sys_package><sys_policy/><sys_replace_on_upgrade>false</sys_replace_on_upgrade><sys_scope display_value="Global">global</sys_scope><sys_update_name>item_option_new_set_9e3044574feb4600380f3879b110c71f</sys_update_name><sys_updated_by>admin</sys_updated_by><sys_updated_on>2015-11-10 19:30:19</sys_updated_on><title/></item_option_new_set></record_update>]]></payload>
<remote_update_set display_value="CopyRequest">545240974feb4600380f3879b110c71a</remote_update_set>
<replace_on_upgrade>false</replace_on_upgrade>
<sys_created_by>admin</sys_created_by>
<sys_created_on>2015-11-10 19:39:23</sys_created_on>
<sys_id>185240974feb4600380f3879b110c71a</sys_id>
<sys_mod_count>0</sys_mod_count>
<sys_updated_by>admin</sys_updated_by>
<sys_updated_on>2015-11-10 19:39:23</sys_updated_on>
<table/>
<target_name>Copy Request</target_name>
<type>Variable Set</type>
<update_domain>global</update_domain>
<update_set display_value=""/>
<view/>
</sys_update_xml>
<sys_update_xml action="INSERT_OR_UPDATE">
<action>INSERT_OR_UPDATE</action>
<application display_value="Global">global</application>
<category>customer</category>
<comments/>
<name>sys_script_include_5d59d4044f27c200380f3879b110c726</name>
<payload>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;record_update table="sys_script_include"&gt;&lt;sys_script_include action="INSERT_OR_UPDATE"&gt;&lt;access&gt;package_private&lt;/access&gt;&lt;active&gt;true&lt;/active&gt;&lt;api_name&gt;global.CopyARequest&lt;/api_name&gt;&lt;client_callable&gt;true&lt;/client_callable&gt;&lt;description&gt;Allows users to copy a requested item on the form&lt;/description&gt;&lt;name&gt;CopyARequest&lt;/name&gt;&lt;script&gt;&lt;![CDATA[/**
* @description Allows users to copy a requested item on the form
* @namespace
* @extends {AbstractAjaxProcessor}
* @type {Class}
*/
var CopyARequest = Class.create();
CopyARequest.prototype = Object.extendsObject(AbstractAjaxProcessor, {
/**
* @description ServiceNow's ArrayUtil
* @type {ArrayUtil}
*/
AU: new ArrayUtil(),
/**
* @description Returns a map of variables to be populated by a requested item
* @memberOf CopyARequest
* @param {String} request_item The sys_id of a request item
* @param {String} exclusions A list of variables to exclude
*/
getRelatedVariables: function(request_item, exclusions) {
var mtomReference = new GlideRecord('sc_item_option_mtom'),
exclusionsArray = [],
exclusion;
if (exclusions) {
exclusions = exclusions.split(',');
}
for (exclusion in exclusions) {
exclusionsArray.push(exclusions[exclusion] + '');
}
mtomReference.addQuery('request_item.sys_id', request_item);
mtomReference.query();
while (mtomReference.next()) {
if (!this.AU.contains(exclusionsArray, mtomReference.sc_item_option.item_option_new.name.toString())) {
this.createVariable(mtomReference);
}
}
},
/**
* @description AJAX wrapper for getting related variables for a requested item
*/
getRelatedVariablesAJAX: function() {
this.getRelatedVariables((this.getParameter('sysparm_request_item')), this.getParameter('sysparm_exclusions'));
},
/**
* @description Creates a variable object to be accessed on the client
* @memberOf CopyARequest
* @param {GlideRecord} mtomReference The current Variable Ownership
*/
createVariable: function(mtomReference) {
var variable = this.newItem("variable");
variable.setAttribute("name",
mtomReference.sc_item_option.item_option_new.name);
variable.setAttribute("value", mtomReference.sc_item_option.value);
},
refQual: function(current) {
return 'cat_item=' + current.cat_item.sys_id;
},
type: 'CopyARequest'
});]]&gt;&lt;/script&gt;&lt;sys_class_name&gt;sys_script_include&lt;/sys_class_name&gt;&lt;sys_created_by&gt;admin&lt;/sys_created_by&gt;&lt;sys_created_on&gt;2015-10-31 19:00:03&lt;/sys_created_on&gt;&lt;sys_customer_update&gt;true&lt;/sys_customer_update&gt;&lt;sys_id&gt;5d59d4044f27c200380f3879b110c726&lt;/sys_id&gt;&lt;sys_mod_count&gt;38&lt;/sys_mod_count&gt;&lt;sys_name&gt;CopyARequest&lt;/sys_name&gt;&lt;sys_package display_value="Global" source="global"&gt;global&lt;/sys_package&gt;&lt;sys_policy/&gt;&lt;sys_replace_on_upgrade&gt;false&lt;/sys_replace_on_upgrade&gt;&lt;sys_scope display_value="Global"&gt;global&lt;/sys_scope&gt;&lt;sys_update_name&gt;sys_script_include_5d59d4044f27c200380f3879b110c726&lt;/sys_update_name&gt;&lt;sys_updated_by&gt;admin&lt;/sys_updated_by&gt;&lt;sys_updated_on&gt;2015-11-10 19:31:47&lt;/sys_updated_on&gt;&lt;/sys_script_include&gt;&lt;/record_update&gt;</payload>
<remote_update_set display_value="CopyRequest">545240974feb4600380f3879b110c71a</remote_update_set>
<replace_on_upgrade>false</replace_on_upgrade>
<sys_created_by>admin</sys_created_by>
<sys_created_on>2015-11-10 19:39:23</sys_created_on>
<sys_id>585240974feb4600380f3879b110c71a</sys_id>
<sys_mod_count>0</sys_mod_count>
<sys_updated_by>admin</sys_updated_by>
<sys_updated_on>2015-11-10 19:39:23</sys_updated_on>
<table/>
<target_name>CopyARequest</target_name>
<type>Script Include</type>
<update_domain>global</update_domain>
<update_set display_value=""/>
<view/>
</sys_update_xml>
<sys_update_xml action="INSERT_OR_UPDATE">
<action>INSERT_OR_UPDATE</action>
<application display_value="Global">global</application>
<category>customer</category>
<comments/>
<name>catalog_script_client_deb0c4574feb4600380f3879b110c79f</name>
<payload>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;record_update sys_domain="global" table="catalog_script_client"&gt;&lt;catalog_script_client action="INSERT_OR_UPDATE"&gt;&lt;active&gt;true&lt;/active&gt;&lt;applies_catalog&gt;true&lt;/applies_catalog&gt;&lt;applies_extended&gt;false&lt;/applies_extended&gt;&lt;applies_req_item&gt;false&lt;/applies_req_item&gt;&lt;applies_sc_task&gt;false&lt;/applies_sc_task&gt;&lt;applies_to&gt;set&lt;/applies_to&gt;&lt;cat_item/&gt;&lt;cat_variable&gt;IO:ec4044574feb4600380f3879b110c781&lt;/cat_variable&gt;&lt;condition/&gt;&lt;description/&gt;&lt;field/&gt;&lt;global&gt;true&lt;/global&gt;&lt;messages/&gt;&lt;name&gt;Copy Request&lt;/name&gt;&lt;order/&gt;&lt;script&gt;&lt;![CDATA[function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('CopyARequest');
ajax.addParam('sysparm_name', 'getRelatedVariablesAJAX');
ajax.addParam('sysparm_request_item', newValue);
ajax.addParam('sysparm_exclusions', "copy_from");
ajax.getXML(fillOutForm);
function fillOutForm(response) {
var variables = response.responseXML.getElementsByTagName("variable"),
variable;
for (variable in variables) {
if (variables.hasOwnProperty(variable)) {
g_form.setValue(variables[variable].getAttribute("name"),
variables[variable].getAttribute("value"));
}
}
}
}]]&gt;&lt;/script&gt;&lt;sys_class_name&gt;catalog_script_client&lt;/sys_class_name&gt;&lt;sys_created_by&gt;admin&lt;/sys_created_by&gt;&lt;sys_created_on&gt;2015-11-10 19:33:14&lt;/sys_created_on&gt;&lt;sys_customer_update&gt;true&lt;/sys_customer_update&gt;&lt;sys_domain&gt;global&lt;/sys_domain&gt;&lt;sys_domain_path&gt;/&lt;/sys_domain_path&gt;&lt;sys_id&gt;deb0c4574feb4600380f3879b110c79f&lt;/sys_id&gt;&lt;sys_mod_count&gt;2&lt;/sys_mod_count&gt;&lt;sys_name&gt;Copy Request&lt;/sys_name&gt;&lt;sys_overrides/&gt;&lt;sys_package display_value="Global" source="global"&gt;global&lt;/sys_package&gt;&lt;sys_policy/&gt;&lt;sys_replace_on_upgrade&gt;false&lt;/sys_replace_on_upgrade&gt;&lt;sys_scope display_value="Global"&gt;global&lt;/sys_scope&gt;&lt;sys_update_name&gt;catalog_script_client_deb0c4574feb4600380f3879b110c79f&lt;/sys_update_name&gt;&lt;sys_updated_by&gt;admin&lt;/sys_updated_by&gt;&lt;sys_updated_on&gt;2015-11-10 19:36:32&lt;/sys_updated_on&gt;&lt;table/&gt;&lt;type&gt;onChange&lt;/type&gt;&lt;ui_type&gt;0&lt;/ui_type&gt;&lt;variable_set display_value="Copy Request"&gt;9e3044574feb4600380f3879b110c71f&lt;/variable_set&gt;&lt;view/&gt;&lt;/catalog_script_client&gt;&lt;/record_update&gt;</payload>
<remote_update_set display_value="CopyRequest">545240974feb4600380f3879b110c71a</remote_update_set>
<replace_on_upgrade>false</replace_on_upgrade>
<sys_created_by>admin</sys_created_by>
<sys_created_on>2015-11-10 19:39:23</sys_created_on>
<sys_id>945240974feb4600380f3879b110c71a</sys_id>
<sys_mod_count>0</sys_mod_count>
<sys_updated_by>admin</sys_updated_by>
<sys_updated_on>2015-11-10 19:39:23</sys_updated_on>
<table/>
<target_name>Copy Request</target_name>
<type>Catalog Client Scripts</type>
<update_domain>global</update_domain>
<update_set display_value=""/>
<view/>
</sys_update_xml>
<sys_update_xml action="INSERT_OR_UPDATE">
<action>INSERT_OR_UPDATE</action>
<application display_value="Global">global</application>
<category>customer</category>
<comments/>
<name>item_option_new_ec4044574feb4600380f3879b110c781</name>
<payload><![CDATA[<?xml version="1.0" encoding="UTF-8"?><record_update table="item_option_new"><item_option_new action="INSERT_OR_UPDATE"><active>true</active><attributes/><cat_item/><category/><choice_direction>down</choice_direction><choice_field/><choice_table/><create_roles/><default_html_value/><default_value/><delete_roles/><delivery_plan/><description/><display_title>false</display_title><do_not_select_first>false</do_not_select_first><dynamic_default_value/><dynamic_ref_qual/><field/><global>false</global><help_tag>More information</help_tag><help_text/><include_none>false</include_none><layout>normal</layout><list_table/><lookup_label/><lookup_price/><lookup_table/><lookup_unique>false</lookup_unique><lookup_value/><macro/><mandatory>false</mandatory><map_to_field>false</map_to_field><mask_use_confirmation>false</mask_use_confirmation><mask_use_encryption>false</mask_use_encryption><name>copy_from</name><order/><price_if_checked>0</price_if_checked><pricing_implications>false</pricing_implications><question_text>Copy Variables From</question_text><read_roles/><rec_lookup_price/><rec_price_if_checked>0</rec_price_if_checked><record/><record_producer_table/><reference>sc_req_item</reference><reference_qual>javascript: new CopyARequest().refQual(current);</reference_qual><reference_qual_condition/><scale_max>5</scale_max><scale_min>0</scale_min><show_help>false</show_help><summary_macro/><sys_class_name>item_option_new</sys_class_name><sys_created_by>admin</sys_created_by><sys_created_on>2015-11-10 19:31:21</sys_created_on><sys_customer_update>true</sys_customer_update><sys_id>ec4044574feb4600380f3879b110c781</sys_id><sys_mod_count>0</sys_mod_count><sys_name>Copy Variables From</sys_name><sys_package display_value="Global" source="global">global</sys_package><sys_policy/><sys_replace_on_upgrade>false</sys_replace_on_upgrade><sys_scope display_value="Global">global</sys_scope><sys_update_name>item_option_new_ec4044574feb4600380f3879b110c781</sys_update_name><sys_updated_by>admin</sys_updated_by><sys_updated_on>2015-11-10 19:31:21</sys_updated_on><table/><type>8</type><ui_page/><use_dynamic_default>false</use_dynamic_default><use_reference_qualifier>advanced</use_reference_qualifier><variable_name/><variable_set display_value="Copy Request">9e3044574feb4600380f3879b110c71f</variable_set><visibility>1</visibility><visible_bundle>true</visible_bundle><visible_guide>true</visible_guide><visible_standalone>true</visible_standalone><visible_summary>true</visible_summary><write_roles/></item_option_new><sys_translated_text action="delete_multiple" query="documentkey=ec4044574feb4600380f3879b110c781"/></record_update>]]></payload>
<remote_update_set display_value="CopyRequest">545240974feb4600380f3879b110c71a</remote_update_set>
<replace_on_upgrade>false</replace_on_upgrade>
<sys_created_by>admin</sys_created_by>
<sys_created_on>2015-11-10 19:39:23</sys_created_on>
<sys_id>d45240974feb4600380f3879b110c71a</sys_id>
<sys_mod_count>0</sys_mod_count>
<sys_updated_by>admin</sys_updated_by>
<sys_updated_on>2015-11-10 19:39:23</sys_updated_on>
<table/>
<target_name>Copy Variables From</target_name>
<type>Variable</type>
<update_domain>global</update_domain>
<update_set display_value=""/>
<view/>
</sys_update_xml>
</unload>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment