Name

sn_cd.cd_CloneContentAJAX

Description

No description available

Script

var cd_CloneContentAJAX = Class.create();
cd_CloneContentAJAX.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
  canCloneforCG: function() {
      var parser = new global.JSON();
      var result = false;
      if (new GlidePluginManager().isActive('sn_cg')) {
          var contentRequestItemSysId = this.getParameter('sysparm_content_request_item');
          var grContentVisibility = new GlideRecord(cd_CommonConstants.CONTENT_TABLE_CONTENT_VISIBILITY);
          grContentVisibility.addQuery('content_request_item', contentRequestItemSysId);
          grContentVisibility.setLimit(1);
          grContentVisibility.query();
          result = !grContentVisibility.hasNext();
      }
      return parser.encode({
          canCloneforCG: result
      });
  },

  getClonedContent: function() {
      var parser = new global.JSON();
      var contentSysId = this.getParameter('sysparm_content_sys_id');
      var grContentBase = new GlideRecord(cd_CommonConstants.CONTENT_TABLE_BASE);
      var cloneTitle;
      var cloneSysId;
      if (grContentBase.get(contentSysId)) {
          var tableName = grContentBase.getValue("sys_class_name");
          // since the fb and pulse content tables belong to a different app
          // we cannot create the records from this script include which belongs to CD scope
          // hence we are cloning in script includes that belong to their apps
          if (tableName === cd_CommonConstants.CONTENT_TABLE_FACEBOOK)
              return new sn_fb_wp_campaigns.fb_CloneContent().getClonedContent(contentSysId);
          else if (tableName === cd_CommonConstants.CONTENT_TABLE_PULSE)
              return new sn_lp.lp_CloneContent().getClonedContent(contentSysId);

          // for content tables which belong in CD scope we clone in this script include
          var grContentClass = new GlideRecord(tableName);
          if (grContentClass.get(contentSysId)) {
              var grContentClone = this._cloneGlideRecord(grContentClass);
              grContentClone.title = this._cloneTitle(grContentClone);
              grContentClone.insert();
              cloneTitle = grContentClone.getValue('title');
              cloneSysId = grContentClone.getUniqueValue();
          }
      }
      return parser.encode({
          title: cloneTitle,
          sys_id: cloneSysId
      });
  },

  /**
   * Return new GlideRecord with non 'sys' properties cloned
   * @param gr GlideRecord
   * @return GlideRecord
   **/
  _cloneGlideRecord: function(gr) {
      var grClone = new GlideRecord(gr.getRecordClassName());
      grClone.newRecord();
      for (var prop in gr) {
          if (prop.match(/^sys_/) == null) {
              grClone[prop] = gr[prop];
          }
      }

      return grClone;
  },


  /**
   * Return cloned String title
   * @param title String
   * @return String Example: New Hire Campaign -> New Hire Campaign(1)
   **/
  _cloneTitle: function(gr) {
      var match = /(.*)\((\d+)\)$/g.exec(gr.title);
      var prefix = match && match[1] ? match[1] : gr.title;

      var grClass = new GlideRecord(gr.getValue("sys_class_name"));
      grClass.addQuery('title', 'STARTSWITH', prefix);
      grClass.query();

      var max = 1;
      while (grClass.next()) {
          match = /\((\d+)\)$/g.exec(grClass.title);

          if (match) {
              var num = parseInt(match[1]);
              if (num >= max)
                  max = num + 1;
          }
      }

      return prefix.trim() + ' (' + max + ')';
  },
  type: 'cd_CloneContentAJAX'
});

Sys ID

2cb6291e58811110fa9b3a5b2cf564c7

Offical Documentation

Official Docs: