Name

global.CSMTableMapUtilSNC

Description

Customers should not change this class directly This class is never called directly. API Usage - To find if mapping exists. Can be used to show or hide action button. //Initialise the map object with Source gliderecord object. var map = new CSMTableMapUtil (); //Find all mappings (use limit to limit the number of return results) //returns boolean if(map.findMap( limit )){ // show or hide button } //Find by CSM Table map sys_id //returns boolean if(map.findMapByID()){ // show or hide button } //Find by CSM Table map api_name //returns boolean if(map.findMapByName()){ // show or hide button } //Find by target table name (In case of multiple maps, highest order is given preference. Also conditions will be evaluated to find the relevant map) //use limit to limit the number of return results //returns boolean if(map.findMapByTarget( , limit )){ // show or hide button } - To retrieve target record URL directly. Can be used in Platform UI Action to redirect to the target form with prefilled values as per mapping. //returns target url array for all matched mappings with target fieldName and values. var map = new CSMTableMapUtil (); map.findMapByID(); //or map.findMapByTarget(); var targetURL = map.getTargetURL(); if(targetURL) action.setRedirectURL(targetURL 0 ); - To retrieve the mappings as encoded query. Can be used in Service portal widget to initialise the Form widget. //returns encoded query array for all matched mappings with target fieldName and values. var map = new CSMTableMapUtil (); map.findMapByID(); //or map.findMapByTarget(); var queryStr = map.getTargetQuery(); if(queryStr){ data.template = $sp.getWidget( widget-form , { table kb_knowledge , sys_id -1 , query queryStr 0 }); } - To retrieve the mappings as JSON object. Can be used in the code to generate custom actions. //returns JSON array for all matched mappings with target fieldName and values. var map = new CSMTableMapUtil (); map.findMapByID(); //or map.findMapByTarget(); var obj = map.getTargetJSON(); if(obj){ var data = {}; data = obj; //custom code } - To add meta data like mapping name, order, target table in output JSON use //returns JSON array for all matched mappings with target fieldName and values. var map = new CSMTableMapUtil (); map.findMapByID(); //or map.findMapByTarget(); map.addMetaData(); var obj = map.getTargetJSON(); if(obj){ var data = {}; data = obj; //custom code } - To retrieve the mappings as GlideRecord object. Can be used in the code to generate custom actions. //returns single GlideRecord object if there is single mapping, and returns GlideRecord array for multiple mappings with target fieldName and values. var map = new CSMTableMapUtil (); map.findMapByID(); //or map.findMapByTarget(); var obj = map.getTargetGlideRecord(); if(obj){ var sys_id = obj.insert(); //custom code } Sample Output Without MetaData - For getTargetQuery kb_knowledge_base=85071a1347c12200e0ef563dbb9a71c1^text=I%20can%20access%20my%20personal%20folder%20but%20can t%20access%20my%20team s%20folder%20on%20our%20file%20share.^short_description=Sales , short_description=Unable%20to%20access%20team%20file%20share^text=I%20can%20access%20my%20personal%20folder%20but%20can t%20access%20my%20team s%20folder%20on%20our%20file%20share. With MetaData { data kb_knowledge_base=85071a1347c12200e0ef563dbb9a71c1^text=I%20can%20access%20my%20personal%20folder%20but%20can t%20access%20my%20team s%20folder%20on%20our%20file%20share.^short_description=Sales , meta { name Incident , order 100, target kb_knowledge }},{ data short_description=Unable%20to%20access%20team%20file%20share^text=I%20can%20access%20my%20personal%20folder%20but%20can t%20access%20my%20team s%20folder%20on%20our%20file%20share. , meta { name Inc2 , order 100, target kb_knowledge }} (sys.scripts extended logging) - For getTargetURL without MetaData /kb_knowledge.do?sys_id=-1&sysparm_query=kb_knowledge_base=85071a1347c12200e0ef563dbb9a71c1^text=I%20can%20access%20my%20personal%20folder%20but%20can t%20access%20my%20team s%20folder%20on%20our%20file%20share.^short_description=Sales , /kb_knowledge.do?sys_id=-1&sysparm_query=short_description=Unable%20to%20access%20team%20file%20share^text=I%20can%20access%20my%20personal%20folder%20but%20can t%20access%20my%20team s%20folder%20on%20our%20file%20share. (sys.scripts extended logging) With MetaData { data /kb_knowledge.do?sys_id=-1&sysparm_query=kb_knowledge_base=85071a1347c12200e0ef563dbb9a71c1^text=I%20can%20access%20my%20personal%20folder%20but%20can t%20access%20my%20team s%20folder%20on%20our%20file%20share.^short_description=Sales , meta { name Incident , order 100, target kb_knowledge }},{ data /kb_knowledge.do?sys_id=-1&sysparm_query=short_description=Unable%20to%20access%20team%20file%20share^text=I%20can%20access%20my%20personal%20folder%20but%20can t%20access%20my%20team s%20folder%20on%20our%20file%20share. , meta { name Inc2 , order 100, target kb_knowledge }} - For getTargetJSON Without MetaData { kb_knowledge_base 85071a1347c12200e0ef563dbb9a71c1 , short_description Sales , text I can access my personal folder but can t access my team s folder on our file share. }, { short_description Unable to access team file share , text I can access my personal folder but can t access my team s folder on our file share. } With MetaData { data { kb_knowledge_base 85071a1347c12200e0ef563dbb9a71c1 , short_description Sales , text I can access my personal folder but can t access my team s folder on our file share. }, meta { name Incident , order 100, target kb_knowledge }}, { data { short_description Unable to access team file share , text I can access my personal folder but can t access my team s folder on our file share. }, meta { name Inc2 , order 100, target kb_knowledge }}

Script

var CSMTableMapUtilSNC = Class.create();
CSMTableMapUtilSNC.prototype = {
  /*
  * Initialize the class with source gliderecord object
  */
  map_table : "csm_table_map",
  field_map_table : "csm_field_map",

  initialize: function(source) {
  	this.source = source;
  	this.map = [];
  	this.meta = false;
  },

  /*
  * Function to find the relevant mapping by providing the CSM table map definition sys_id
  * @params: CSM Table Map definition sys_id
  * @return: boolean
  *
  */
  findMapByID:function(mapID){
  	if(mapID){
  		var map = new GlideRecord(this.map_table);
  		map.addQuery("sys_id",mapID);
  		map.addActiveQuery();
  		map.query();

  		return this._getMap(map);
  	}
  	return false;
  },

  /*
  * Function to find the relevant mapping by providing the CSM table map definition api name
  * @params: CSM Table Map definition api_name
  * @return: boolean
  *
  */
  findMapByName:function(name){
  	if(name){
  		var map = new GlideRecord(this.map_table);
  		map.addQuery("api_name",name);
  		map.addActiveQuery();
  		map.query();

  		return this._getMap(map);
  	}
  	return false;
  },

  /*
  * Function to find the relevant mapping from the CSM table map definitions by target table name
  * @params: Target table name
  * @return: boolean
  */
  findMapByTarget:function(target,limit){
  	return this.findMapByTargetAndSourceTable(target,limit,this.source.getTableName());
  },
  
  /*
  * Function to find the relevant mapping from the CSM table map definitions by target table name and source table name
  * @params: Target table name
  * @return: boolean
  */
  findMapByTargetAndSourceTable:function(target,limit,sourceTable){
  	var map = new GlideRecord(this.map_table);
  	map.addActiveQuery();
  	map.addQuery("source_table",sourceTable);
  	if(target)
  		map.addQuery("target_table",target);
  	map.orderByDesc("order");
  	map.query();
  	return this._getMap(map,limit);

  },

  /*
  * Function to find the relevant mapping from the CSM table map definitions by target table name and source table hierarchy
  * @params: Target table name
  * @return: boolean
  */
  findMapBySourceHierarchy:function(target,limit){
  	var tableApi = new TableUtils(this.source.getTableName());
  	var hierarchyTableList = tableApi.getHierarchy();
  	var filteredTableList = [];
  	var map = new GlideRecord(this.map_table);
  	map.addActiveQuery();
  	map.addQuery("source_table",hierarchyTableList);
  	if(target)
  		map.addQuery("target_table",target);
  	map.query();
  	while(map.next()){
  		filteredTableList.push(map.getValue('source_table'));
  	}
  	var i=0;
  	var result=false;
  	while(hierarchyTableList.size()>i){
  		if(filteredTableList.indexOf(hierarchyTableList.get(i)+'')>-1){
  			result = this.findMapByTargetAndSourceTable(target,limit,hierarchyTableList.get(i));
  			if(result)
  				break;
  		}
  		i++;
  	}
  	return result;
  },

  /*
  * Function to find all the mappings related to source table
  * @return: boolean
  *
  */
  findMap:function(limit){
  	return this.findMapByTarget(false,limit);
  },

  /*
  * Function to add meta data along with the output
  */
  addMetaData:function(){
  	this.meta = true;
  },

  /*
  * Private function to get mapping
  * @params: CSM Table Map definition gliderecord object
  * @return: boolean
  */
  _getMap:function(map,limit){
  	var result = false;
  	var count = 0;
  	while(map.next()){
  		if(this._evaluateMapConditions(map)){
  			result = true;
  			count++;
  			if(limit && limit == count)
  				break;
  		}
  	}

  	return result;
  },

  /*
  * Private function to evaluate mapping conditions
  * @params: CSM Table Map definition gliderecord object
  * @return: boolean
  */
  _evaluateMapConditions:function(map){
  	var result = false;
  	var noCondition = false;
  	if(map.use_advanced_condition){
  		if(!JSUtil.nil(map.advanced_condition)){
  			try{
  				var evaluator = new GlideScopedEvaluator();
  				evaluator.putVariable("source", this.source);
  				evaluator.putVariable("answer", null);
  				var scriptResult = evaluator.evaluateScript(map,"advanced_condition",null);
  				var answer = evaluator.getVariable("answer");

  				if ((scriptResult && scriptResult == true) || (answer && answer == true)){
  					this.map.push(map.sys_id+'');
  					result = true;
  				}
  			}catch(e){
  				gs.error("ERROR while evaluating advanced condition script for : "+ map.sys_id +" : "+e,"EntityTransformUtil");
  			}
  		}else{
  			noCondition = true;
  		}
  	}else if(map.conditions){
  		var rec = new GlideRecord(this.source.getTableName());
  		if(!gs.nil(this.source.sys_id))
  			rec.addQuery('sys_id', this.source.sys_id);
  		rec.addEncodedQuery(map.conditions);
  		rec.query();
  		if(rec.hasNext()){
  			this.map.push(map.sys_id+'');
  			result = true;
  		}
  	}else{
  		noCondition = true;
  	}

  	if(noCondition){
  		this.map.push(map.sys_id+'');
  		result = true;
  	}

  	return result;
  },

  /*
  * Function to create a direct target url for new record creation with all mappings as parameters
  * @return: target url
  */
  getTargetURL : function(){
  	var output = [];
  	if(this.source && this.map && this.map.length){
  		var map = new GlideRecord(this.map_table);
  		map.addQuery('sys_id','IN',this.map.join());
  		map.query();
  		while(map.next()){
  			var result = this._processMap(map);
  			if(result){
  				var targetURL = "/"+map.target_table+".do?sys_id=-1";
  				var params = "";
  				var notFirst = false;
  				for (var item in result) {
  					if(!result.hasOwnProperty(item)) continue;
  					if(notFirst) params = params + '^';

  					params =  params + item + "=" + encodeURIComponent(result[item].replaceAll('^','^^'));
  					notFirst = true;
  				}

  				if(params)
  					targetURL =  targetURL + "&sysparm_query=" + params;

  				if(this.meta){
  					var obj = {};
  					obj.data = targetURL;

  					obj.meta = {};
  					obj.meta.name = map.mapping_name+'';
  					obj.meta.target = map.target_table+'';
  					obj.meta.order = map.order+'' ? parseInt(map.order+'') : 0;

  					output.push(obj);
  				}else{
  					output.push(targetURL);
  				}
  			}
  		}
  	}
  	return output;
  },

  /*
   * Function to create an encoded query with all mappings which can used in target URL creation
   * @return: mapping as encoded query
   */
  getTargetQuery : function(){
  	var output = [];
  	if(this.source && this.map && this.map.length){
  		var map = new GlideRecord(this.map_table);
  		map.addQuery('sys_id','IN',this.map.join());
  		map.query();
  		while(map.next()){
  			var result = this._processMap(map);
  			var targetQuery = "";
  			if(result){
  				for (var item in result) {
  					if(!result.hasOwnProperty(item)) continue;
  					if(targetQuery) targetQuery = targetQuery + '^';
  					targetQuery =  targetQuery + item + "=" + result[item].replaceAll('^','^^');
  				}

  				if(targetQuery){
  					if(this.meta){
  						var obj = {};
  						obj.data = targetQuery;

  						obj.meta = {};
  						obj.meta.name = map.mapping_name+'';
  						obj.meta.target = map.target_table+'';
  						obj.meta.order = map.order+'' ? parseInt(map.order+'') : 0;

  						output.push(obj);
  					}else{
  						output.push(targetQuery);
  					}
  				}
  			}
  		}
  	}
  	return output;
  },

  /*
   * Function to a generate JSON data with all associated mappings.
   * @return: target mappings as JSON
   */
  getTargetJSON : function(){
  	var output = [];
  	if(this.source && this.map && this.map.length){
  		var map = new GlideRecord(this.map_table);
  		map.addQuery('sys_id','IN',this.map.join());
  		map.query();
  		while(map.next()){
  			var result = this._processMap(map);
  			if(result){

  				if(this.meta){
  					var obj = {};
  					obj.data = result;

  					obj.meta = {};
  					obj.meta.name = map.mapping_name+'';
  					obj.meta.target = map.target_table+'';
  					obj.meta.order = map.order+'' ? parseInt(map.order+'') : 0;

  					output.push(obj);
  				}else{
  					output.push(result);
  				}
  			}
  		}
  	}

  	return output;
  },

  /*
   * Function to a generate GlideRecord data.
   * @return: target mappings as GlideRecord
   */
  getTargetGlideRecord : function(){
  	var output = [];
  	if(this.source && this.map && this.map.length){
  		var map = new GlideRecord(this.map_table);
  		map.addQuery('sys_id','IN',this.map.join());
  		map.query();
  		if(map.getRowCount()==1 && map.next()){
  			var result = this._processMap(map);
  			if(result){
  				var target = new GlideRecordSecure(map.target_table);
  				target.initialize();
  				for(var item in result)
  				{
  					target.setValue(item, result[item]);
  				}
  				return target;
  			}
  		}
  		while(map.next()){
  			var resultGr = this._processMap(map);
  			if(resultGr){
  				var targetGr = new GlideRecordSecure(map.target_table);
  				targetGr.initialize();
  				for(var itemGr in resultGr)
  				{
  					targetGr.setValue(itemGr, resultGr[itemGr]);
  				}
  				output.push(targetGr);
  			}
  		}
  	}

  	return output;
  },
  
  /*
   * Function to process associated mappings
   * @params: CSM Table Map definition gliderecord object
   * @return: boolean
   */
  _processMap:function(map){
  	var result = {};

  	if(map && this.source){
  		result = this._evaluateFieldMappings(map,result);
  		if(map.advanced_field_mapping)
  		result = this._evaluateAdvancedMappingScript(map,result);
  	}

  	return result;
  },

  /*
   * Function to evaluate advanced field transform script in CSM Field map.
   * @params: CSM Table Map definition gliderecord object, CSM Table Map definition sys_id
   * @return: boolean
   */
  _evaluateFieldMappings : function(map,result){

  	var target = new GlideRecord(map.target_table);
  	target.initialize();

  	var fieldMap = new GlideRecord(this.field_map_table);
  	fieldMap.addQuery("table_map",map.sys_id);
  	fieldMap.addActiveQuery();
  	fieldMap.orderBy("order");
  	fieldMap.query();
  	while(fieldMap.next()){
  		if(target.isValidField(fieldMap.target_field)){
  			try{
  				if(fieldMap.advanced){
  					if(!JSUtil.nil(fieldMap.transform_script)){
  						var evaluator = new GlideScopedEvaluator();
  						evaluator.putVariable("source", this.source);
  						evaluator.putVariable("answer", "");
  						var scriptResult = evaluator.evaluateScript(fieldMap,"transform_script",null);
  						var answer = evaluator.getVariable("answer");

  						if (!JSUtil.nil(answer) || !JSUtil.nil(scriptResult)){
  							result[fieldMap.target_field+''] = answer+'' || scriptResult+'';
  						}else{
  							result[fieldMap.target_field+''] = "";
  						}
  					}
  				}else if(fieldMap.source_field){
  					//Not updating the fieldMap, only setting the value of the transform script to use it with GlideScopedEvaluator
  					fieldMap.setValue("transform_script","sourceValue = source.getElement(source_field) + '';");
  					var evaluator  = new GlideScopedEvaluator();
  					evaluator.putVariable("source",this.source);
  					evaluator.putVariable("source_field",fieldMap.source_field);
  					evaluator.putVariable("sourceValue",null);
  					evaluator.evaluateScript(fieldMap,"transform_script",null);
  					var sourceValue = evaluator.getVariable("sourceValue");
  					var sourceFieldType = "";
  					var targetFieldType = "";
  					if(sourceValue){
  						sourceFieldType = this.source.getElement(fieldMap.source_field).getED().getInternalType();
  						targetFieldType = target.getElement(fieldMap.target_field).getED().getInternalType();
  					}

  					if(sourceFieldType && sourceFieldType == "string" && targetFieldType && (targetFieldType == "html" || targetFieldType == "translated_html"))
  						result[fieldMap.target_field+''] = sourceValue.toString().replaceAll("\r\n","<br\>")+'';
  					else
  						result[fieldMap.target_field+''] = sourceValue+'';
  				}
  			}catch(e){
  				gs.error("ERROR while evaluating advanced field script : "+ fieldMap.sys_id +" : for map : "+ map.sys_id +" : "+e,"EntityTransformUtil");
  			}
  		}
  	}
  	return result;
  },

  /*
   * Function to evaluate advanced mapping script in CSM Table map definition.
   * @params: CSM Table Map definition gliderecord object, CSM Table Map definition target object name,
   * @return: boolean
   */
  _evaluateAdvancedMappingScript : function(map,result){
  	try{
  		if(!JSUtil.nil(map.mapping_script)){
  			var evaluator = new GlideScopedEvaluator();
  			evaluator.putVariable("source", this.source);
  			evaluator.putVariable("target", result);
  			evaluator.evaluateScript(map,"mapping_script",null);
  			var scriptResult = evaluator.getVariable("target");

  			if (!JSUtil.nil(scriptResult)){
  				result = scriptResult;
  			}
  		}
  	}catch(e){
  		gs.error("ERROR while evaluating advanced mapping script for : "+ map.sys_id +" : "+e,"EntityTransformUtil");
  	}
  	return result;
  },

  type: 'CSMTableMapUtilSNC'
};

Sys ID

cc27e13b9f333200d7cabde8132e702d

Offical Documentation

Official Docs: