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.

add current loggedin user to watchlist using client script...

hasthipooji
Tera Contributor
   IN THIS CODE WHILE DISPLAYING IN G_FORM IT IS NOT SHOWING .....
g_form.addInfoMessage("userId ofwatchlist "+g_form.getDisplayBox('watch_list').value);
}
THIS ISNOT DISPLAYING IN MY FORM WHY? WHATS THE MISTAKE IN CODE


var watchList=g_form.getValue('watch_list');
    var userId= g_user.userID;
    if (!watchList.includes(userId)) {
    if(watchList==""){
        g_form.setValue('watch_list',userId);
    }
    else{
        g_form.setValue('watch_list',watchList+" "+userId);
    }
  }
        g_form.addInfoMessage("userId ofwatchlist "+g_form.getDisplayBox('watch_list').value);
}
1 REPLY 1

M Iftikhar
Tera Sage

Hello @hasthipooji,

 

The issue is that g_form.getDisplayBox('watch_list').value  returns null or undefined, because the Watch list field is not a simple reference field , it’s a Glide List type. The  g_form.getDisplayBox('watch_list').value method works with reference fields, not glide lists.

 

You can fix it by using getDisplayValue() instead.Corrected version of your script:

 

    var watchList = g_form.getValue('watch_list');
    var userId = g_user.userID;

    // If user not already in watch list
    if (!watchList.includes(userId)) {
        if (watchList == "") {
            g_form.setValue('watch_list', userId);
        } else {
            g_form.setValue('watch_list', watchList + "," + userId);
        }
    }

    // Wait a bit for the display value to update
    setTimeout(function() {
        g_form.addInfoMessage("userId of Watch List: " + g_form.getDisplayValue('watch_list'));
    }, 1000);

This should correctly display the updated watch list on your form.


If my response helped, please mark it as the accepted solution so others can benefit as well.    

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.