How to get a watch list in widget in Service Portal ?

Nithin12
Tera Expert

Hi Team,

How to get watch list for a particular ticket opened in service portal.

How to create a widget for this?

Any suggestions or ideas will help me a lot.

Thanks.

PlatformDeveloper Community

1 ACCEPTED SOLUTION

Hi Nithin,



Here is a quick widget I made, however it does not have any save functionality so you would have to add that. Also the sn-record-picker does not allow to add an email address as the usual watch list field does. If your goal is only to display a read of what was set on creation than this would do the job by simply setting sn-disabled="true".



HTML:


<div ng-if="data.canRead" class="panel panel-primary b">


  <div class="panel-heading">


      <h4 class="panel-title pull-left">


          ${Watch list}


      </h4>


      <div class="clearfix"></div>


  </div>


  <div class="panel-body">


          <div class="text-center text-italic text-muted">


              <sn-record-picker field="watch_list" sn-disabled="!data.canWrite" table="'sys_user'" display-field="'name'" search-fields="'name'" value-field="'sys_id'" default-query="'active=true'" multiple="true"></sn-record-picker>


          </div>


  </div>


</div>



Client script:


function($scope, spUtil) {


      var c = this;


     


      $scope.watch_list = {  


              displayValue: c.data.displayValue,  


              value: c.data.value,


              name: 'watch_list'  


  };


}



Server script:


(function() {


     


      var table = $sp.getParameter('table')


      var sys_id = $sp.getParameter('sys_id')


      var gr = new GlideRecord(table);


      if(gr.get(sys_id)){


              data.canRead = gr.watch_list.canRead();


              data.canWrite = gr.watch_list.canWrite();


              if(data.canRead){


                      data.displayValue = gr.getDisplayValue('watch_list');


                      data.value = gr.getValue('watch_list');


              }


      }


})();



I know this is incomplete but I hope it helps.


View solution in original post

65 REPLIES 65

I did a bit of digging, and there is actually old documentation on the subject. https://docs.servicenow.com/bundle/paris-application-development/page/script/useful-scripts/task/t_AddFldSrvcCatCkout.html

This old documentation can be adapted to the portal. So no need to use the special_instructions or a business rule.

In my modifications attempt I did not do anything to cache the value like the other values are, which means that if the checkout window is closed, the watch list value is lost unlike other values.

In the client script add your watch list object (I did put it just after var c = this)

c.watchList = {
	displayValue: '',
	value: '',
	name: 'watch_list'
}

Update the additionParms of triggerCheckout function with:

var additionalParms = {'sysparm_requested_for': c.requestedFor.value,
													 'special_instructions': c.special_instructions,
													 'delivery_address': c.deliverTo,
													 'sysparm_processing_hint': 'setfield:request.watch_list=' + c.watchList.value
													};

Update the else case where c.server.update() is used.

...
else {
	c.data.delivery_address = c.deliverTo;
	c.data.special_instructions = c.special_instructions;
	c.data.requested_for = c.requestedFor.value;
	c.data.watch_list = c.watchList.value;
...

In your server script, using the documentation I linked add script to set the hint of the first item of the cart.

...
if (data.action !== 'order_now') {
	cartJS = new sn_sc.CartJS(cartName, '' + gs.getUser().getID());
	if(localInput.watch_list){
		var cartItemGr = new GlideRecord('sc_cart_item');
		cartItemGr.addQuery('cart', cartJS.getCartID());
		cartItemGr.query();
		if(cartItemGr.next()){
			cartItemGr.hints = "<hints><entry key='sysparm_processing_hint' value='setfield:request.watch_list=" + localInput.watch_list + "'/></hints>";
			cartItemGr.update();
		}
	}
...

I hope it helps you and that you are able to make it work.

Hi Laurent,

This is awesome! Thank you so much it is working now!!

Jamison Cote2
Mega Expert
This is a known issue with ServiceNow (KB Article Below) that I was attempting to work through. If you get "scope.field.value.split is not a function" in the console logs when attempting to add a watch list to a custom widget, you need to modify the server script in the custom widget to solve the error. Hope this helps.
//In the server script for that custom widget

//change this: 
data.displayValue = dV == '' ? [] : dV; 
data.value = sV == null ? [] : sV; 

//to this: 
data.displayValue = dV == '' ? "" : dV; 
data.value = sV == null ? "" : sV; 
 
https://hi.service-now.com/kb_view.do?sysparm_article=KB0696077 
 
Service Portal: Using snRecordPicker with multiple="true" has javascript error "scope.field.value.split is not a function" in Jakarta & Kingston
 

pagdenl
Giga Expert

Why doesn't ServiceNow create  OOB widget that only shows your tasks that you have been added to the watchlist.  Why is this so hard to do.