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 Sean,



This is being caused by one line of code in the server side portion of the widget:



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



When you use getValue and there is nothing in that field, getValue returns 'null', this means the clinet side see's the javascript value null and cant deal with it.



I suggest replacing the 2 lines:



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


with these lines:



var dV = gr.getDisplayValue('watch_list');
var sV = gr.getValue('watch_list');
data.displayValue = dV == '' ? [] : dV;
data.value = sV == null ? [] : sV;



This will ensure at least a blank array is sent, which can be split.



Edit: I don't know how to do the fancy code blocks sorry if it's tough to read


This shows the display value which is the name, can we make this show the email address instead?


siladityamishra
Kilo Contributor

Hi Guys,


Does any one figured out how to add save functionality, so that it will update added watch list user to the incident record?


I am struggling to get it done from last few days.



Any suggestions or ideas will help me a lot.



Hi Siladitya,



How do you which to update the watch list? On save (another button on your page, that is probably in another widget), as soon as a user is added/removed or a dedicated button in the watch list widget?



Having an idea of which widgets you want to combine this with would help (especially if they are default widgets).


Hi Laurent,



I have just used your above code to get watch list on the service portal page. As of now when a user is added to the watch list in the back end in ServiceNow the user is visible in the portal page while opening the incident.


However now I want the user should be able to add a person in the watch list from the Service portal page, and the same should reflect in the back end of ServiceNow now.



I have added a button on the page, so that the user should be able to save the watch list after adding user(s) to 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">


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


          <sn-record-picker on-change="getWatchlists(watch_list)" 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>


              <input type="button" value = "Apply"/>


       


      </div>


  </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.getWatchlists = function (cObj) {


  var value = document.getElementById(cObj).value;


  console.log("Value: "+value);



  }


}



Server side script:-


---------------------------------


(function() {  


         


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


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


  var gr = new GlideRecord('task');


  if(gr.get(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;


              }


  }



})();



Please let me know if you need any more details.