Name

global.PatternRelatedItems

Description

Used to retrive a pattern related items

Script

var PatternRelatedItems = Class.create();
PatternRelatedItems.prototype = {
  initialize: function(patternIdsList) {
  	this.patternIdsList = patternIdsList; // an Array
  	this.saCiToPatternFilenames = []; // reset
  	this.patternsCis = this.getPatternCis();
  	this.patternRelatedCis = this.getRelatedCis();
  },
  
  getFilename : function (tableName, sysId){
  	return (tableName + "_" + sysId);
  },
  
  getFilenames : function (tableName, sysIds){
  	var arr = [];
  	
  	for (var i = 0; i < sysIds.length; i++)
  		arr.push(this.getFilename(tableName, sysIds[i]));
  	
  	return arr;
  },
  
  getPatternsCis : function(){
  	return this.patternsCis;
  },
  
  getPatternRelatedCis : function(){
  	return this.patternRelatedCis;
  },
  
  getPatternCis : function() {
  	var patternCis = [];
  	var patternGr = new GlideRecord("sa_pattern");
  	patternGr.addQuery('sys_id',this.patternIdsList);
  	patternGr.query();
  	while (patternGr.next()) {
  		patternCis.push(patternGr.getValue('ci_type'));
  		
  	}
  	return patternCis;
  },
  
  getRelatedCis : function() {
  	var pRelatedCis = [];
  	var saCiToPatternTable = "sa_ci_to_pattern";
  	var patternGr = new GlideRecord(saCiToPatternTable);
  	patternGr.addQuery('pattern',this.patternIdsList);
  	patternGr.query();
  	while (patternGr.next()){
  		pRelatedCis.push(patternGr.getValue('ci_type'));
  		this.saCiToPatternFilenames.push(this.getFilename(saCiToPatternTable, patternGr.getUniqueValue()));
  	}

  	return pRelatedCis;
  },
  
  getCiToPatternFiles : function() {
  	return this.saCiToPatternFilenames;
  },
  
  getIdentifiersFilenamesForCis : function(cis) {
  	var patternIdentifiers = [];
  	var patternIdentifierEntries = [];
  	var patternIdentifierEntryRelatedEntries = [];
  	
  	var idGr = new GlideRecord('cmdb_identifier');
  	idGr.addQuery('applies_to', cis);
  	idGr.addActiveQuery();
  	idGr.orderBy('order');
  	idGr.query();
  	
  	while (idGr.next())
  		patternIdentifiers.push(idGr.getUniqueValue());
  	
  	var ieGr = new GlideRecord('cmdb_identifier_entry');
  	ieGr.addQuery('identifier', patternIdentifiers);
  	ieGr.query();
  	while (ieGr.next())
  		patternIdentifierEntries.push(ieGr.getUniqueValue());
  	
  	var ireGr = new GlideRecord('cmdb_related_entry');
  	ireGr.addQuery('identifier', patternIdentifiers);
  	ireGr.query();
  	while (ieGr.next())
  		patternIdentifierEntryRelatedEntries.push(ieGr.getUniqueValue());
  	
  	var patternIdentifiersFiles = this.getFilenames("cmdb_identifier", patternIdentifiers);
  	var patternIdentifierEntriesFiles = this.getFilenames("cmdb_identifier_entry", patternIdentifierEntries);
  	var patternIdentifierEntryRelatedEntriesFiles = this.getFilenames("cmdb_related_entry", patternIdentifierEntryRelatedEntries);
  	
  	// return all arrays as one array of all files related to identifiers
  	return patternIdentifiersFiles.concat(patternIdentifierEntriesFiles,patternIdentifierEntryRelatedEntriesFiles);
  },
  
  getIdentifiersFilenames : function (){
  	var arrMainCisIdentifiers = this.getIdentifiersFilenamesForCis(this.patternsCis);
  	var arrRelatedCisIdentifiers = this.getIdentifiersFilenamesForCis(this.patternRelatedCis);
  	
  	return this.mergeArraysNoDups(arrMainCisIdentifiers, arrRelatedCisIdentifiers);
  },
  
  getHostingRulesFilenamesForCis : function(cis) {
  	var hostingRules = [];
  	var metadataHostingTable = "cmdb_metadata_hosting";
  	var hostingRuleGr = new GlideRecord(metadataHostingTable);
  	hostingRuleGr.addQuery('parent_type', cis).addOrCondition('child_type', cis);
  	hostingRuleGr.query();
  	while (hostingRuleGr.next())
  		if (!this.isArrayIncludesValue(hostingRules, hostingRuleGr.getUniqueValue()))
  			hostingRules.push(hostingRuleGr.getUniqueValue());
  	
  	return this.getFilenames(metadataHostingTable, hostingRules);
  },
  
  getHostingRulesFilenames : function(){
  	var arrHostingRulesForMainCis = this.getHostingRulesFilenamesForCis(this.patternsCis);
  	var arrHostingRulesForRelatedCis = this.getHostingRulesFilenamesForCis(this.patternRelatedCis);
  	
  	return this.mergeArraysNoDups(arrHostingRulesForRelatedCis, arrHostingRulesForMainCis);
  },
  
  getContainmentRulesFilenamesForCis : function(cis) {
  	var arrContainmentRulsFilenames = [];
  	var metadataContainmentTable = "cmdb_metadata_containment";
  	var containmentRuleGr = new GlideRecord(metadataContainmentTable);
  	containmentRuleGr.addQuery('ci_type', cis);
  	containmentRuleGr.query();
  	while (containmentRuleGr.next())
  		arrContainmentRulsFilenames.push(this.getFilename(metadataContainmentTable, containmentRuleGr.getUniqueValue()));

  	return arrContainmentRulsFilenames;
  },
  
  getContainmentRulesFilenames : function() {
  	var arrContainmentRulesForMainCis = this.getContainmentRulesFilenamesForCis(this.patternsCis);
  	var arrContainmentForRelatedCis = this.getContainmentRulesFilenamesForCis(this.patternRelatedCis);
  	
  	return this.mergeArraysNoDups(arrContainmentRulesForMainCis, arrContainmentForRelatedCis);
  },
  
  getReconciliationDefinitionFilenamesForCis : function(cis) {
  	var arrReconciliationDefs = [];
  	var reconciliationDefTablename = "cmdb_reconciliation_definition";
  	var reconciliationDefGr = new GlideRecord(reconciliationDefTablename);
  	reconciliationDefGr.addQuery('applies_to', cis);
  	reconciliationDefGr.query();
  	while (reconciliationDefGr.next())
  		arrReconciliationDefs.push(this.getFilename(reconciliationDefTablename, reconciliationDefGr.getUniqueValue()));
  	
  	return arrReconciliationDefs;
  },
  
  getReconciliationDefinitionFilenames : function() {
  	var arrRecDefsForMainCis = this.getReconciliationDefinitionFilenamesForCis(this.patternsCis);
  	var arrRecDefsForRelatedCis = this.getReconciliationDefinitionFilenamesForCis(this.patternRelatedCis);
  	
  	return this.mergeArraysNoDups(arrRecDefsForMainCis, arrRecDefsForRelatedCis);
  },
  
  getExtensionRecordsFilenames: function(){
  	var patternExtensionsIds = [];
  	var patternExtensionsFilenames = [];
  	var patternsTables = "sa_pattern";
  	var extentionsIds = [];
  	
  	var extensionGr = new GlideRecord('sa_pattern_extension');
  	extensionGr.addQuery('pattern', this.patternIdsList);
  	extensionGr.query();
  	while (extensionGr.next()){
  		patternExtensionsIds.push(extensionGr.getValue('extension'));
  		extentionsIds.push(extensionGr.getUniqueValue());
  	}
  	var extentionsFilenames = this.getFilenames('sa_pattern_extension', extentionsIds);
  	var extensionPatternFilenames = this.getFilenames(patternsTables, patternExtensionsIds);

  	return extentionsFilenames.concat(extensionPatternFilenames);
  },
  
  getSharedLibrariesFilenames : function() {
  	var sharedLibsIds = [];
  	var ptl = new SNC.GlidePatternLibrary();
  	for (var idx in this.patternIdsList) {
  		var patternId = this.patternIdsList[idx];
  		var libs = JSON.parse(ptl.getPatternLibraries(patternId));
  		for (var i in libs) // we cannot concat becasue libs is an Array and sharedLibsIds is a NativeArray
  			if (!this.isArrayIncludesValue(sharedLibsIds, libs[i]))
  				sharedLibsIds.push(libs[i]);
  	}
  	return this.getFilenames("sa_pattern", sharedLibsIds);
  },
  
  getPrePostRecordsFilenames: function() {
  	var prePostTablename = "sa_pattern_prepost_script";
  	var prePostsIds = [];
  	
  	for (var idx in this.patternIdsList) {
  		var patternId = this.patternIdsList[idx];
  		var prePostGr = new GlideRecord(prePostTablename);
  		prePostGr.addQuery("pattern", 'CONTAINS', patternId);
  		prePostGr.query();
  		while (prePostGr.next())
  			if (!this.isArrayIncludesValue(prePostsIds, prePostGr.getUniqueValue()))
  				prePostsIds.push(prePostGr.getUniqueValue());
  	}
  	
  	return this.getFilenames(prePostTablename, prePostsIds);
  },
  
  getTriggerProbesGr : function(){
  	var triggerProbesGr = new GlideRecord('discovery_classifier_probe');
  	triggerProbesGr.addQuery('pattern', this.patternIdsList);
  	triggerProbesGr.query();
  	return triggerProbesGr;
  },
  
  getTriggerProbesFilenames: function(patternId) {
  	var triggerProbesTablename = "discovery_classifier_probe";
  	var triggerProbesIds = [];
  	
  	var triggerProbesGr = this.getTriggerProbesGr();
  	while (triggerProbesGr.next())
  		triggerProbesIds.push(triggerProbesGr.getUniqueValue());
  	
  	return this.getFilenames(triggerProbesTablename, triggerProbesIds);
  },
  
  getDiscoveryClassyForTriggerProbesFilenames: function() {
  	var discoveryClassyTable = 'discovery_classy';
  	var triggerProbesGr = this.getTriggerProbesGr();
  	var discoClassyIds = [];
  	
  	var discoveryClassCriteria = 'discovery_class_criteria';
  	var discoClassCriteria = [];

  	while (triggerProbesGr.next())
  		discoClassyIds.push(triggerProbesGr.getValue("classy"));
  	
  	var discoClassGr = new GlideRecord(discoveryClassCriteria);
  	discoClassGr.addQuery("classy", discoClassyIds);
  	discoClassGr.query();
  	
  	while (discoClassGr.next())
  		discoClassCriteria.push(discoClassGr.getUniqueValue());
  	
  	var classyIds = this.getFilenames(discoveryClassyTable, discoClassyIds);
  	var classCriteriaIds = this.getFilenames(discoveryClassCriteria, discoClassCriteria);

  	return classyIds.concat(classCriteriaIds);
  },
  
  getTrackedFileDefintionsFilenames: function() {
  	var fileDefTablename = 'sa_tracked_file_definition';
  	var fileDefGr = new GlideRecord(fileDefTablename);
  	var fileDefSysIds = [];
  	
  	fileDefGr.addQuery('pattern', this.patternIdsList);
  	fileDefGr.query();  
  	while (fileDefGr.next())
  		fileDefSysIds.push(fileDefGr.getUniqueValue());
  	
  	return this.getFilenames(fileDefTablename, fileDefSysIds);
  },
  
  getCustomOperationsParsingStrategiesFilenames: function() {
  	var pl = new SNC.GlidePatternLibrary();
  	var customOperationsIds = [];
  	var customStIds = [];
  	var customOperationsParamsIds = [];
  	var customOperationTablename = 'sa_custom_operation';
  	var customOperationParamsTablename = 'sa_custom_operation_param';
  	var customStTablename = 'sa_custom_parsing_strategy';
  	
  	for (var idx in this.patternIdsList) {
  		var patternId = this.patternIdsList[idx];
  		var result = JSON.parse(pl.getCustomOperationsAndStrategies(patternId));

  		if (!result) // check
  			continue;

  		var listOfOperations = result['CUSTOM_OPERATIONS'][1];
  		var listOfStrategies = result['CUSTOM_STRATEGIES'][1];
  		if (!(listOfOperations instanceof Array) && listOfOperations !== null) // Make sure we have arrays
  			listOfOperations = [listOfOperations];

  		if (!(listOfStrategies instanceof Array) && listOfStrategies !== null) // Make sure we have arrays
  			listOfStrategies = [listOfStrategies];

  		// First operations
  		for (var op in listOfOperations)
  			if (!this.isArrayIncludesValue(customOperationsIds, listOfOperations[op]))
  				customOperationsIds.push(listOfOperations[op]);

  		var customOperParamsGr = new GlideRecord(customOperationParamsTablename);
  		customOperParamsGr.addQuery('operation', listOfOperations);
  		customOperParamsGr.query();
  		while (customOperParamsGr.next())
  			customOperationsParamsIds.push(customOperParamsGr.getUniqueValue());


  		for (var st in listOfStrategies)
  			if (!this.isArrayIncludesValue(customStIds, listOfStrategies[st]))
  				customStIds.push(listOfStrategies[st]);
  	}
  	
  	var customOperationsFilename = this.getFilenames(customOperationTablename, customOperationsIds);
  	var customOperationsParamsFilename = this.getFilenames(customOperationParamsTablename, customOperationsParamsIds);
  	var customeSt = this.getFilenames(customStTablename, customStIds);
  	
  	return customOperationsFilename.concat(customOperationsParamsFilename,customeSt);
  },
  
  getServerlessInputParametersFilenames: function() {
  	var serverlessParamsTablename = 'discovery_ptrn_lnch_param_def';
  	var serverlessParamsGr = new GlideRecord(serverlessParamsTablename);
  	var serverlessParamsIds = [];

  	serverlessParamsGr.addQuery('pattern', this.patternIdsList);
  	serverlessParamsGr.query();
  	while (serverlessParamsGr.next())
  		serverlessParamsIds.push(serverlessParamsGr.getUniqueValue());
  	
  	return this.getFilenames(serverlessParamsTablename, serverlessParamsIds);
  },
  
  getPreSmTaskDefinitionsFilenames: function() {
  	var preTaskScriptTablename = 'sa_pre_task_script';
  	if (!GlideTableDescriptor.isValid(preTaskScriptTablename))
  		return [];
  	
  	var preTaskSysIds = [];
  	var preTaskGr = new GlideRecord(preTaskScriptTablename);
  	preTaskGr.addQuery('ci_types', this.patternsCis);
  	preTaskGr.query();
  	while (preTaskGr.next())
  		preTaskSysIds.push(preTaskGr.getUniqueValue());

  	return this.getFilenames(preTaskScriptTablename, preTaskSysIds);
  },
  
  safeGetTableFilenamesForPatterns: function(tablename, patternRefField) {
  	var arr = [];
  	
  	if (!GlideTableDescriptor.isValid(tablename))
  		return arr;

  	var gr = new GlideRecord(tablename);
  	gr.addQuery(patternRefField, this.patternIdsList);
  	gr.query();
  	while (gr.next())
  		arr.push(gr.getUniqueValue());

  	return this.getFilenames(tablename, arr);
  },
  
  getSnPetternTriggerRuleFilenames : function() {
  	return this.safeGetTableFilenamesForPatterns("sn_pattern_trigger_rule", "pattern_id");
  },
  
  getDiscoPatternLunchParamDefExtFilenames : function() {
  	return this.safeGetTableFilenamesForPatterns("discovery_ptrn_lnch_param_def_ext", "pattern");
  },
  
  getSnPatternOutputsFilenames : function() {
  	return this.safeGetTableFilenamesForPatterns("sn_pattern_outputs", "pattern_id");
  },
  
  getDeleteStrategyScripts : function () {
  	var stScriptsIds = [];
  	var grCiToPattern = new GlideRecord("sa_ci_to_pattern");
  	grCiToPattern.addQuery("ci_type", this.patternRelatedCis);
  	grCiToPattern.query(this.patternRelatedCis);
  	
  	while (grCiToPattern.next()){
  		var delStrategyScriptId = grCiToPattern.getValue("strategy_script");
  		if (delStrategyScriptId && delStrategyScriptId != "null" && !this.isArrayIncludesValue(stScriptsIds, delStrategyScriptId))
  			stScriptsIds.push(delStrategyScriptId);
  	}
  	
  	return this.getFilenames("sn_discovery_delete_strategy", stScriptsIds);
  },
  
  getUploadedFilesFilenames : function() {
  	var uploadedFilesNames = [];
  	var uploadedFilesSysIds = [];
  	var arrUploadedFilenames = [];
  	var arrSysAttachmentsSysIds = [];
  	var arrSysAttachments = [];
  	var ptl = new SNC.GlidePatternLibrary();
  	for (var idx in this.patternIdsList) {
  		var patternId = this.patternIdsList[idx];
  		var uploadedFiles = JSON.parse(ptl.getUploadfiles(patternId));
  		for (var i in uploadedFiles) // we cannot concat becasue libs is an Array and sharedLibsIds is a NativeArray
  			if (!this.isArrayIncludesValue(uploadedFilesNames, uploadedFiles[i]))
  				uploadedFilesNames.push(uploadedFiles[i]);
  	}
  	
  	var uploadedFilesGr = new GlideRecord("sa_uploaded_file");
  	uploadedFilesGr.addQuery("name", uploadedFilesNames);
  	uploadedFilesGr.query();
  	while (uploadedFilesGr.next())
  		uploadedFilesSysIds.push(uploadedFilesGr.getValue("sys_id"));
  	
  	arrUploadedFilenames = this.getFilenames("sa_uploaded_file", uploadedFilesSysIds);
  	
  	var sysAttachmentGr = new GlideRecord("sys_attachment");
  	sysAttachmentGr.addQuery("table_sys_id", uploadedFilesSysIds);
  	sysAttachmentGr.query();
  	while (sysAttachmentGr.next())
  		arrSysAttachmentsSysIds.push(sysAttachmentGr.getValue("sys_id"));
  	
  	arrSysAttachments = this.getFilenames("sys_attachment", arrSysAttachmentsSysIds);
  	
  	var arrSysAttachmendDocSysIds = [];
  	var arrSysAttachmentDocsFilenames = [];
  	var sysAttachmentDocGr = new GlideRecord("sys_attachment_doc");
  	sysAttachmentDocGr.addQuery("sys_attachment", arrSysAttachmentsSysIds);
  	sysAttachmentDocGr.query();
  	while (sysAttachmentDocGr.next())
  		arrSysAttachmendDocSysIds.push(sysAttachmentDocGr.getValue("sys_id"));
  	
  	arrSysAttachmentDocsFilenames = this.getFilenames("sys_attachment_doc", arrSysAttachmendDocSysIds);
  	
  	return arrSysAttachments.concat(arrUploadedFilenames, arrSysAttachmentDocsFilenames);
  },
  
  isArrayIncludesValue : function(arr, val) {
  	for (var i=0; i<arr.length; i++){
  		if (val === arr[i])
  			return true;
  	}
  	
  	return false;
  },
  
  mergeArraysNoDups: function(arr1, arr2){
  	var arr2uniqueValues = [];
  	for (var i=0; i<arr2.length; i++){
  		if (!this.isArrayIncludesValue(arr1, arr2[i]))
  			arr2uniqueValues.push(arr2[i]);
  	}
  	
  	return arr1.concat(arr2uniqueValues);
  },
  
  type: 'PatternRelatedItems'
};

Sys ID

57a2b37853330010234dddeeff7b12b6

Offical Documentation

Official Docs: