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

Sorry, I did it quick and forgot to test with an empty watch list. I updated the code I posted with the following change: I replaced if(gr.getValue('watch_list') != '') with: if(!gr.watch_list.nil())



On my side it is working with this. If it's not working, please provide the logs that are given in your browser console.




I replaced the server code and still the widget did not appear. Here's the error from browser console:


TypeError: Cannot read property 'startsWith' of undefined


      at isBlockedTable (js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Mar_28_20_39_49_PDT_2017&c=2_42:56122)


      at Object.initChannel (js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Mar_28_20_39_49_PDT_2017&c=2_42:56098)


      at Object.recordWatch (js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Mar_28_20_39_49_PDT_2017&c=2_42:66766)


      at new api.controller (ticket_watchlist_jgs.js:16)


      at Object.invoke (js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Mar_28_20_39_49_PDT_2017&c=2_42:8230)


      at $controllerInit (js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Mar_28_20_39_49_PDT_2017&c=2_42:10657)


      at nodeLinkFn (js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Mar_28_20_39_49_PDT_2017&c=2_42:9945)


      at compositeLinkFn (js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Mar_28_20_39_49_PDT_2017&c=2_42:9521)


      at publicLinkFn (js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Mar_28_20_39_49_PDT_2017&c=2_42:9444)


      at Object.render (js_includes_sp.jsx?v=07-20-2017_1155&lp=Tue_Mar_28_20_39_49_PDT_2017&c=2_42:69355)


Ok, I took your script to update it to make sure I have the same thing as you have.



First thing, taking it as is it is not working for me a I get the following error: Uncaught TypeError: scope.field.value.split is not a function



This seems to be caused by the use of an array instead of using a string because usually the split function is called on a string and is not available for an array.



Here is the updated server function:



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


}


});


}


var dV = emails.join(',');


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


data.displayValue = dV;      


data.value = sV == null ? "" : sV;      


                      }      


              }      


      }      


})();  




It is working in my developer instance on Jakarta release on a test page having this single widget. If you still have an error maybe you should try with a test page to see if your issue is not related to a library being loaded or another widget.


Hi Laurent, the new server script is now working on my side as well - it now shows the email address instead of display name. Thank you very much for your swift response.


Just noticed now that this does not check whether the incident ticket is active still active or not, which leads us to unexpected behavior that when user is viewing the ticket and the incident was closed then user can still edit the watch list.



I was trying to play around with the record watcher you incorporated on client script but can't make it work so that when ticket is not active anymore the watch list will be disabled. I know that I can add an OR statement on the sn-disabled property and check on server script if ticket is active or not but this only applied when user reloads the page, when user does not reload and ticket is updated it is not captured.