Copy Change - Cancelled Change Tasks

Andrew Bettcher
Kilo Sage

Hi,

 

I want to amend the copy change process so that it excludes cancelled change tasks. OOTB, it excludes changes that are of type = review. I just want it to also exclude change tasks where state = cancelled. 

The script include where this might be achieved (changeUtils), isn't straight forward because it includes change tasks in with general related lists but it does look at which attributes are copied.

Any hints would be appreciated.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Andrew Bettcher 

that's correct

You will have to override this function "copyChangeRelatedLists" and update it in ChangeUtils script include

this function "_makeRelatedTableCopy" performs the copy and you will have to check the table name and then update the query accordingly

Please check if this function calls any other function, that also you will have to override in ChangeUtils

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Andrew Bettcher 

that's correct

You will have to override this function "copyChangeRelatedLists" and update it in ChangeUtils script include

this function "_makeRelatedTableCopy" performs the copy and you will have to check the table name and then update the query accordingly

Please check if this function calls any other function, that also you will have to override in ChangeUtils

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you for your response Ankur.

This is the function that does the work, as you've said:

	makeRelatedTableCopy: function( /*String*/ srcParentSysId,
									/*String*/ targetParentSysId,
									/*String*/ table,
									/*String*/ key, /*If provided, used to prevent duplicate rows*/
									/*String*/ parentAttr,
									/*Array*/ copyAttrs,
									/*Boolean*/ copyAttachments,
									/*CSV String*/ copyChildRelatedLists) {
		if (!copyChildRelatedLists)
			copyChildRelatedLists = [];
		else
			copyChildRelatedLists = this.getCsvValue(copyChildRelatedLists);

		var ans = [];
		copyAttrs = this.arrayUtil.diff(copyAttrs, this.ALWAYS_IGNORE_ATTRS, [
			parentAttr
		]);
		var srcGr = new GlideRecordSecure(table);
		if (srcGr.isValid()) {
			var existingRecords = [];
			srcGr.addQuery(parentAttr, srcParentSysId);
			//Check if table is task_ci and if so exclude dynamic added CI as these will be processed when the dynamic CI is inserted
			if (table === this.TASK_CI)
				srcGr.addNullQuery('added_from_dynamic_ci');
			srcGr.query();
			if (key) {
				existingRecords = this._getTargetRelatedRecordKeys(table, key,
					parentAttr, targetParentSysId);
			}
			while (srcGr.next()) {
				if (key
					&& this.arrayUtil.contains(existingRecords, srcGr.getValue(key)))
					continue;
				if ((table === this.CHANGE_TASK) && (srcGr.getValue(this.TASK_CREATED_FROM) === this.TASK_CREATED_FROM_WORKFLOW || srcGr.getValue(this.TASK_CREATED_FROM) === this.TASK_CREATED_FROM_FLOW))
					continue;
				var newSysId = this._makeRelatedRecordCopy(srcGr, copyAttrs,
					parentAttr, targetParentSysId);
				if (newSysId) {
					ans.push(newSysId);

					if (copyAttachments)
						this._copyAttachments(srcGr, newSysId);

					for (var i = 0; i < copyChildRelatedLists.length; ++i) {
						var relatedTable = copyChildRelatedLists[i];
						var newAns = this._makeRelatedTableCopy(srcGr.getUniqueValue(),
																newSysId, relatedTable);
						if (!this._isSet(newAns)) {
							this.log.logWarning('makeRelatedTableCopy: Could not copy related\'s related table ' +
												relatedTable);
						}
					}
				} else {
					this.log.logWarning('makeRelatedTableCopy: Could not copy related table ' + table);
				}
			}
			return ans;
		} else {
			this.log.logWarning('makeRelatedTableCopy: Invalid table ' + table);
		}
	},

	_makeRelatedTableCopy: function(srcParentSysId, targetParentSysId, table) {
		var map = this.RELATED_TABLES_MAP[table];
		if (!map) {
			this.log.logWarning('_makeRelatedTableCopy: Unsupported related table ' + table);
			return;
		}
		var key = map.key;
		var parentAttr = map.parentAttr;
		var attachmentKey = map.copyAttachmentsKey;
		var copyAttachments = false;
		if (attachmentKey)
			copyAttachments = this._getCsvPropertyValue(attachmentKey, 'true') == 'true';
		var copyAttrs = this._getCsvPropertyValue(map.property,
			map.defaultValue);
		var copyChildRelatedLists = map['copyRelated'];
		return this.makeRelatedTableCopy(srcParentSysId, targetParentSysId, table,
										key, parentAttr, copyAttrs,
										copyAttachments, copyChildRelatedLists);
	},


Can you confirm that I just need to add additional queries to the GlideRecordSecure query in order to filter out what I don't want?

I'm being very careful as I am working on a customer instance.

Just in case anyone else is looking to do the same thing, here is the process I followed (and it seems to work):

 

  1. Copy the entire function from ChangeUtilsSNC and paste it into ChangeUtils
  2. Find the secure record query (srcGr)
  3. Add a new "If" statement that says:
if (table == this.CHANGE_TASK && srcGr.state == 4) {  //4 = cancelled
return;
}
​
  • Comment your code
  • Rejoice

@Ankur Bawiskar gets the props for pointing me in the right direction. Thank you Sir.

 

@Andrew Bettcher 

Glad to help.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader