Watch List Service Portal Help

Chaz
Tera Guru

Hello Community!

I am working on a Service Portal widget to allow users to add to the watch list. I used the code from the widget provided in the ServiceNow Elite blog.

https://www.servicenowelite.com/blog/2017/6/1/service-portal-watchlist-widget?rq=watch%20list

I took the code from both widgets and blended them together so that users can select someone from the list or add an email address manually. The code works fine for manual entries. The problem I'm running into is if you select someone from the users list and the manual box is blank it sets an "undefined" value as well. I'm not the greatest at widget scripting and was wondering if anyone could spot my issue?

Widget Screenshot

find_real_file.png

 

HTML

<div class="panel panel-default" ng-if="data.canRead">  
	<div class="panel-heading">       
    <h4 class="panel-title pull-left" style="font-size: 14px;" ng-click="c.variable_toggle = !c.variable_toggle" aria-expanded="{{c.variable_toggle}}" aria-controls="variables-toggle">       
     <span style="font-size: 12px;" class="glyphicon" ng-class="c.variable_toggle ? 'glyphicon-chevron-down' : 'glyphicon-chevron-up'"></span> ${Watch List Actions}
    </h4>   
    <div class="clearfix"></div>       
  </div>   
  <div ng-if="c.variable_toggle"class="panel-subheading" align="center" style="font-size: 12px;">${Click in the white box below to add users and select the "Update" button}</div>
  <div ng-if="c.variable_toggle"class="panel-body">
    <form ng-click="save()">
      <div class="text-center text-italic text-muted">
        <div>
          <sn-record-picker field="watch_list" sn-disabled="!data.canWrite" table="'sys_user'" display-fields="'name'" display-field="'email'" search-fields="'name'" value-field="'sys_id'" default-query="'active=true^web_service_access_only=false^user_nameISNOTEMPTY^emailISNOTEMPTY'" page-size="10" multiple="true"></sn-record-picker>   </div>
        <div style="margin-top:15px; float: right;">
          <button ng-if="data.canWrite" type="submit" class="btn btn-primary btn-sm">${Update}</button>
        </div>
      </div>
    </form>
  </div>
  
  <div ng-if="c.variable_toggle" class="panel-body" style="font-size: 12px;">
<form>
${Distribution lists: type an email address in the box below and select the "Update" button}
<div class="textbox_space">
<input ng-disabled="!data.canWrite" ng-model="data.watch_list" id="watch_list_dist" class="form-control" type="email" maxlength="50" />
</div>
<input ng-if="data.canWrite" ng-click="save()" type="submit" id="submit" value="Update" class="btn btn-primary btn-sm" style="margin-top:10px; float: right;"/>
</form>
  </div>

 

Client Script

function($scope, spUtil, $http) {   
	var c = this;   

	$scope.watch_list = {   
		displayValue: c.data.displayValue,   
		value: c.data.value,   
		name: 'watch_list'
	};   
	$scope.save = function(){   
		c.data.watchList = $scope.watch_list.value;   
		c.server.update().then(function()   {
		
	spUtil.recordWatch($scope, c.data.table, "sys_id=" + c.data.sys_id, function(name, data) {   
		if(name.name == 'record.updated' && data.operation == 'update'){   
			$scope.watch_list.value = data.record.watch_list.value;   
			$scope.watch_list.displayValue = data.record.watch_list.display_value;   
			$scope.$apply();   
		} 
	});   
	});
	$scope.$on("field.change", function(evt, parms) {
		if (parms.field.name == 'watch_list'){}
		
		});
	};
}

 

Server Script

(function() {   
	var gr;
//if the user inputs data
	if(input){   
		gr = new GlideRecord(input.table);   
		if(gr.get(input.sys_id)){   
	// if user has write access allow update of list
			if(gr.watch_list.canWrite()){   
				gr.watch_list = input.watchList;   
				gr.update();   
				gs.addInfoMessage('Watch List Updated');   
			}   
	//if user does not have write access show error message
			else{   
				gs.addErrorMessage("Update failed, you don't have the required access");   
			}   
		}   
	}   
	//if user isn't inputting data
	else{   
		var table = $sp.getParameter('table');   
		var sys_id = $sp.getParameter('sys_id');   
		gr = new GlideRecord(table);
	//Get the record data
		if(gr.get(sys_id)){   
			data.table = table;   
			data.sys_id = sys_id;   
			data.canRead = gr.watch_list.canRead();   
			data.canWrite = gr.watch_list.canWrite();   
	//if user has read rights, display the watch list values
			if(data.canRead){   
				var dV = gr.getDisplayValue('watch_list');   
				var sV = gr.getValue('watch_list');   
				//data.displayValue = dV == '' ? [] : dV;   
				//data.value = sV == null ? [] : sV;   
				data.displayValue = dV;
				data.value = sV;
			}   
		}   
	}   
	
})();
	
	/*distribution list function*/
(function() {
data.sys_id = $sp.getParameter('sys_id');
//if the user inputs data find the record
if (input) {
data.sys_id = input.sys_id;
var grTask = GlideRecord("task");
grTask.query("sys_id", data.sys_id);
grTask.query();

if (grTask.next()) {
var wList = grTask.watch_list;
var wEmail = input.watch_list;
// If there's already users listed
if(wList != "") {
wList = (wList + "," + wEmail);
}
//If the watch list is empty
else {
wList = wEmail;
}
//Update the record with new Watch List values
grTask.watch_list = wList;
data.displayValue = wEmail;
grTask.update();
}
}
})();

	

 

Any help is greatly appreciated!

1 ACCEPTED SOLUTION

Mike Patel
Tera Sage

Try below server script

(function() {
	var gr;
	//if the user inputs data
	if(input){
		gr = new GlideRecord(input.table);
		if(gr.get(input.sys_id)){
			// if user has write access allow update of list
			if(gr.watch_list.canWrite()){
				gr.watch_list = input.watchList;
				gr.update();
				gs.addInfoMessage('Watch List Updated');
			}
			//if user does not have write access show error message
			else{
				gs.addErrorMessage("Update failed, you don't have the required access");
			}
		}
	}
	//if user isn't inputting data
	else{
		var table = $sp.getParameter('table');
		var sys_id = $sp.getParameter('sys_id');
		gr = new GlideRecord(table);
		//Get the record data
		if(gr.get(sys_id)){
			data.table = table;
			data.sys_id = sys_id;
			data.canRead = gr.watch_list.canRead();
			data.canWrite = gr.watch_list.canWrite();
			//if user has read rights, display the watch list values
			if(data.canRead){
				var dV = gr.getDisplayValue('watch_list');
				var sV = gr.getValue('watch_list');
				//data.displayValue = dV == '' ? [] : dV;
				//data.value = sV == null ? [] : sV;
				data.displayValue = dV;
				data.value = sV;
			}
		}
	}

})();

/*distribution list function*/
(function() {
	data.sys_id = $sp.getParameter('sys_id');
	//if the user inputs data find the record
	if (input) {
		if(input.watch_list){
			data.sys_id = input.sys_id;
			var grTask = GlideRecord("task");
			grTask.query("sys_id", data.sys_id);
			grTask.query();

			if (grTask.next()) {
				var wList = grTask.watch_list;
				var wEmail = input.watch_list;
				// If there's already users listed
				if(wList != "") {
					wList = (wList + "," + wEmail);
				}
				//If the watch list is empty
				else {
					wList = wEmail;
				}
				//Update the record with new Watch List values
				grTask.watch_list = wList;
				data.displayValue = wEmail;
				grTask.update();
			}
		}
	}
})();

View solution in original post

19 REPLIES 19

Shane Craddock
Kilo Expert

Hi Chazzer,

Your "distribution list function" is triggering when you update, there are no checks for valid values so 'wEmail' is undefined and it gets added to the watch list.

 

If this answer helped you please mark it as Helpful, if it solved your issue please mark as Correct, thank you!

Chaz
Tera Guru

Hi Shane,

 

Thanks for the follow up, I'm not quite sure how to put that check into place in this distribution list function. I've tried a few different ways and keep getting undefined.

Mike Patel
Tera Sage

Try below server script

(function() {
	var gr;
	//if the user inputs data
	if(input){
		gr = new GlideRecord(input.table);
		if(gr.get(input.sys_id)){
			// if user has write access allow update of list
			if(gr.watch_list.canWrite()){
				gr.watch_list = input.watchList;
				gr.update();
				gs.addInfoMessage('Watch List Updated');
			}
			//if user does not have write access show error message
			else{
				gs.addErrorMessage("Update failed, you don't have the required access");
			}
		}
	}
	//if user isn't inputting data
	else{
		var table = $sp.getParameter('table');
		var sys_id = $sp.getParameter('sys_id');
		gr = new GlideRecord(table);
		//Get the record data
		if(gr.get(sys_id)){
			data.table = table;
			data.sys_id = sys_id;
			data.canRead = gr.watch_list.canRead();
			data.canWrite = gr.watch_list.canWrite();
			//if user has read rights, display the watch list values
			if(data.canRead){
				var dV = gr.getDisplayValue('watch_list');
				var sV = gr.getValue('watch_list');
				//data.displayValue = dV == '' ? [] : dV;
				//data.value = sV == null ? [] : sV;
				data.displayValue = dV;
				data.value = sV;
			}
		}
	}

})();

/*distribution list function*/
(function() {
	data.sys_id = $sp.getParameter('sys_id');
	//if the user inputs data find the record
	if (input) {
		if(input.watch_list){
			data.sys_id = input.sys_id;
			var grTask = GlideRecord("task");
			grTask.query("sys_id", data.sys_id);
			grTask.query();

			if (grTask.next()) {
				var wList = grTask.watch_list;
				var wEmail = input.watch_list;
				// If there's already users listed
				if(wList != "") {
					wList = (wList + "," + wEmail);
				}
				//If the watch list is empty
				else {
					wList = wEmail;
				}
				//Update the record with new Watch List values
				grTask.watch_list = wList;
				data.displayValue = wEmail;
				grTask.update();
			}
		}
	}
})();

Thanks for the code.

It works well for Incident/Request ... but not on record producer 

Could you explain what needs to be change to be able to add external email to watch list from Incident record producer 

Regards