is it possible to use recordwatch outside of the service portal?

sndangibbard
Mega Guru

I'd like to use it in a UI page/Script Include to make updates to the page whenever a new record comes in.

1 ACCEPTED SOLUTION

sndangibbard
Mega Guru

Yes it is! I combined a few of the existing AMB methods that already exist client side and you can hook into RecordWatch functionality!


var rw = (function(amb) {


  var watcherChannel;


  var connected = false;


  var diagnosticLog = true;


  function getFilterString(filter) {


      filter = filter.


      replace(/\^EQ/g, '').


      replace(/\^ORDERBY(?:DESC)?[^^]*/g, '').


      replace(/^GOTO/, '');


      return btoa(filter).replace(/=/g, '-');


  }


  function getChannelRW(table, filter) {


      var t = '/rw/default/' + table + '/' + getFilterString(filter);


      return amb.getChannel(t);


  }


  function initWatcher(table, sys_id, query) {


      if (!table)


          return;


      if (sys_id)


          var filter = "sys_id=" + sys_id;


      else


          filter = query;


      if (!filter)


          return;


      return initChannel(table, filter);


  }


  function initList(table, query) {


      if (!table)


          return;


      query = query || "sys_idISNOTEMPTY";


      return initChannel(table, query);


  }


  function initTaskList(list, prevChannel) {


      if (prevChannel)


          prevChannel.unsubscribe();


      var sys_ids = list.toString();


      var filter = "sys_idIN" + sys_ids;


      return initChannel("task", filter);


  }


  function initChannel(table, filter) {


      if (isBlockedTable(table)) {


          log("Blocked from watching", table);


          return null;


      }


      if (diagnosticLog)


          log(">>> init " + table + "?" + filter);


      watcherChannel = getChannelRW(table, filter);


      watcherChannel.subscribe(onMessage);


      amb.connect();


      return watcherChannel;


  }


  function onMessage(message) {


      var r = message.data;


      var c = message.channel;


      if (diagnosticLog)


          log(r);


  }


  function log(message) {


      console.log(message);


  }


  function isBlockedTable(table) {


      return table == 'sys_amb_message' || table.startsWith('sys_rw');


  }


  return {


      initTaskList: initTaskList,


      initChannel: initChannel,


      init: function(table, sys_id, query) {


          initWatcher(table, sys_id, query);


      },


      initList: initList,


      initRecord: function(table, sysId) {


          initWatcher(table, sysId, null);


      }


  }


})(window.top.g_ambClient);


rw.initList('sys_user');



This will initiate a watcher on the User table, any actions/checks you want to take are implemented in the onMessage function. You should also be able to watch a specific record rather than the entire table but I haven't tested that.


View solution in original post

2 REPLIES 2

sndangibbard
Mega Guru

Yes it is! I combined a few of the existing AMB methods that already exist client side and you can hook into RecordWatch functionality!


var rw = (function(amb) {


  var watcherChannel;


  var connected = false;


  var diagnosticLog = true;


  function getFilterString(filter) {


      filter = filter.


      replace(/\^EQ/g, '').


      replace(/\^ORDERBY(?:DESC)?[^^]*/g, '').


      replace(/^GOTO/, '');


      return btoa(filter).replace(/=/g, '-');


  }


  function getChannelRW(table, filter) {


      var t = '/rw/default/' + table + '/' + getFilterString(filter);


      return amb.getChannel(t);


  }


  function initWatcher(table, sys_id, query) {


      if (!table)


          return;


      if (sys_id)


          var filter = "sys_id=" + sys_id;


      else


          filter = query;


      if (!filter)


          return;


      return initChannel(table, filter);


  }


  function initList(table, query) {


      if (!table)


          return;


      query = query || "sys_idISNOTEMPTY";


      return initChannel(table, query);


  }


  function initTaskList(list, prevChannel) {


      if (prevChannel)


          prevChannel.unsubscribe();


      var sys_ids = list.toString();


      var filter = "sys_idIN" + sys_ids;


      return initChannel("task", filter);


  }


  function initChannel(table, filter) {


      if (isBlockedTable(table)) {


          log("Blocked from watching", table);


          return null;


      }


      if (diagnosticLog)


          log(">>> init " + table + "?" + filter);


      watcherChannel = getChannelRW(table, filter);


      watcherChannel.subscribe(onMessage);


      amb.connect();


      return watcherChannel;


  }


  function onMessage(message) {


      var r = message.data;


      var c = message.channel;


      if (diagnosticLog)


          log(r);


  }


  function log(message) {


      console.log(message);


  }


  function isBlockedTable(table) {


      return table == 'sys_amb_message' || table.startsWith('sys_rw');


  }


  return {


      initTaskList: initTaskList,


      initChannel: initChannel,


      init: function(table, sys_id, query) {


          initWatcher(table, sys_id, query);


      },


      initList: initList,


      initRecord: function(table, sysId) {


          initWatcher(table, sysId, null);


      }


  }


})(window.top.g_ambClient);


rw.initList('sys_user');



This will initiate a watcher on the User table, any actions/checks you want to take are implemented in the onMessage function. You should also be able to watch a specific record rather than the entire table but I haven't tested that.


Hey a few of us at my company are trying to figure out how you implemented this and made it work. Could you tell me specifically where you implemented this script to use it? Script includes? Client Scripts? I would love to understand how this works, because I just keep getting an error in console saying cannot read properties... top undefined...