Adding values to a list field by a Transform Script

D_ Nistal
Tera Contributor

Hello SN Experts,

I'm trying to make a Transform Map using source script for one field.

The target field is a 'List field' type and what I what to do is fill this field with data from the source.

Down below, is the script that i made.

What I'm trying to do is create a array variable (osg) where all data from the 'list field' source type are stored, then within a 'forEach' loop I go through every element and for each I create a GlideRecord to the "sys_user_group" table and add the following query(because the data from the source is related to the "sys_user_group" table). The last step is set the output variable 'answer' to the ''sys_id".

the problem is when I run the transform map, it does nothing for the target field.

answer = (function transformEntry(source) {

	// Add your code here
	var osg_array = source.u_other_support_group.split(',');
	var cont = 1;
	
	
	osg_array.forEach(function(element){
		var gr = new GlideRecord('sys_user_group');
		gr.addQuery('name',String(element).trim());
		gr.setWorkflow(false);
		gr.query();
		
		if(gr.next()){
			if(cont < osg_array.length)
				answer = gr.sys_id + ',';
			else
				answer = gr.sys_id;
			return answer;		
		}
		cont++;
	});
	//return answer; // return the value to be put into the target field
	
})(source);
1 ACCEPTED SOLUTION

Troy Riblett
Giga Guru

I think your "return answer" line is being called as a part of the function instead of for the script as a whole. I also did a bit of other cleanup. Try the below and let me know if it works or not

answer = (function transformEntry(source) {

	// Add your code here
	var osg_array = source.u_other_support_group.split(',');
	var ans = [];
	
	osg_array.forEach(function(element){
		var gr = new GlideRecord('sys_user_group');
		gr.addQuery('name',String(element).trim());
		gr.query();
		
		if(gr.next()){
		    ans.push(gr.getValue("sys_id"));
		}
	});
	return ans.join(","); // return the value to be put into the target field
	
})(source);

View solution in original post

2 REPLIES 2

Troy Riblett
Giga Guru

I think your "return answer" line is being called as a part of the function instead of for the script as a whole. I also did a bit of other cleanup. Try the below and let me know if it works or not

answer = (function transformEntry(source) {

	// Add your code here
	var osg_array = source.u_other_support_group.split(',');
	var ans = [];
	
	osg_array.forEach(function(element){
		var gr = new GlideRecord('sys_user_group');
		gr.addQuery('name',String(element).trim());
		gr.query();
		
		if(gr.next()){
		    ans.push(gr.getValue("sys_id"));
		}
	});
	return ans.join(","); // return the value to be put into the target field
	
})(source);

D_ Nistal
Tera Contributor

Thanks you a lot Troy, your solution works very good!!