comparison between two strings

Maharshi Chatte
Kilo Guru

Hi,

I have two variables - test and target .both contains a list of sys_Id separated by comma. Now I have to write a business rule which compare the sys_ids in test with that of the target and will throw an error message if any of the sys_id in test is not present in the target. How can I write a script for this?

 

 

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Maharshi,

Fields "test" and "target" are both of type "list".

  var testArray = test.split(',');
  var targetArray = target.split(',');

  var arrayUtil = new ArrayUtil();

  var intersect = arrayUtil.intersect(targetArray, testArray);

  if (intersect.length != testArray.length) {
     gs.addErrorMessage('There are some items in test array that is not in target array.');
     current.setAbortAction(true);

 I've tested the script using the following code to make sure it works as expected.

var target = '4f1764429780111086d3b4b3f153afba,4f1764429780111086d3b4b3f153afbd,4f1764429780111086d3b4b3f153afbc,62826bf03710200044e0bfc8bcbe5df1,a8f98bb0eb32010045e1a5115206fe3a'
var grTable = new GlideRecord('a0_table2');
if (grTable.get('4f1764429780111086d3b4b3f153afbc')) {
  var test= grTable.u_list_field;  // field of type 'list'
  
  var testArray = test.split(',');
  var targetArray = target.split(',');

  var arrayUtil = new ArrayUtil();

  var intersect = arrayUtil.intersect(targetArray, testArray);

  if (intersect.length != testArray.length) {
     gs.addErrorMessage('There are some items in test array that is not in target array.');
     current.setAbortAction(true);
  }
}

View solution in original post

19 REPLIES 19

Yes, so your a1 will be test, and a2 will be target.

No matter the size of array, this will work. just make sure your list is an array.

Best Regards
Aman Kumar

@Maharshi Chatterjee 

Did you try this, this should work for your req

Best Regards
Aman Kumar

Hey,

@Maharshi Chatterjee 

I suggested using intersect in the beginning itself, but glad that you found the solution and ignored this one

Best Regards
Aman Kumar

Rahul Talreja
Mega Sage
Mega Sage

Hi Maharshi,

You can use below snippet, it returns true if all values of list one is present in list two, otherwise false.

var target = ['B', 'C', 'A', 'D'];
var test = ['D', 'C'];

var result = test.every(function(val) {

  return target.indexOf(val) >= 0;

});

gs.log(result);

 

Mark my response correct/helpful as applicable,

Thanks and Regards,
Rahul

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

 

Hi,

var target = [ 'D', 'C'];
var test = ['D', 'C'];

var result = test.every(function(val) {

return target.indexOf(val) >= 0;

});

gs.log(result);

 

For this input also it is giving result = true