Name

sn_hr_core.hr_ApprovalUtilsAjax

Description

HR case approval client callable methods for workspace.

Script

var hr_ApprovalUtilsAjax = Class.create();
hr_ApprovalUtilsAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
  
  /*
  * Expects GlideAjax call to have 'sysparam_sys_id' set to HR case sys_id
  */
  initialize: function(request, responseXML, gc) {
  	global.AbstractAjaxProcessor.prototype.initialize.call(this, request, responseXML, gc);
  	
  	this.caseID = this.getParameter('sysparm_sys_id');
  	this.isValidUser = new sn_hr_core.hr_CaseAjax().isAssignedToCaseWriter(gs.getUserID(), this.caseID);
  },
  
  /*
  * Returns object containing missing approvers set on approval type of HR service activity
  *  and boolean value true if at-least one approver/ approval is set for HR case
  *
  * @param (extracted from caller)
  *  caseID: HR case sys_id string value (extracted in initialize function)
  * 
  * @returns
  *  object {
  *    missingApprovers: array list of missing approvers defined on approval HR service activity,
  *    approverSet: boolean true if at-least one approver is set false otherwise
  *  }
  *  OR error message as
  *  object {
  *    error: string error message if invalid user or missing caseID
  *  }
  */
  getSubstituteApprovers: function() {
  	var response = {};
  	
  	if (this.isValidUser && this.caseID) {
  		response = {
  			missingApprovers: hr_ApprovalUtil.getMissingApproversOnCase(this.caseID),
  			approverSet: hr_ApprovalUtil.approverSetForCase(this.caseID)
  		};
  	} else {
  		response = {
  			error: this.isValidUser ? gs.getMessage('Invalid user. Access denied.') :  '' +
  				this.caseID ?  gs.getMessage(' Verify HR case sys_id is set.') : ''
  		};
  	}
  	return JSON.stringify(response);
  },
  
  /*
  * Adds approvers to the case and updates workflow context
  *
  * @param (extracted from caller)
  *  HR case ID: HR case sys_id string value (extranced in initialize function)
  *  table name: string table name
  *  approvers: comma separated string value containing sys_user ids
  * 
  * @returns error message as
  *  object {
  *    error: string error message if invalid user or missing caseID
  *  }
  *  OR null for success
  */
  addApproversToCase: function() {
  	var response = {};
  	
  	if (this.isValidUser) {
  		//add approvers to case and update case workflow context
  		response = hr_ApprovalUtil.addApproversToCase(this.getParameter('sysparm_approvers'), this.getParameter('sysparm_table'), this.caseID);
  	} else {
  		response = {
  			error: gs.getMessage('Invalid user. Access denied')
  		};
  	}
  	
  	return JSON.stringify(response);
  },
  
  /*
  *  Re-requests approvals for an eligible case where approvals were previously rejected
  *
  * @param (extracted from caller)
  *  caseID: HR case sys_id string value (extracted in initialize function)
  *  journalType: string type of journal field to record reason (comment or work_note)
  *  reason: string explanation for re-requesting approval (gets saved in the specified journal field)
  * 
  * @returns error message as
  *  object {
  *    error: string error message if invalid user or missing caseID
  *  }
  *  OR null for success
  */
  resubmitApprovals: function() {
  	var response = {};
  	var grCase = new GlideRecord('sn_hr_core_case');
  	
  	if (this.isValidUser && grCase.get(this.caseID)) {
  		hr_ApprovalUtil.rerequestCaseApproval(grCase);
  		
  		var journalType = this.getParameter('sysparm_journalType');
  		var reason = gs.getMessage('Resubmit approval reason: {0}', this.getParameter('sysparm_reason'));
  		
  		if ('work_notes' == journalType)
  			grCase.work_notes = reason;
  		else if ('additional_comments' == journalType)
  			grCase.comments = reason;
  		
  		grCase.work_notes = gs.getMessage('The approval request has been resubmitted by the agent');
  		grCase.update();
  		
  	} else {
  		response = {
  			error: gs.getMessage('Invalid user. Access denied')
  		};
  	}
  	
  	return JSON.stringify(response);
  },
  /*
  * Check if adhoc approvers can be allowed
  *
  * @param (extracted from caller)
  *  HR case ID: HR case sys_id string value (extranced in initialize function)
  *  table name: string table name
  *  
  * 
  * @returns error message as
  *  object {
  *    error: string error message if invalid user or missing caseID
  *  }
  *  OR true for success false for failure
  */
  isAdhocApprovers: function() {
  	var response = {};
  	var tableName = this.getParameter('sysparm_parent_table');
  	var grCase = new GlideRecord(tableName);
  	if (grCase.get(this.caseID)) {

  		var approvalContext = hr_ApprovalUtil.getApprovalSubflowsForCase(grCase);
  		response = (approvalContext == null) && (grCase.state == '10'  || grCase.state == '18') ;

  	} else {
  		response = {
  			error: gs.getMessage('Invalid case')
  		};
  	}

  	return JSON.stringify(response);
  },
  
  /*
  * To add adhoc approvers in case
  *
  * @param 
  *  data: data contain below parameters
  		caseId: case sys_id,
  		tableName: case table name,
  		approvers: comma seperated sys_id(s) of approver users,
  		waitForOption : wait for option ['anyone','everyone','first_response'],
  		rejectOption : reject option ['resubmit','close_case'],
  		noteType: Type of comments ['comment' for additional comments otherwise worknote],
  		note: Notes to be added in activity stream
  *
  * @returns error message as
  *  object {
  *    error: string error message if missing caseID or if approver can't be added
  *  }
  *
  */
  submitAdhocApproval: function(data) {
  	
  	if (!data.tableName) {
  		var error = gs.getMessage("Table name is blank");
  		return JSON.stringify({
  			error: error
  		});
  	}
  	
  	var caseGr = new GlideRecord(data.tableName);
  	if (!caseGr.get(data.caseId) || !hr_ApprovalUtil.isAdhocApprovers(caseGr)) {
  		var error = gs.getMessage("Adhoc approver request can not be submitted");
  		return JSON.stringify({
  			error: error
  		});
  	}
  	var vars = {};
  	vars.adhoc_approvers = data.approvers;
  	vars.wait_for = data.waitForOption;
  	vars.on_rejection = data.rejectOption;
  	
  	hr_ApprovalUtil.startAdhocApprovalFlow(caseGr, vars);
  	
  	if (data.note) {
  		if(data.noteType == 'comment')
  			caseGr.comments = data.note;
  		else
  			caseGr.work_notes = data.note;
  	}
  	caseGr.update();
  },
  
  

  type: 'hr_ApprovalUtilsAjax'
});

Sys ID

c9222e7353633300535ee856a11c086a

Offical Documentation

Official Docs: