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,

I did assume that your onchange function was just for testing so I removed it in my code, here you go (I also added a record watcher to allow the watch list to be updated in real time to avoid potential conflicts).

 

I haven't played much with the service portal, so my code might not be optimal, feel free to improve it.

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">
      <form ng-submit="save()">
              <div class="text-center text-italic text-muted">
                  <div>
                          <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^web_service_access_only=false^user_nameISNOTEMPTY'" multiple="true"></sn-record-picker>  
                  </div>
                  <div style="float:right; margin-top:15px;">
                      <button type="submit" class="btn btn-primary">
                          ${Apply}
                          </button>
                  </div>
              </div>
      </form>  
  </div>  
</div>

Client controller

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();
              }
      });
}

Server script

(function() {
      var gr;
      if(input && typeof input.watchList != 'undefined'){
              gr = new GlideRecord(input.table);
              if(gr.get(input.sys_id)){
                      if(gr.watch_list.canWrite()){
                              gr.watch_list = input.watchList;
                              gr.update();
                              gs.addInfoMessage('Updated');
                      }
                      else{
                              gs.addErrorMessage("Update failed, you don't have the required access");
                      }
              }
      }
      else{
              var table = $sp.getParameter('table');
              var sys_id = $sp.getParameter('sys_id');
              gr = new GlideRecord(table);
              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(data.canRead){
                            data.displayValue = gr.getDisplayValue('watch_list') || '';
                            data.value = gr.getValue('watch_list') || '';
                      }
              }
      }
     
})();

Update 2018-07-30: The server side script has been updated following feedback of some errors in the widget.

 

Hi Laurent,



Thanks for the help!



I have just remove one of the condition from the Server side script, so that the user can make the watch list value EMPTY/NULL.



From


if(input && input.watchList)



To


if(input)



Thanks again


Hi,



I did this changes in my environment and it worked fine, but I want to add in the same widget the possibility to change the caller\requeter of the ticket. How can I do that?


When I set data.displayValue it always get the last field.


I've added the widget and though it seems to be working, it's throwing a bunch of errors.   Has anyone else seen that, and did you resolve them?


juliana3
Tera Contributor

You have to add as a list collector to the sys_user table and have the name equals to the table (watch_list).



De: jued0001


Enviada em: segunda-feira, 19 de junho de 2017 11:52


Para: Juliana Mendo <jmraimundo@stefanini.com>


Assunto: Re: - How to get a watch list in widget in Service Portal ?



<https://community.servicenow.com/?et=watches.email.thread>




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



reply from jued0001<https://community.servicenow.com/people/jued0001?et=watches.email.thread> in Platform - View the full discussion<https://community.servicenow.com/message/1176429?et=watches.email.thread#1176429>