Retain existing values using field map script

Evren Yamin
Tera Contributor

Hello,

Is there a way to retain existing values of the field when using a transform map?

My requirement is for example I have this record on group table:

group name = cab approval

type = itil

Then I'm using transform map to update this record. The data I'm loading for example is:

group name = cab approval

type = catalog

What needs to happen after transform is:

group name = cab approval

type = itil, catalog

 

I need to retain the existing value of the type field on the group table after transform. If not empty then update, if empty then inser

Appreciate all the help.

1 ACCEPTED SOLUTION

The code does not implement the part where it is supposed to check whether the current list already contains the item to be added. I would go with a field map script as below:

answer = (function transformEntry (source, target, action, log) {
	var groupTypeUniqueValues = getGroupTypeUniqueValues('' + source.u_sys_user_group_type);

	return addTypeToNewGroup(action, groupTypeUniqueValues) ||
		addTypeToGroupWithNoType(target.type.nil(), groupTypeUniqueValues) ||
		addTypeToGroupWithType(getGroupTypes('' + target.type), groupTypeUniqueValues);

	function addMissingTypes () {
		return function (existingValues, newValue) {
			if (existingValues.indexOf(newValue) < 0)
				existingValues.push(newValue);

			return existingValues;
		};
	}

	function addTypeToGroupWithNoType (typeIsEmpty, value) {
		if (typeIsEmpty)
			return value.join(',');
	}

	function addTypeToGroupWithType (existingValues, newValues) {
		return newValues
			.reduce(addMissingTypes(), existingValues)
			.join(',');
	}

	function addTypeToNewGroup (action, value) {
		if (action == 'insert')
			return value.join(',');
	}

	function getGroupTypes (groupTypeList) {
		return groupTypeList
			.split(semiColonCommaOrNewLineSorrundedByWhiteSpace())
			.filter(retainNotEmpty())
			.filter(retainUnique());
	}

	function getGroupTypeUniqueValues (groupTypeList) {
		return groupTypeList
			.split(semiColonCommaOrNewLineSorrundedByWhiteSpace())
			.filter(retainNotEmpty())
			.map(getUniqueValueFromDisplayValue('sys_user_group_type'))
			.filter(retainNotEmpty())
			.filter(retainUnique());
	}

	function getUniqueValueFromDisplayValue (tableName) {
		return function getUniqueValueFromDisplayValue (displayValue) {
			return '' + GetIDValue(tableName, displayValue);
		};
	}

	function retainNotEmpty () {
		return function retainNotEmpty (item) {
			return item != '';
		};
	}

	function retainUnique () {
		return function retainUnique (item, index, items) {
			return items.indexOf(item) == index;
		};
	}

	function semiColonCommaOrNewLineSorrundedByWhiteSpace () {
		return /\s*[;,\n]\s*/g;
	}

})(source, target, action, log);

answer;

Tested it with a field map setup in a transform map as below:

find_real_file.png

View solution in original post

5 REPLIES 5

Tony Chatfield1
Kilo Patron

Hi, unfortunately your question is not very clear.
If a field is not mapped in your transform field mappings, then it would not be automatically updated and you will retain the existing value, unless you had a before transform script configured to set a value.
If using a before transform script you can define when to populate a field, and or use scripts to map your values. IE

//Populate on Insert
if(action == 'insert') {
target.fieldName = source.fieldName;
}

//Populate on update
if(action == 'update') {
target.fieldName = source.fieldName;
}

//Or a crude check to see if the field of an existing record is empty
if(target.fieldName == '' && action == 'update') {
target.fieldName = source.fieldName;
}

https://docs.servicenow.com/bundle/sandiego-platform-administration/page/script/server-scripting/reference/r_TransformationScriptVariables.html

If this is not the information you are looking for then you can update this thread with clear and specific details so that the forum can better understand your requirement.

Hello Tony, thanks for your reply.

My requirement is for example I have this record on group table:

group name = cab approval

type = itil

Then I'm using transform map to update this record. The data I'm loading for example is:

group name = cab approval

type = catalog

What needs to happen after transform is:

group name = cab approval

type = itil, catalog

 

I need to retain the existing value of the type field on the group table after transform. If not empty then update, if empty then insert

Appreciate all the help. Thank you

 

-O-
Kilo Patron
Kilo Patron

For sure you can do it. Probably implementing a logic along the lines of:

- if the current record is beind creted (as opposed to updated), just return the incoming value
- if the current record is being updated:

- fetch existing values (target.type)
- split those by comma (,)
- filter out empty elements
- eliminate duplicates
- push the sys_id of the new value into the array - if not already there
- return the new array joined by comma (,).

This will also work in a more simple manner, but doing it as above will also clean up the type list. The reduced logic would be:

- fetch existing values
- append the sys_id of the new value - if the string is empty or
- append a comma and the sys_id of the new value - if not in the string already and the string is not empty
- return the new string.

Evren Yamin
Tera Contributor

Hello, I was able to do this but I can't seem to make the condition where if the certain type (itil) is already existing. Then it should not update anymore since my current code, adds the itil again, so the record now has duplicate type.

find_real_file.png

here is my code 

find_real_file.png