Help Please on Transform map script for list collector urgent,

JPSS
Tera Contributor

With the below code only one value is updated in the list collector field.. Could you please help.. its urgent

 

 

 

(function transformRow(source, target, map, log, isUpdate) {
     var name = source.u_watchlist.toString();
       var split_names = name.split (",");//Breaking the input in a array
     var num_names = split_names.length;
      var sys_id;
      for(i=0;i<num_names;i++)


              {


                      var gr = new GlideRecord('sys_user');


                      gr.get('email',split_names[0]);
                      gr.query();
                      if(gr.next())
                             {

                                    target.u_watch_list= target.u_watch_list+","+gr.getValue('sys_id');
                        
                                                     


                              }
                   


                   


              }
})(source, target, map, log, action==="update");
1 ACCEPTED SOLUTION

James Chun
Kilo Patron

Hey @JPSS,

 

Try the following code:

(function transformRow(source, target, map, log, isUpdate) {
    var name = source.u_watchlist.toString();
    var split_names = name.split(","); //Breaking the input in a array
    var userArr = [];;

    for (i = 0; i < split_names.length; i++)
    {
        var gr = new GlideRecord('sys_user');
        gr.get('email', split_names[i]);
        if (gr.isValidRecord()) {
			userArr.push(gr.getUniqueValue());
        }
    }

	target.setValue('u_watch_list', userArr.join(','));

})(source, target, map, log, action === "update");

 

Cheers

 

View solution in original post

3 REPLIES 3

James Chun
Kilo Patron

Hey @JPSS,

 

Try the following code:

(function transformRow(source, target, map, log, isUpdate) {
    var name = source.u_watchlist.toString();
    var split_names = name.split(","); //Breaking the input in a array
    var userArr = [];;

    for (i = 0; i < split_names.length; i++)
    {
        var gr = new GlideRecord('sys_user');
        gr.get('email', split_names[i]);
        if (gr.isValidRecord()) {
			userArr.push(gr.getUniqueValue());
        }
    }

	target.setValue('u_watch_list', userArr.join(','));

})(source, target, map, log, action === "update");

 

Cheers

 

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @JPSS 

 

In your ServiceNow transform script, it seems you want to populate a list collector field (u_watch_list) in the target record based on a comma-separated string of user emails (u_watchlist) from the source record. However, there are a couple of issues in your script:

  1. You're using split_names[0] in your loop, which means you're always accessing the first element of the split_names array. Instead, you should use split_names[i] to access each element in the loop.

  2. You're concatenating the sys_id values with , to the existing u_watch_list value. But this may result in an extra comma at the beginning. You should handle this case to avoid it.

Here's the corrected version of your transform script:

 

(function transformRow(source, target, map, log, isUpdate) {
    var name = source.u_watchlist.toString();
    var split_names = name.split(","); // Breaking the input into an array
    var num_names = split_names.length;
    
    for (var i = 0; i < num_names; i++) {
        var gr = new GlideRecord('sys_user');
        if (gr.get('email', split_names[i])) {
            target.u_watch_list = (target.u_watch_list ? target.u_watch_list + ',' : '') + gr.getValue('sys_id');
        }
    }
})(source, target, map, log, action === "update");

 

 

This script iterates over each email in the u_watchlist field, retrieves the corresponding sys_id from the sys_user table, and appends it to the u_watch_list field in the target record. It handles the case where the u_watch_list field might already have values by checking if it's not empty before concatenating.

 

🙂

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

Sumanth16
Kilo Patron

Hi @JPSS ,

 

Update below script according to your requirement:

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

	var grp_type = [];
	var inputArray = source.u_group_type.split(',');
	var gtype = new ArrayUtil().unique(inputArray);
	for (var i = 0; i < gtype.length; i++) {
		var grpType = new GlideRecord('sys_user_group_type');
		grpType.addQuery('name', gtype[i].trim());
		grpType.query();
		if (grpType.next()) {
			grp_type.push(grpType.sys_id.toString());
		}
	}
	var listArray = target.type.toString().split(',');
	listArray = new ArrayUtil().concat(listArray, grp_type);
	var uniqueArray = new ArrayUtil().unique(listArray);
	target.type = uniqueArray.join(',');

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

 

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda