Name

global.CaseRESTAPIValidatorForExternalUserSNC

Description

No description available

Script

var CaseRESTAPIValidatorForExternalUserSNC = Class.create();
CaseRESTAPIValidatorForExternalUserSNC.prototype = {
  initialize: function() {
  },

  isExternalUser: function() {
      return !gs.hasRole('snc_internal');
  },

  prepareGlideRecord: function(params, sysId) {
      var gr = new GlideRecord(global.CSMBaseConstants.CASE_TABLE);
      var fields = [];
      if (sysId) {
          gr.get(sysId);
  		if(!gr.canWrite())
  			return gr;
          fields = this.UPDATE_ALLOWED_FIELDS;
      } else {
  		if(gr.canCreate())
  			gr.initialize();
  		else return null;
          fields = this.CREATE_ALLOWED_FIELDS;
      }
  	var allowedFields = this.getMapFromArray(fields);
  	var skipAclFields = this.getMapFromArray(this.SKIP_ACL_FIELDS);
      for (var attr in params) {
  		if (gr.isValidField(attr) && allowedFields[attr + ''] && (gr[attr].canWrite() || skipAclFields[attr + ''])){
            this.validateField(gr,attr,params[attr]);
          }
      }
      return gr;
  },
  
  validateField: function(record, field, value){
  	if (field == "comments")
         record.comments = value;
      else if (field == "state")
         this.validateStateChange(record, record.getValue('state'), value);
      else
         record.setValue(field, value);
  },
  
  getMapFromArray: function(fields){
  	var fieldsMap = {};
      if (fields) {
          for (var f in fields) {
              fieldsMap[fields[f].trim()] = 1;
          }
      }
  	return fieldsMap;
  },
  
  validateStateChange: function(gr, prevState, curState) {
      if (prevState == null) //create case (begining of state flow)
          gr.setValue('state', '1');
      else if (prevState == 3 || prevState == 7 || prevState == curState) //closed/cancelled case  (end of state flow) or no change.
          return;
      else if (curState == 3 || ((prevState == 6 || prevState == 18) && curState == 10)) //external user trying to close the case or open from resolved/ awaiting info state
          this.executeStateFlow(gr, prevState, curState);
      return;
  },

  executeStateFlow: function(gr, prevState, curState) {
      var stateFlowId;
      if ((prevState == 6 && (curState == 3 || curState == 10)) || (prevState == 18 && curState == 10)) // Accept or reject solution or Awaiting Info to open.
          stateFlowId = this.getStateFlowForCase(prevState, curState);
      else if ((prevState == 1 || prevState == 10 || prevState == 18) && curState == 3){ //close a case
  		gr.resolution_code = '4'; //Value of 'Solved by Customer' choice in resolution code choices
          gr.close_notes = gs.getMessage("Closed by customer.");
  		stateFlowId = this.getStateFlowForCase(null, curState);
  	}
      if (stateFlowId)
         new global.StateFlow().processFlow(gr, stateFlowId, 'manual');
  },

  getStateFlowForCase: function(prevState, curState) {
      var gr = new GlideRecord('sn_customerservice_sf_case');
      gr.addQuery('table', 'sn_customerservice_case'); //might not be required
      if (prevState)
          gr.addQuery('start_text', prevState);
      else
          gr.addNullQuery('start_text');
      if (curState)
          gr.addQuery('end_text', curState);
      else
          gr.addNullQuery('end_text');
      gr.query();
      if (gr.next())
          return gr.getUniqueValue();
  },

  type: 'CaseRESTAPIValidatorForExternalUserSNC'
};

Sys ID

9b46a81853905010dc25ddeeff7b128a

Offical Documentation

Official Docs: