Name

sn_customerservice.EscalationUtils

Description

No description available

Script

var EscalationUtils = Class.create();
EscalationUtils.prototype = {
  initialize: function() {
  },
  
  canRequestEscalation: function(gr) {
  			
  	var gr_esc = new GlideRecord('sn_customerservice_escalation');
  	gr_esc.addQuery('source_table', gr.sys_class_name);
  	gr_esc.addQuery('source_record', gr.sys_id);
  	gr_esc.addQuery('active', true);
  	gr_esc.query();
  	if(!gr_esc.next()) 
  		return true;
  	
  	return false;
  	
  },
  
  canRequestDeescalation: function(gr) {
  	var gr_esc = new GlideRecord('sn_customerservice_escalation');
  	gr_esc.addQuery('source_table', gr.sys_class_name);
  	gr_esc.addQuery('source_record', gr.sys_id);
  	gr_esc.addQuery('state', 101);    // escalated
  	gr_esc.addQuery('active', true);
  	gr_esc.query();
  	if (gr_esc.next()) 
  		return true;
  	
  	return false;
  },
  
  appendNote: function(escalation, fieldName, newValue, oldValue) {
  			
  	var gr_source = new GlideRecord(escalation.getValue('source_table'));
  	gr_source.get(escalation.getValue('source_record'));
  	if(!gr_source) return;
  	
  	if(fieldName == "state") {
  		
  		if(escalation.state == 100) { // Requested	
  			
  			if(gr_source.isValidField('work_notes')){
  				gr_source.work_notes = gs.getMessage('{0} has requested to escalate this {1}. {2} created.', [gs.getUserDisplayName(), gr_source.getClassDisplayValue(), escalation.getDisplayValue('number')]);
  				gr_source.update();
  			}
  			
  			// create depend on type
  			if(escalation.getValue('source_table') == 'sn_customerservice_case' || escalation.getValue('source_table') == 'csm_order_case') {
  				escalation.work_notes = gs.getMessage('Case: {0}\nCase Short Description: {1}\nAccount: {2}\nContact: {3}\nEscalation Phase: {4}', [gr_source.getDisplayValue('number'), gr_source.getDisplayValue('short_description'), gr_source.getDisplayValue('account'), gr_source.getDisplayValue('contact'), escalation.getDisplayValue('escalation_severity')]);
  				escalation.update();
  			}
  			
  		} else if(escalation.state == 101) { // Escalated
  			
  			if(!oldValue) {
  				// skip approval
  				if(escalation.getValue('source_table') == 'sn_customerservice_case' || escalation.getValue('source_table') == 'csm_order_case') {
  				escalation.work_notes = gs.getMessage('Case: {0}\nCase Short Description: {1}\nAccount: {2}\nContact: {3}\nEscalation Phase: {4}', [gr_source.getDisplayValue('number'), gr_source.getDisplayValue('short_description'), gr_source.getDisplayValue('account'), gr_source.getDisplayValue('contact'), escalation.getDisplayValue('escalation_severity')]);
  				escalation.update();
  				}
  				if(gr_source.isValidField('work_notes')){
  					gr_source.work_notes = gs.getMessage("Escalation {0} has been approved.", [escalation.getDisplayValue('number')]);
  				}
  			} else {
  				if(gr_source.isValidField('work_notes')){
  					gr_source.work_notes = gs.getMessage("{0} has approved escalation {1}.", [escalation.getDisplayValue('approved_by'), escalation.getDisplayValue('number')]);
  				}
  				
  			}

  			gr_source.update();
  			
  		} else if(escalation.state == 102) { // Declined
  			
  			if(gr_source.isValidField('work_notes')){
  				gr_source.work_notes = gs.getMessage("{0} has declined escalation {1}.", [escalation.getDisplayValue('declined_by'), escalation.getDisplayValue('number')]);
  				gr_source.update();
  			}
  			
  			var declineComments = this.getComments(escalation);
  				
  				if(declineComments != "") {
  					escalation.work_notes =  gs.getMessage("{0}", [declineComments]);
  					escalation.update();
  				}
  			
  			
  		} else if(escalation.state == 103) { // Closed Escalated
  			if(gr_source.isValidField('work_notes')){
  				gr_source.work_notes = gs.getMessage("Escalation {0} has been closed.", [escalation.getDisplayValue('number')]);
  				gr_source.update();
  			}
  			
  		} else if(escalation.state == 104) { // De-escalation Requested
  			// TBD
  		}
  		
  	} else if(fieldName == "escalation_severity") {
  		
  		if(gr_source.isValidField('work_notes')) {
  			gr_source.work_notes = gs.getMessage("Escalation {0} phase has changed: {1} was {2}.", [escalation.getDisplayValue('number'), newValue, oldValue]);
  			gr_source.update();
  		}
  		
  	} else if(fieldName == "trend") {
  		
  		if(gr_source.isValidField('work_notes')) {
  		gr_source.work_notes = gs.getMessage("Escalation {0} trend has changed: {1} was {2}.", [escalation.getDisplayValue('number'), newValue, oldValue]);
  		gr_source.update();
  		}
  		
  	} else if(fieldName == "work_notes" || fieldName == "comments") {
  		
  		if(gr_source.isValidField('work_notes')) {
  			gr_source.work_notes = gs.getMessage("Escalation {0} has updated:\n{1}", [escalation.getDisplayValue('number'), newValue]);
  			gr_source.update();
  		}
  		
  	}
  
  },
  
  notifyUsers: function(escalation, fieldName) {
  	
  	if(fieldName == 'state') {	
  		if(escalation.state == 100) { // Requested 
  			gs.eventQueue('sn_customerservice.escalation.request', current, gs.getUserID(), escalation.sys_id);
  		} else if(escalation.state == 101) { // Escalated
  			gs.eventQueue('sn_customerservice.escalation.escalated', current, gs.getUserID(), escalation.sys_id);
  		} else if(escalation.state == 102) { // Declined
  			gs.eventQueue('sn_customerservice.escalation.declined', current, gs.getUserID(), escalation.sys_id);
  		} else if(escalation.state == 103) { // Closed 
  			gs.eventQueue('sn_customerservice.escalation.closed', current, gs.getUserID(), escalation.sys_id);
  		} else if(escalation.state == 104) { // De-escalation Requested
  			// TBD
  		}
  	} else if (fieldName == 'escalation_severity') {
  		gs.eventQueue('sn_customerservice.escalation.serchange', current, gs.getUserID(), escalation.sys_id);
  	} else if (fieldName == 'trend') {
  		gs.eventQueue('sn_customerservice.escalation.trdchange', current, gs.getUserID(), escalation.sys_id);
  	} else if (fieldName == 'assignment_group') {
  		gs.eventQueue('sn_customerservice.escalation.gpchange', current, gs.getUserID(), escalation.sys_id);
  	} else if (fieldName == 'assigned_to') {
  		gs.eventQueue('sn_customerservice.escalation.aschange', current, gs.getUserID(), escalation.sys_id);
  	} else if (fieldName == 'comments') {
  		gs.eventQueue('sn_customerservice.escalation.cmtchange', current, gs.getUserID(), escalation.sys_id);
  	}
  },
  
  getComments: function(gr_escalation) {
  	var gr = new GlideRecord('sysapproval_approver');
  	gr.addQuery('document_id', gr_escalation.sys_id);
  	gr.query();
  	while(gr.next()) {
  		var approval_state = gr.getValue('state');
  		if(approval_state == 'approved' || approval_state == 'rejected') {
  			
  			return gr.comments.getJournalEntry(1);
  		}
  	}
  	return "";
  },
  
  getQualifier: function(source_table) {
  	var query = null;
  	if(source_table == 'sn_customerservice_case' || source_table == 'csm_order_case') { // case escalation
  		query = "type=0";
  	} else if (source_table == 'customer_account') { // account escalation
  		query = "type=1";
  	}
  	return query;
  },
  
  type: 'EscalationUtils'
};

Sys ID

e1f5c542b3500300ff6e6e5f26a8dcf3

Offical Documentation

Official Docs: