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.

Business rule add in Watchlist

Max Lin
Tera Contributor

hi there!

I have a Problem watchlist already has some user, say original1@gmail.com and original2@gmail.com.

I added the below BIZ Rule Script, it seems like it will replace the original1@gmail.com and original2@gmail.com and add in the below testing1@gmail.com

 

I'm trying to prevent the replace, but to add in more email addresses. How can i do that in UI?

 

Thanks! 

 

 

MaxLin_0-1665991861686.png

 

1 ACCEPTED SOLUTION

Murthy Ch
Giga Sage

Hi @Max Lin 

So here we can have a if validation and check whether gmail is already present or not.

If not present then add those gmails into watchlist.

Here is the updated logic:

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

    // Add your code here
    if (current.u_list_user.indexOf("testing1@gmail.com") < 0)
        current.u_list_user += "," + "testing1@gmail.com";
    if (current.u_list_user.indexOf("testing3@gmail.com") < 0)
        current.u_list_user += "," + "testing3@gmail.com";
    if (current.u_list_user.indexOf("testing2@gmail.com") < 0)
        current.u_list_user += "," + "testing2@gmail.com";

    //current.watch_list+=","+"testing1@gmail.com"+","+"testing2@gmail.com"+","+"testing3@gmail.com";

})(current, previous);

Hope it helps

(=tested)

 

Thanks,
Murthy

View solution in original post

4 REPLIES 4

Murthy Ch
Giga Sage

Hi @Max Lin 

In advanced script section try below logic:

 

 

current.watch_list+=","+"testing1@gmail.com"+","+"testing2@gmail.com"+","+"testing3@gmail.com";

 

 

Hope it helps

 

 

Thanks,
Murthy

Hi Murthy! Thanks for your reply! 

 

I tested, seems like everytime it's adding the same email address everytime the business rule criteria matches.

Is there anyway we can do a check before applying the adding of email?

 

MaxLin_0-1666083420996.png

 

Murthy Ch
Giga Sage

Hi @Max Lin 

So here we can have a if validation and check whether gmail is already present or not.

If not present then add those gmails into watchlist.

Here is the updated logic:

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

    // Add your code here
    if (current.u_list_user.indexOf("testing1@gmail.com") < 0)
        current.u_list_user += "," + "testing1@gmail.com";
    if (current.u_list_user.indexOf("testing3@gmail.com") < 0)
        current.u_list_user += "," + "testing3@gmail.com";
    if (current.u_list_user.indexOf("testing2@gmail.com") < 0)
        current.u_list_user += "," + "testing2@gmail.com";

    //current.watch_list+=","+"testing1@gmail.com"+","+"testing2@gmail.com"+","+"testing3@gmail.com";

})(current, previous);

Hope it helps

(=tested)

 

Thanks,
Murthy

Mohit Kaushik
Mega Sage
Mega Sage

Hi @Max Lin ,

 

You can put a check before adding a new email address to the watchlist by using below code:

 

var watcList = current.watch_list.toString();
var email = 'the email you want to add';
if(watcList.indexOf(email) ==-1){
current.watch_list+=","+email;
}

 

 

Please mark my answer as correct and helpful if it resolved your query.

 

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)