How to concatenate an Array with a List field

Sue Frost
Giga Guru

I would like to maintain a field (Linked Records) where I store all the records to which a case is, well, linked. That field will then feed the custom relationships I've built.

When saving a case, the code should:

  • get the values currently in the Linked Records field
  • check all the fields which may contain relevant links
  • concatenate the two lists
  • remove any duplicates

The code I have right now is throwing errors - "Cannot find function unique in object [object Object],566d3ee51b5a338007047592cd4". If I fix one error, another, similiar one pops up.

Here's my current code:

/*
The 'Linked Records' field concatenates all related records - PI Cases, System Inquiries, Extreme Quotes, etc. into one list field. This field can then be used in relationships, related lists, etc.

If more fields are needed, just add them into the array.
 */

(function executeRule(current, previous /*null when async*/) {
	
	// Write any existing links out to array
	var exList = new global.ArrayUtil();
	var linkedRecArr = current.u_linked_records.split(',');

	gs.info("Update Linked Records - linkedRecArr : " + linkedRecArr);
	
	// Check new fields
	var exFields = new global.ArrayUtil();
	exFields = [
	current.u_created_from_system_inquiry,
	current.u_pi_case,
	current.u_copied_case,
	current.u_duplicate_of,
	current.u_quote_number,
	current.parent
	];
	
	gs.info("Update Linked Records - linkedRecArr : " + linkedRecArr[0]);
	gs.info("Update Linked Records - exFields : " + exFields[0]);
	
	//Join lists
	var newList = new global.ArrayUtil();	
	var newList1 = newList.union(exList, exFields);
	gs.info("Update Linked Records - newList1 : " + newList1[0]);

        //var newList1 = new global.ArrayUtil();
	var newList2 = new global.ArrayUtil(); 

	//Remove duplicates
	newList2 = newList1.unique(newList1);
	
	//And write out the list
	current.u_linked_records = newList2.join(',');
	
})(current, previous);

 

If someone can point out what's wrong here, I'd really appreciate the assistance.

 

 

1 ACCEPTED SOLUTION

Brian Dailey1
Kilo Sage

Hi Sue,

 

Your issue is that your "newList.union(...)" method will return an array (i.e., newList1 is an array, not an ArrayUtil), which does not have the method 'unique()' that you are trying to call.  Only your ArrayUtil variables would have the unique() method (i.e., newList or newList2).

var newList = new global.ArrayUtil();	
var newList1 = newList.union(exList, exFields);
gs.info("Update Linked Records - newList1 : " + newList1[0]);

//var newList1 = new global.ArrayUtil();
var newList2 = new global.ArrayUtil(); 

//Remove duplicates
newList2 = newList1.unique(newList1);

 

So in your code, newList.unique(...) would be a valid method call. (so would newList2.unique(...), for that matter).  But I think the following is what you are going for:

(function executeRule(current, previous /*null when async*/) {

	var linkedRecArr = current.u_linked_records.split(','); // Get array of existing linked values

	// Check additional fields for new values
	var exFields = [
		current.u_created_from_system_inquiry,
		current.u_pi_case,
		current.u_copied_case,
		current.u_duplicate_of,
		current.u_quote_number,
		current.parent
		];

	//Join lists of existing and new values
	var newList = new global.ArrayUtil();	
	var newList1 = newList.union(linkedRecArr, exFields);
	
	//Remove duplicates from resultant list
	var newList2 = newList.unique(newList1);
	current.u_linked_records = newList2.join(',');
	
})(current, previous);

 

Please give that a try.  Thanks.

-Brian

 

Edit/update: My apologies, the first time I only addressed the error you were seeing.  I spotted some other issues in your original logic and addressed those here.

View solution in original post

4 REPLIES 4

Prateek kumar
Mega Sage

Have you tried concat()

var newList = new global.ArrayUtil();

var newList1 = newList.concat(exList, exFields);


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

I did have that in there before. When I change 

 

var newList1 = newList.union(exList, exFields);

 to 

var newList1 = newList.concat(exList, exFields);

 

I get these log statements, and the value currently in the Linked Records field is cleared.

 

Update Linked Records - linkedRecArr : 782092d16f2ea10062dbb4ecbb3ee429,204b51006fee210062dbb4ecbb3ee45c

Update Linked Records - newList1 : undefined

Update Linked Records - newList2 : undefined

 

There must be something else going on.

Why this line??

var newList1 = newList.union(exList, exFields);

Doesn't it needs to be

var newList1 = newList.union(linkedRecArr, exFields);


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Brian Dailey1
Kilo Sage

Hi Sue,

 

Your issue is that your "newList.union(...)" method will return an array (i.e., newList1 is an array, not an ArrayUtil), which does not have the method 'unique()' that you are trying to call.  Only your ArrayUtil variables would have the unique() method (i.e., newList or newList2).

var newList = new global.ArrayUtil();	
var newList1 = newList.union(exList, exFields);
gs.info("Update Linked Records - newList1 : " + newList1[0]);

//var newList1 = new global.ArrayUtil();
var newList2 = new global.ArrayUtil(); 

//Remove duplicates
newList2 = newList1.unique(newList1);

 

So in your code, newList.unique(...) would be a valid method call. (so would newList2.unique(...), for that matter).  But I think the following is what you are going for:

(function executeRule(current, previous /*null when async*/) {

	var linkedRecArr = current.u_linked_records.split(','); // Get array of existing linked values

	// Check additional fields for new values
	var exFields = [
		current.u_created_from_system_inquiry,
		current.u_pi_case,
		current.u_copied_case,
		current.u_duplicate_of,
		current.u_quote_number,
		current.parent
		];

	//Join lists of existing and new values
	var newList = new global.ArrayUtil();	
	var newList1 = newList.union(linkedRecArr, exFields);
	
	//Remove duplicates from resultant list
	var newList2 = newList.unique(newList1);
	current.u_linked_records = newList2.join(',');
	
})(current, previous);

 

Please give that a try.  Thanks.

-Brian

 

Edit/update: My apologies, the first time I only addressed the error you were seeing.  I spotted some other issues in your original logic and addressed those here.