Name

global.PlannedTaskRelationDBService

Description

No description available

Script

var PlannedTaskRelationDBService = Class.create();
PlannedTaskRelationDBService.prototype = {
  TABLE: 'planned_task_rel_planned_task',

  initialize: function() {
  
  },

  allRelations: function (taskId, excludeIds) {
      PPMDebug.log("Into PlannedTaskRelationDBService.allRelations -> " + taskId);
      if(JSUtil.notNil(taskId)) {
          var gr = new GlideRecord(this.TABLE);
          gr.addQuery('child', taskId).addOrCondition('parent', taskId);
          if(JSUtil.notNil(excludeIds) && excludeIds.length > 0)
              gr.addQuery("sys_id", "NOT IN", excludeIds.join(","));
          gr.query();
          PPMDebug.log("Into PlannedTaskRelationDBService.allRelations count -> " + gr.getRowCount() + " - " + 
              gr.getEncodedQuery());
          return gr;
      }
  },

  relations: function (taskId) {
      PPMDebug.log("Into PlannedTaskRelationDBService.relations -> " + taskId);
      if(JSUtil.notNil(taskId)) {
          var gr = new GlideRecord(this.TABLE);
          gr.addQuery('child', taskId);
          gr.query();
          return gr;
      }
  },

  relationsCount: function (taskId) {
      PPMDebug.log("Into PlannedTaskRelationDBService.relationsCount -> " + taskId);
      if(JSUtil.notNil(taskId)) {
          var ga = new GlideAggregate(this.TABLE);
          ga.addAggregate("COUNT", "sys_id");
          ga.addQuery('child', taskId);
          ga.setGroup(false);
          ga.query();
          if(ga.next()) {
              return ga.getAggregate("COUNT", "sys_id");
          }
      }
      return 0;
  },
  
  externalRelationsCount: function(taskId) {
      var ga = new GlideAggregate(this.TABLE);
      ga.addAggregate("COUNT", "sys_id");
      ga.addQuery('child', taskId);
      ga.addNotNullQuery('orig_sys_id');
      ga.setGroup(false);
      ga.query();
      if(ga.next()) {
          return ga.getAggregate("COUNT", "sys_id");
      }
      return 0;
  },

  internalRelationsCount: function(taskId) {
      var ga = new GlideAggregate(this.TABLE);
      ga.addAggregate("COUNT", "sys_id");
      ga.addQuery('child', taskId);
      ga.addQuery('external', 'false');
      ga.addNullQuery('orig_sys_id');
      ga.setGroup(false);
      ga.query();
      if(ga.next()) {
          return ga.getAggregate("COUNT", "sys_id");
      }
      return 0;
  },

  externalRelations: function (sysId, filterQuery) {
      var relations = [], arrayUtil = new ArrayUtil();
      relations.concat(this.incomingExternalRelations(sysId, filterQuery));
      relations.concat(this.outgoingExternalRelations(sysId, filterQuery));
      return relations;
  },

  incomingExternalRelations: function (sysId, filterQuery) {
      PPMDebug.log('PlannedTaskRelationDBService.incomingExternalRelations: ' + sysId + " | " + filterQuery);
      var incomingRelations = [], relation;
      if(JSUtil.notNil(sysId)) {
          var gr = new GlideRecord(this.TABLE);
          gr.addQuery("child_top_task", sysId);
          gr.addQuery("external", "true").addOrCondition("external", "1");
          if(JSUtil.notNil(filterQuery))
              gr.addEncodedQuery(filterQuery);
          gr.query();
          PPMDebug.log('PlannedTaskRelationDBService.incomingExternalRelations - gr: ' + gr.getRowCount() + " | " + gr.getEncodedQuery());
          while(gr.next()) {
              relation = this.getRelationObj(gr, 'in');
              incomingRelations.push(relation);
          }
      }
      return incomingRelations;
  },

  outgoingExternalRelations: function (sysId, filterQuery) {
      PPMDebug.log('PlannedTaskRelationDBService.incomingExternalRelations: ' + sysId + " | " + filterQuery);
      var outgoingRelations = [], relation;
      if(JSUtil.notNil(sysId)) {
          var gr = new GlideRecord(this.TABLE);
          gr.addQuery("parent_top_task", sysId);
          gr.addQuery("external", "true").addOrCondition("external", "1");
          if(JSUtil.notNil(filterQuery))
              gr.addEncodedQuery(filterQuery);
          gr.query();
          PPMDebug.log('PlannedTaskRelationDBService.outgoingExternalRelations - gr: ' + gr.getRowCount() + " | " + gr.getEncodedQuery());
          while(gr.next()) {
              relation = this.getRelationObj(gr, 'out');
              outgoingRelations.push(relation);
          }
      }
      return outgoingRelations;
  },

  getRelationObj: function (gr, boundType) {
      if(JSUtil.notNil(gr)) {
          var parentRec = gr.parent.getRefRecord();
          var childRec = gr.child.getRefRecord();
          var parentTopTaskRec = gr.parent_top_task.getRefRecord();
          var childTopTaskRec = gr.child_top_task.getRefRecord();
          var isPendingNotification = false;
          if(boundType == 'in') {
              isPendingNotification = PlannedTaskNotificationService.getNewNotificationsForTask(childRec.getValue('sys_id')).length > 0;
          } else {
              isPendingNotification = PlannedTaskNotificationService.getNewNotificationsForTask(parentRec.getValue('sys_id')).length > 0;
          }

          return {
              bound_type: boundType,
              sys_id: gr.getValue("sys_id"),
              parent: gr.getValue("parent"),
              parent_number: parentRec.getValue("number"),
              parent_short_description: parentRec.getValue("short_description"),
              child: gr.getValue("child"),
              child_number: childRec.getValue("number"),
              child_short_description: childRec.getValue("short_description"),
              parent_top_task: gr.getValue("parent_top_task"),
              parent_top_task_number: parentTopTaskRec.getValue("number"),
              parent_top_task_short_description: parentTopTaskRec.getValue("short_description"),
              child_top_task: gr.getValue("child_top_task"),
              child_top_task_number: childTopTaskRec.getValue("number"),
              child_top_task_short_description: childTopTaskRec.getValue("short_description"),
              type: gr.getDisplayValue("sub_type"),
              isPendingNotification: isPendingNotification
          };
      }
  },

  type: 'PlannedTaskRelationDBService'
};

Sys ID

665d5a179f403200598a5bb0657fcffd

Offical Documentation

Official Docs: