Name

sn_service_builder.ASBCancelCheckout

Description

No description available

Script

var ASBCancelCheckout = Class.create();
ASBCancelCheckout.prototype = {
  initialize: function() {
      this.utils = new sn_service_builder.ASBUtils();
  	this.CONSTANTS = new sn_service_builder.ASBConstants();
  	this.DPM_ACTIVE = GlidePluginManager.isActive('com.snc.dpm');

  	if (this.DPM_ACTIVE)
  		this.DPM_SB_UTILS = new sn_service_builder.ASBUtilsDPM();
  },
  
  /**
   * cancelCheckout
   * Cancel the checkout of a service or offering. Pass table and service id
   * 
   * @param {string} table - the table to discard the draft from 
   * @param {string} sysId - the sys_id of record to discard draft 
   * @returns {boolean} - true if succesful
   */
  cancelCheckout: function(table, sysId) {
  	if (!table || !typeof table === 'string' || !sysId || !typeof sysId === 'string')
  		return false;

  	var supportedTables = [
  		this.CONSTANTS.TABLE.OFFERING,
  		this.CONSTANTS.TABLE.CMDB_CI_SERVICE_BUSINESS,
  		this.CONSTANTS.TABLE.CMDB_CI_SERVICE_TECHNICAL
  	];

  	if (supportedTables.indexOf(table) < 0)
  		return false;

  	var gr = new GlideRecord(table);
  	if (gr.get(sysId)) {

  		//get draft record if record is published
  		if (!gr.getValue(this.CONSTANTS.FIELD.PUBLISHED_REF)) {
  			gr = new GlideRecord(table);
  			gr.get(this.CONSTANTS.FIELD.PUBLISHED_REF, sysId);
  		}
  			
  		if (gr.isValidRecord()) {
  			//Delete the draft record - will cause BR to run that calls cancelDraftService
  			if (!gr.deleteRecord())
  				throw('[ASB]: Could not delete draft record for ' + gr.name);
  			
  			return true;	
  		}
  	}
  	return false;
  },

  /**
   * cancelDraftService
   * Deletes the mtom table references for the service and deletes all of its offerings
   * NOTE: does not delete the service, this code should be run from a delete BR
   * 
   * @param {GlideRecord} draftServiceGr - the draft Service being deleted
   */
  cancelDraftService: function(draftServiceGr) {
  	if (!draftServiceGr.isValidRecord())
  		return false;
  	
  	//remove draft offerings
  	var draftOfferingGr = new GlideRecord(this.CONSTANTS.TABLE.SERVICE_OFFERING);
  	draftOfferingGr.addQuery(this.CONSTANTS.FIELD.PARENT, draftServiceGr.getUniqueValue());
  	draftOfferingGr.query();
  	while (draftOfferingGr.next()) {
  		//Delete the draft Offering - will cause BR to run that calls cancelDraftOffering
  		if (!draftOfferingGr.deleteRecord())
  			gs.error('[ASB]: Could not delete draft record for ' + draftOfferingsGr.name);
  	}

  	var serviceRefTableMappings = {};
  	if (this.DPM_ACTIVE)
  		serviceRefTableMappings = this.DPM_SB_UTILS.getReferenceTableMappings(draftServiceGr.getTableName());

  	serviceRefTableMappings[this.CONSTANTS.TABLE.CMDB_REL_CI] = {
  		field: this.CONSTANTS.FIELD.CHILD,
  		query: this.CONSTANTS.FIELD.TYPE + '=' + this.CONSTANTS.FIELD_VALUE.PROVIDES_BY_PROVIDES
  	};

      //removes service references
  	for (var tableName in serviceRefTableMappings) {
  		if (serviceRefTableMappings.hasOwnProperty(tableName)) {
  			this.utils.deleteReferences(tableName,
  				serviceRefTableMappings[tableName].field,
  				draftServiceGr.getUniqueValue(),
  				(serviceRefTableMappings[tableName].query || null));
  		}
  	}
  	
  	//get published service
  	var publishedServiceGr = new GlideRecord(draftServiceGr.getTableName());
  	if (publishedServiceGr.get(draftServiceGr.getValue(this.CONSTANTS.FIELD.PUBLISHED_REF))) {
  		//set state on published record if it exists
  		this.utils.setCheckoutState(publishedServiceGr, this.CONSTANTS.FIELD_VALUE.FALSE);
  	}
  },

  /**
   * cancelDraftOffering
   * Deletes the mtom table references for the service and deletes all of its offerings
   * NOTE: does not delete the offering, this code should be run from a delete BR
   * 
   * @param {GlideRecord} draftOfferingGr - the draft Offering being deleted
   */
  cancelDraftOffering: function(draftOfferingGr) {
      var subscribeTables = [
  		this.CONSTANTS.TABLE.SERVICE_SUBSCRIBE_COMPANY,
  		this.CONSTANTS.TABLE.SERVICE_SUBSCRIBE_DEPARTMENT,
  		this.CONSTANTS.TABLE.SERVICE_SUBSCRIBE_LOCATION,
  		this.CONSTANTS.TABLE.SERVICE_SUBSCRIBE_SYS_USER_GRP,
  		this.CONSTANTS.TABLE.SERVICE_SUBSCRIBE_SYS_USER,
  		this.CONSTANTS.TABLE.SERVICE_OFFERING_COMMITMENT,
  		this.CONSTANTS.TABLE.SC_CAT_ITEM_SUBSCRIBE_MTOM
  	];

  	var offeringRefTableMappings = {};
  	if (this.DPM_ACTIVE)
  		offeringRefTableMappings = this.DPM_SB_UTILS.getReferenceTableMappings(this.CONSTANTS.TABLE.SERVICE_OFFERING);

  	offeringRefTableMappings[this.CONSTANTS.TABLE.CMDB_REL_CI] = {
  		field: this.CONSTANTS.FIELD.PARENT,
  		query: this.CONSTANTS.FIELD.TYPE + '=' + this.CONSTANTS.FIELD_VALUE.DEPENDS_ON_USED_BY
  	};
  	
      for (var i = 0; i < subscribeTables.length; i++) {
  		this.utils.deleteReferences(subscribeTables[i],
  			this.CONSTANTS.FIELD.SERVICE_OFFERING,
  			draftOfferingGr.getUniqueValue());
  	}

  	for (var tableName in offeringRefTableMappings) {
  		if (offeringRefTableMappings.hasOwnProperty(tableName)) {
  			this.utils.deleteReferences(tableName,
  				offeringRefTableMappings[tableName].field,
  				draftOfferingGr.getUniqueValue(),
  				(offeringRefTableMappings[tableName].query || null));
  		}
  	}

  	// get published offering and set checkout to false 
  	var publishedOfferingGr = new GlideRecord(draftOfferingGr.getTableName());
  	if (publishedOfferingGr.get(draftOfferingGr.getValue(this.CONSTANTS.FIELD.PUBLISHED_REF))) {
  		//set state on published record if it exists
  		this.utils.setCheckoutState(publishedOfferingGr, this.CONSTANTS.FIELD_VALUE.FALSE);
  	}
  },

  type: 'ASBCancelCheckout'
};

Sys ID

42b062b6c7532010a6dff2327ec2604b

Offical Documentation

Official Docs: