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

Hi Laurent,

How to apply this in the 'Catalog Checkout' widget? I've created a watchlist field in the widget using sn-record-picker and have followed your scripts but when I submit by clicking the 'Checkout' button, the value of the watchlist field is not mapped in the request ticket. Any idea on where should I modify the scripts of that widget.

Thanks,

Raph

Hi Raphael, in the context of the Catalog Checkout, variables are not used. When looking at the OOB script, the order is made using the spScUtil javascript object, which ultimately calls the CartJS API which does not look to support passing watch list attributes or other request attributes (refer to https://community.servicenow.com/community?id=community_question&sys_id=e9d40866dbca9b042b6dfb651f96... for discussion on the subject).

The simplest option I see would be to manipulate the special_instructions field to push the watch list in that field. You could then add a before insert business rule that could retrieve information from that field and put it inside the watch list. I'm thinking of passing a JSON to the field so that you can easily parse it in the business rule. However you would need to make sure to not impact other requests that come from outside the service portal. I would probably do this by validating that the content of the field is a valid JSON before processing it.

I don't see any clean way to do it without rewriting the whole catalog API which would be heavy customization.

Hi Laurent,

Thank you for your response. any idea on how to push the watchlist value in special_instructions field? What part of the server script do I need to configure and what table will the value be stored so that I can GlideRecord it in the business rule.

Thanks, 

Raph

It's actually in the client script that it is being set. So everywhere special_script is passed to a function, you should update it to pass a JSON.

So for example, this would be the updated function call in of of the calls being made (it may vary based on how you store the watch list client side):

c.server.get({
	action: "change_shipping_info",
	requestedFor: c.requestedFor.value,
	special_instructions: JSON.stringify({
		special_instructions: c.special_instructions,
		watch_list: $scope.watch_list
	}),
	deliverTo: c.deliverTo,
	cart: {name: c.data.cart.name}
}).then(function(response) {
	c.data.cart = response.data.cart;
	c.updateDetails();
});



Folllowing that the value will be stored in the special_instruction field of the sc_request table (where you want the watch list from my understanding).

Thanks for the reply, how do i get the value of special_instructions in the portal so that i can call it in the business rule and set it to the watchlist field of the request.

current.watch_list = JSON.parse(special_instructions);

is that correct?