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,



Is it possible to not show any results when user has not typed any value yet? And how would we change the displayed value to email instead of showing the name because some of our Exchange users does not have name defined (select DL list for example).


To show the email instead of the name you can change the server script to this:



(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){


var emails = [];


if(!gr.watch_list.nil()){


var usersId = gr.getValue('watch_list').split(',');


var userDisplay = gr.watch_list.getDisplayValue().split(',');


usersId.forEach(function(userId, index){


var userGr = new GlideRecord('sys_user');


if(userGr.get(userId) && !userGr.email.nil()){


emails.push(userGr.getValue('email'));


}


else{


//Keep display value


emails.push(userDisplay[index]);


}


});


data.displayValue = emails.join(',');


}


else{


data.displayValue = '';


}


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


}


data.formModel = $sp.getForm(table, sys_id, '', 'default_view');


}


})();



And change the sn-record-picker to this:


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



The server script allows existing member of the watch list that would not have an email address (or email addresses that would not be a reference to a user). However the field won't accept these inputs for new value.



For your question about not showing any result before the user gives an input, I don't see any good way to do it, and there is no option for that.


Tried this new server script and updated the sn-record-picker but the widget disappeared, seems like there's an error somewhere in the server script?


Could you post your previous server script?



Dix you call the widget with the table and sys_id parameter?



Are you able to get the errors in the log?


Here's the widget code:



HTML


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


  <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="'email'" display-fields="'name'" search-fields="'email'" 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 type="submit" class="btn btn-default btn-sm">${Update}</button>


              </div>


          </div>


      </form>


  </div>


</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(input){  


              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){  


var dV = gr.getDisplayValue('watch_list');  


                              var sV = gr.getValue('watch_list');  


                              data.displayValue = dV == '' ? [] : dV;  


                              data.value = sV == null ? [] : sV;  


                      }  


              }  


      }  


})();