Copy Existing Values and Add New to Watch List onChange

Sofija
Giga Expert

Hi All!

I was hoping maybe someone encountered the same issue or would be able to help regarding watch list.

I currently have client script running to add specified email based on incident priority selected. However, once user chooses to change the priority - the existing watchlist is replaced with the new value defined in the client script.

What I would like it to do is:

1) Replace one email DL with another based on priority

AND

2) Copy all other users added to the watch list which not need to be replaced by the new value

I tried to modify the client script to copy existing value and replace just one of them, but I feel stuck

I hope I am making myself clear, but if no please let me know and I will try to be more detailed and specific

Thank you in advance!

Kamile

1 ACCEPTED SOLUTION

Those should be fine.   It is because I used the same variable names.   You could change the names, or you could pull newWl up to the top and do this:



  1. var wl = g_form.getValue('watch_list');
  2. var newWl = '';
  3.   if (newValue == 1 ) {  
  4.   if(wl.indexOf("email2@email.com") > -1){  
  5.   newWl = wl.replace("email2@email.com", "email@email.com");  
  6.   }else{  
  7.   newWl = wl + ",email@email.com";  
  8.   }  
  9.   /*var alist = g_form.getValue('watch_list');
  10.   var arrlist =   alist.split(",");
  11.   arrlist.push("email@email.com");
  12.   g_form.setValue("watch_list", arrlist);*/  
  13.   g_form.setValue("watch_list", newWl);  
  14.   }  
  15.  
  16.   if (newValue == 2) {  
  17.   if(wl.indexOf("email@email.com") > -1){  
  18.   newWl = wl.replace("email@email.com", "email2@email.com");  
  19.   }else{  
  20.   newWl = wl + ",email2@email.com";  
  21.   }  
  22.   /*var alst = g_form.getValue('watch_list');
  23.   var arrlst =   alst.split(",");
  24.   arrlst.push("email2@email.com");
  25.   g_form.setValue("watch_list", arrlst);*/  
  26.   g_form.setValue("watch_list", newWl);  
  27.   }  

View solution in original post

10 REPLIES 10

Mike Allen
Mega Sage

Can you post your script?   That will help us provide feedback.


This is the original script (attempts to keep the users on watch list were removed so this is blank slate).




function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
          return;
    }


    //Type appropriate comment here, and begin script below

if (newValue == 1 ) {
  /*var alist = g_form.getValue('watch_list');
  var arrlist =   alist.split(",");
 
  arrlist.push("email@email.com");
  g_form.setValue("watch_list", arrlist);*/


  g_form.setValue("watch_list", "", "email@email.com");
}

if (newValue == 2) {
  /*var alst = g_form.getValue('watch_list');
  var arrlst =   alst.split(",");
  arrlst.push("email2@email.com");
  g_form.setValue("watch_list", arrlst);*/


  g_form.setValue("watch_list", "", "email2@email.com");
}
   
if (newValue == 3 || newValue == 4) {
  var watchlist = g_form.getValue('watch_list');
  var newwatch =   watchlist.split(",");
  //alert(watchlist);
 
  var outarr = [];
 
  var arrayLength = newwatch.length;
  for (var i = 0; i < arrayLength; i++) {
    //alert(newwatch[i]);
    if (newwatch[i] == "email@email.com" ||
            newwatch[i] == "email2@email.com" ) {
    } else
    {
      outarr.push(newwatch[i]);
    }
   
  }
 
  g_form.setValue("watch_list",outarr);


}  



}


I did this and it works:



var wl = g_form.getValue('watch_list');


  if (newValue == 1 ) {


  if(wl.indexOf("email2@email.com") > -1){


  var newWl = wl.replace("email2@email.com", "email@email.com");


  }else{


  newWl = wl + ",email@email.com";


  }


  /*var alist = g_form.getValue('watch_list');


  var arrlist =   alist.split(",");



  arrlist.push("email@email.com");


  g_form.setValue("watch_list", arrlist);*/


  g_form.setValue("watch_list", newWl);


  }



  if (newValue == 2) {


  if(wl.indexOf("email@email.com") > -1){


  var newWl = wl.replace("email@email.com", "email2@email.com");


  }else{


  newWl = wl + ",email2@email.com";


  }


  /*var alst = g_form.getValue('watch_list');


  var arrlst =   alst.split(",");


  arrlst.push("email2@email.com");


  g_form.setValue("watch_list", arrlst);*/


  g_form.setValue("watch_list", newWl);


  }



Play around with that.


Hi Mike,



I tried using your example but I get errors of 'newWl' used out of scope and 'newWl' is already defined. Thoughts?



Note: I am using this on client script onChange and I am leaving the script starting if (newValue == 3 || newValue == 4) unchanged for clearing values.