Page tree
Skip to end of metadata
Go to start of metadata

Business description

To facilitate the cost approval process, our company decided to auto-fill data, which are used in the process.

Solution Brief

In that company, each user is related to exactly one department and one cost center. Based on an issue author, we can specify his HR number, department or related cost center, which will be useful in the next steps. We can add another post-function, which can indicate them.

Example Decision Table

Script

Note

In this script, we use some superclasses, which help us in code management. For more information, see the Usage of base groovy classes and utilities page.

import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import global.fields.CustomFieldCode
import global.fields.FieldUtils
import global.fields.SystemFieldId

@WithPlugin([
		"eu.rivetgroup.atlas.rvg-jira-app-base-plugin",
		"eu.rivetgroup.atlas.rvg-jira-decision-tables-plugin"
])
class TestSetEmployeeDataPostFunction extends BaseDecisionTableWorkflowFunction {

	TestSetEmployeeDataPostFunction(Map<String, Object> transientVars, boolean validatorMode) {
		super(transientVars, validatorMode, false)
	}

	@Override
	protected boolean executeInternal() {
		def reporterField = issueFieldOperator.forField(FieldUtils.getSystemField(SystemFieldId.REPORTER))
		def employeeHRNumberField = issueFieldOperator.forField(FieldUtils.getCustomField(CustomFieldCode.EMPLOYEE_HR_NUMBER))
		def departmentField = issueFieldOperator.forField(FieldUtils.getCustomField(CustomFieldCode.DEPARTMENT_CF))
		def costCentreField = issueFieldOperator.forField(FieldUtils.getCustomField(CustomFieldCode.COST_CENTRE))

		issueFieldOperator.checkRequired(reporterField);

		def dtResult = decisionTableManager.getDMNDecisionTableOperations(
				"employees", 
				"employeeData"
		).executeQuery([
				"user": reporterField.getBusinessKey().first()
		])
		issueFieldOperator.getConfig().setCurrentExecutionInfo(dtResult.executionContextInfo)

		Map<String, Object> dtResEntry = dtResult.singleNonEmpty()
		employeeHRNumberField.setObjectValue(dtResEntry.get("employeeHRNumber"))
		departmentField.setValueByBusinessKey(dtResEntry.get("department"))
		costCentreField.setValueByBusinessKey(dtResEntry.get("costCentre"))

		return true;
	}
}

Explanation

@WithPlugin([
	"eu.rivetgroup.atlas.rvg-jira-app-base-plugin",
	"eu.rivetgroup.atlas.rvg-jira-decision-tables-plugin"
])
class TestSetEmployeeDataPostFunction extends BaseDecisionTableWorkflowFunction{

TestSetEmployeeDataPostFunction(Map<String, Object> transientVars, boolean validatorMode) {
	super(transientVars, validatorMode, false)
}

In that case, we also start with point-dependent imports and define a constructor.

def reporterField = issueFieldOperator.forField(FieldUtils.getSystemField(SystemFieldId.REPORTER))
def employeeHRNumberField = issueFieldOperator.forField(FieldUtils.getCustomField(CustomFieldCode.EMPLOYEE_HR_NUMBER))
def departmentField = issueFieldOperator.forField(FieldUtils.getCustomField(CustomFieldCode.DEPARTMENT_CF))
def costCentreField = issueFieldOperator.forField(FieldUtils.getCustomField(CustomFieldCode.COST_CENTRE))
issueFieldOperator.checkRequired(reporterField);

References to related fields and addition of the validation, if necessary fields have values. Without them, operations could be impossible.

def dtResult = decisionTableManager.getDMNDecisionTableOperations(
	"employees",
	"employeeData"
).executeQuery([
	"user": reporterField.getBusinessKey().first()
])
issueFieldOperator.getConfig().setCurrentExecutionInfo(dtResult.executionContextInfo)

Execution of one parameter query on the decision table. To get the Decision Table Operation instance we use "employees" as the name of the Decision Table file (with .dmn extension) and "employeeData" as an id of decision table element inside the DMN model, because DMN can contain multiple decision tables. Next, we set the context related to the executed decision table. Thanks to that we have info about the decision table name or query, which helps to handle any errors.

Map<String, Object> dtResEntry = dtResult.singleNonEmpty()
employeeHRNumberField.setObjectValue(dtResEntry.get("employeeHRNumber"))
departmentField.setValueByBusinessKey(dtResEntry.get("department"))
costCentreField.setValueByBusinessKey(dtResEntry.get("costCentre"))

At the finish, we get an entry as a map with output parameters from the result and use obtained values to fill related fields in the issue.

Attachments

TestSetEmployeeDataPostFunction.groovyemployees.dmn

  • No labels