Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Remove element from an array

pennieturner
Mega Guru

Hi

I'm trying to remove 1 element from an array, but I'm a little stuck on how to do it.

I have a watch list where the callers manager gets added it to, so, if the callers manager changes, I need to remove the old callers manager from it.

I've got far:

updateWatchList();

function updateWatchList() {

var oldCallerMgr = previous.caller_id.manager;

var wList = current.watch_list;

var arr = [];

var newWList = [];

arr = wList.split(',');

var i = 0;

for (; i < arr.length; i++) {

  if (arr[i]!= "") {

  var existWList = arr[i];

  gs.log('existWList person is - ' +existWList);

  if (oldCallerMgr != arr[i]) {

  newWList.push(existWList);

  }

  }

}

  gs.log('newwatch list is - ' +newWList); //This seems to be correct

current.watch_list = newWList;

gs.log('my new watch list is - '+current.watch_list);   //it comes out org.mozilla.javascript.NativeArray@13bae85

}

It seems to work barr the current.watch_list - newWList?   I get the org.mozilla.javascript.NativeArray@13bae85 error?

Any help is appreciated

1 ACCEPTED SOLUTION

srinivasthelu
Tera Guru

Hi Pennie,



current.watch_list expects string.   You need to join the array first in LineNumber 22



current.watch_list = newWList.join(',');  


gs.log('my new watch list is - '+current.watch_list);   //it comes out org.mozilla.javascript.NativeArray@13bae85  



Thanks


Srini


View solution in original post

6 REPLIES 6


  1. updateWatchList();  
  2. function updateWatchList() {  
  3. var oldCallerMgr = previous.caller_id.manager;  
  4. var currentCallerMgr=current.caller_id.manager;
  5. var wList = current.watch_list;  
  6. var arr = [];  
  7. arr = wList.split(',');  

                    var index=arr.indexOf(oldCallerMgr);


  1. if(index>=0)
  2. {
  3. arr[index]=currentCallerMgr;
  4. }
  5. current.watch_list = arr.join(',');  
  6. }  



Hi Pennie,



The above code should work for you;



Hope that helps


Srini



srinivasthelu
Tera Guru

Hi Pennie,



current.watch_list expects string.   You need to join the array first in LineNumber 22



current.watch_list = newWList.join(',');  


gs.log('my new watch list is - '+current.watch_list);   //it comes out org.mozilla.javascript.NativeArray@13bae85  



Thanks


Srini