CM/Resource Pool - Resource Actions
Introduction
Resource actions are a component of the ConSol CM Action Framework. Resource actions are actions which can be performed for a resource, i.e., an object which is stored in the Resource Pool. The actions can be performed automatically by the system or manually, triggered by an engineer who has the required permissions. You might want to apply resource actions for use cases like the following:
- Create a maintenance ticket for a printer.
- Find all companies which use a certain SLA.
You can use the following types of resource actions:
- Automatic actions which are performed by the system after one of the following resource operations:
- CREATE
- UPDATE
- DELETE
- RELATION
- SEARCH
- Manual actions which are performed by the engineer using Activities links on a resource page in the Web Client (similar to Workflow activities for tickets). Manual actions are executed for the resource which is displayed.
Please keep in mind that only engineers who have at least one role with the following access permissions for the respective resource type are allowed to use the resource actions, i.e., only then will the Activities link be displayed in the Web Client:
- Act
Figure 496: ConSol CM Web Client - Resource action (on resource page)
Resource actions are defined as Groovy scripts which are stored in the Script and Template section of the Admin Tool.
The execution of resource actions can be controlled using condition scripts, i.e., you can implement a condition script which is executed before the action script of the resource action. The action script is only executed if the condition script returns true.
So there are two types of scripts you have to deal with when you use ConSol CM resource actions:
- Resource action script
Defines the action which should be performed. - Resource condition script
Defines one or more conditions for the execution of the action script. Has to return true or false. If false is returned the action script is not executed. If this is a manual action, it is not displayed as Activity in the Web Client.
When you want to implement a resource action you have to proceed in three steps:
- Create a script for the resource action (either an action script only or an action script and a condition script).
- Create the resource action(s) which use(s) the script(s).
- Assign the resource action(s) to the resource type(s) where they should be available.
In the following sections, all three steps are explained in detail.
Creating Resource Actions Using the Admin Tool
Step 1: Write the Resource Action Script
Create a new Admin Tool script of type Resource action. If required, create another script of type Resource condition.
For a detailed explanation of Admin Tool scripts in general, please refer to section Admin Tool Scripts.
Figure 497: ConSol CM Admin Tool - System, Scripts and Templates: Resource action script
// this script creates a new ticket for the resource from which the activity is executed, i.e. creates new ticket and links it to resource
// resource - ticket relation must be configured beforehand!
Ticket newtic = new Ticket()
Queue qu = queueService.getByName("ServiceDesk")
newtic.setQueue(qu)
def subj = resource.get("PC_Desktop_Fields_basic.name")
// newtic.setSubject("New Ticket for Resource: " + resource.getId())
newtic.setSubject("New Ticket for Resource: " + subj)
newtic.set("helpdesk_standard.priority","low")
// use main contact person of the resource as main contact for the ticket
Unit maincont = new Unit()
def crit = new ResourceRelationWithTargetUnitCriteria()
crit.setResource(resource)
List<ResourceRelationWithTargetUnit> cont_list = resourceRelationService.getByCriteria(crit)
if (cont_list.size() == 0) {
client.failure("cmweb.rp.resource.action.no_contact_set")
} else {
def cont_rel = cont_list[0]
maincont = cont_rel.getTargetUnit()
ticketService.createWithUnit(newtic,maincont)
log.info 'New Ticket created for resource with ID' + resource.getId()
// link ticket to resource
def resRelationDefCriteria = new ResourceRelationDefinitionCriteria()
resRelationDefCriteria.addDefinitionName("PC_Desktop_to_Ticket_Relation")
def s_res_type = resource.getResourceType()
resRelationDefCriteria.addSourceResourceType(s_res_type)
resRelationDefCriteria.addTargetQueue(qu)
def resRelationDef = resourceRelationDefinitionService.getByCriteriaUniqueResult(resRelationDefCriteria)
def resRelation = new ResourceTicketRelation(resRelationDef, resource, newtic)
resourceRelationService.create(resRelation)
// go to new ticket
client.goToTicket(newtic)
}
Code example 95: Resource action script for PC_Desktops to create a new Service Desk ticket for responsible PC contact
Step 2: Create Resource Action(s) Which Use(s) the Script
To create, edit, or delete resource actions, open the navigation item Actions in navigation group Resources in the Admin Tool.
To create or add a new action click the Add button and fill-in the required data in the pop-up window (the pop-up window is the same for adding and for editing a resource action).
Figure 498: ConSol CM Admin Tool - Resources, Actions: Creating a resource action
- Name
The technical name of the resource action. You can localize the value by using the Localize button. The localized name will be displayed in the Web Client. For a detailed explanation of the localization mechanism, please refer to section Localization of Objects in General, Type 1. - Type
The resource action type. Select one of the following types. Once a type has been defined, it cannot be modified afterwards (when you edit an existing resource action).- Create
This action will be executed automatically when the resource is created. - Update
This action will be executed automatically when the resource is updated, i.e., when the data has been modified (either manually or automatically) and is saved again. - Delete
This action will be executed automatically when the resource is deleted. Relation
This script will be executed automatically when a relation to or from a resource of this type is- created
- deleted
(The script will not be executed when the comment of a relation is changed.)
- Manual
The resource action is displayed as Activity in the Web Client and can be executed only manually. If a Resource Condition Script is implemented, the activity will only be displayed in the Web Client if the condition script has returned true. - Search
The resource action is a search action. Actions of this type are explained in section Action Framework - Search Actions.
- Create
- Condition Script
In case a condition script should be executed before the action script, the name of the condition script must be entered here. Use the drop-down menu to select from a list with all scripts of type Resource condition which are stored in the Scripts section of the Admin Tool. The action script will only be executed if the condition script returns true. If there is no condition, just leave this field empty. - Action Script
The name of the action script which should be executed. Use the drop-down menu to select from a list with all scripts of type Resource action which are stored in the Scripts section of the Admin Tool. - Description
Enter the description which should be displayed as mouse-over in the Web Client (for manual actions only).
Save the action. Then you can assign it to resource types. Please see following step.
Step 3: Assign Resource Actions to Resource Types
To assign pre-defined Resource Execution and/or resource condition scripts to resource types, the respective manual and/or automatic actions have to be assigned to a resource type. Open the navigation item Data Models in navigation group Resources in the Admin Tool. Select the resource type you would like to edit and click the Edit button to open the pop-up window where you can assign the resource actions. An action might contain only a resource action script, or a resource condition script and a resource action script.
In the following example (next figure), a manual resource action is assigned to the resource type PC_Desktop.
Figure 499: ConSol CM Admin Tool - Resources, Data Models: Assigning manual resource actions to a resource type
You can assign resource actions of the following action types:
- Automatic Actions
Those actions will be executed automatically when the respective type of event (Create, Update, Delete, Relation) has been triggered. Select an action for each type which is required. Only the actions with the correct type (which have been defined as resource actions, see step 2) are listed in each drop-down menu. For example, for an automatic action of type Create, only resource actions of type Create (which have been defined in navigation item Actions) will be available. - Manual Actions
Those actions are displayed as activities on the resource page in the Web Client and have to be executed manually. An activity is only displayed if either no Resource Condition Script is present or if the respective resource condition script has returned true. - Search Actions
See section Action Framework - Search Actions.
Using Resource Actions in the Web Client (as an Engineer)
As an engineer (user), two resource action types are relevant for you because they are available as activities in the Web Client:
- Manual
Manual actions are offered in the Web Client similar to workflow activities for a ticket. Please see Example 1 in the next section. - Search
See section Action Framework - Search Actions.
The CREATE, UPDATE, RELATION, and DELETE actions run in the background.
Programming Resource Execution and Resource Condition Scripts
Please read the section about Scripts for the Action Framework for a general introduction about important principles, classes, and methods for execution and condition scripts.
Examples for Resource Actions
Example 1: Simple Manual Action
Use case: The engineer should be able to create a new Service Desk ticket directly from a resource page of a PC. The new ticket should be related to the resource (PC). The main contact of the new Service Desk ticket should be the person who is responsible for the PC. This is implemented as resource-contact relation in the resource type PC_Desktops. To implement the resource action, perform the following steps.
Write the resource action script:
// this script creates a new ticket for the resource from which the activity is executed, i.e. creates new ticket and links it to resource
// resource - ticket relation must be configured beforehand!
Ticket newtic = new Ticket()
Queue qu = queueService.getByName("ServiceDesk")
newtic.setQueue(qu)
def subj = resource.get("PC_Desktop_Fields_basic.name")
// newtic.setSubject("New Ticket for Resource: " + resource.getId())
newtic.setSubject("New Ticket for Resource: " + subj)
newtic.set("helpdesk_standard.priority","low")
// use main contact person of the resource as main contact for the ticket
Unit maincont = new Unit()
def crit = new ResourceRelationWithTargetUnitCriteria()
crit.setResource(resource)
List<ResourceRelationWithTargetUnit> cont_list = resourceRelationService.getByCriteria(crit)
if (cont_list.size() == 0) {
client.failure("cmweb.rp.resource.action.no_contact_set")
} else {
def cont_rel = cont_list[0]
maincont = cont_rel.getTargetUnit()
ticketService.createWithUnit(newtic,maincont)
log.info 'New Ticket created for resource with ID' + resource.getId()
// link ticket to resource
def resRelationDefCriteria = new ResourceRelationDefinitionCriteria()
resRelationDefCriteria.addDefinitionName("PC_Desktop_to_Ticket_Relation")
def s_res_type = resource.getResourceType()
resRelationDefCriteria.addSourceResourceType(s_res_type)
resRelationDefCriteria.addTargetQueue(qu)
def resRelationDef = resourceRelationDefinitionService.getByCriteriaUniqueResult(resRelationDefCriteria)
def resRelation = new ResourceTicketRelation(resRelationDef, resource, newtic)
resourceRelationService.create(resRelation)
// go to new ticket
client.goToTicket(newtic)
}
Code example 96: Resource action script for PC_Desktops to create a new Service Desk ticket for responsible PC contact
Create a resource action based on the script:
Figure 500: ConSol CM Admin Tool - Resources, Actions: Creating a new resource action
Assign the action to the correct resource type:
Figure 501: ConSol CM Admin Tool - Resources, Data Models: Assign the resource action to the correct resource type
Check the functionality using the Web Client:
Figure 502: ConSol CM Web Client - Resource page with the resource action
Figure 503: ConSol CM Web Client - New Service Desk ticket created by the resource action
Figure 504: ConSol CM Web Client - Resource page with one or more ticket relations (new Service Desk ticket)
Example 2: Open a Maintenance Ticket from a Resource Using an ACF
The Action Framework offers the possibility to open an ACF (Activity Control Form) when a ticket is created. The ACF is used to gather data for the following workflow activity, i.e., the ticket can be created and moved through the first workflow step very easily. The following example demonstrates this functionality. A maintenance ticket should be created for a resource (an HP printer). During the (sub-) process, an ACF should be displayed to ask for data which will then be used during the next workflow activity.
To implement the resource action, perform the following steps.
Write the resource action script:
// this script creates a new ticket for the resource from which the activity is executed, i.e. creates new ticket and links it to resource
// resource - ticket relation must be configured beforehand!
Ticket newtic = new Ticket()
Queue qu = queueService.getByName("SpecialTasks")
newtic.setQueue(qu)
newtic.setSubject("New Ticket for HP Printer: " + resource.getId())
newtic.set("SpecialTasks_Fields.SpecialTasksPrio","normal")
// use main contact person of the resource as main contact for the ticket
Unit maincont = new Unit()
def crit = new ResourceRelationWithTargetUnitCriteria()
crit.setResource(resource)
List<ResourceRelationWithTargetUnit> cont_list = resourceRelationService.getByCriteria(crit)
if (cont_list.size() == 0) {
client.showErrorMessage("Add a relation to a customer first.")
} else {
def cont_rel = cont_list[0]
maincont = cont_rel.getTargetUnit()
ticketService.createWithUnit(newtic,maincont)
log.info 'New Ticket created for resource with ID ' + resource.getId()
// link ticket to resource
def resRelationDefCriteria = new ResourceRelationDefinitionCriteria()
resRelationDefCriteria.addDefinitionName("HP_Printer_ToTicket_Relation")
def s_res_type = resource.getResourceType()
resRelationDefCriteria.addSourceResourceType(s_res_type)
resRelationDefCriteria.addTargetQueue(qu)
def resRelationDef = resourceRelationDefinitionService.getByCriteriaUniqueResult(resRelationDefCriteria)
def resRelation = new ResourceTicketRelation(resRelationDef, resource, newtic)
resourceRelationService.create(resRelation)
// go to new ticket, but fill ACF before
def executionContext = controlFormDefinitionService.getExecutionContext(newtic, "defaultScope/TaskInProgress/Aufgabe_annehmen")
// set ticket number in list in resource:
def newtic_id = newtic.id.toString()
def newtic_name = newtic.name
resource.add("HP_Printer_MaintenanceTickets.MaintenanceTicketsList",new Struct().set("MaintenanceTicketID", newtic_id).set("MaintenanceTicketName", newtic_name)
)
if (!executionContext) {
client.showErrorMessage("action.fail.wrong.activity")
} else {
// if ticktes are in the list in the resource, the field group should be visible
def groupName = "HP_Printer_MaintenanceTickets"
def fieldGroupDefinition = fieldDefinitionService.getGroupByName(groupName)
if (fieldGroupDefinition == null) {
throw new IllegalArgumentException("There is no group definition with name '" + groupName + "'.")
}
resource.getGroupsConfiguration().setProperty(fieldGroupDefinition, GroupPropertyType.VISIBLE, "true")
// Modify entities from the execution context - not the original ones
// - since the user may still press cancel.
executionContext.ticket.add("SpecialTasks_Fields","Deadline", new Date());
client.goToTicket(newtic).openActivityForm(executionContext);
}
}
Code example 97: Resource action script which opens a ticket and uses an ACF
Create a resource action based on the script.
Figure 505: ConSol CM Admin Tool - Resources, Actions: Create the resource action for the HP Printer maintenance ticket
Assign the action to the correct resource type:
Figure 506: ConSol CM Admin Tool - Resources, Data Models: Assigning the resource action to the correct resource type (HP Printer)
Check the functionality using the Web Client:
Figure 507: ConSol CM Web Client - Resource action for HP Printer
Figure 508: ConSol CM Web Client - New maintenance ticket for resource (HP Printer), ACF
Working with the Changes Object in Resource Update Actions
Starting with CM version 6.10.5.4, it is possible to monitor the changes which have been performed during a resource update action. (The same applies to unit Update actions, explained in section Working with the Changes Object in Customer Update Actions).
To find out which changes have been performed use the object of class ResourceChanges in resource actions.
Please remember, the Update script will be executed:
- in an explicit Update action
- when comments or attachments are added
- when comments or attachments are removed
There are two methods of the ResourceChanges object which provide information about the changed data:
- getCustomFieldChangeInfo()
provides information about changes of resource data (in resource fields) - getContentChangeInfo()
provides information about changes in the resource history (comments, attachments)
Since the method return parameters contain rather complex components, we recommend to read the API doc of the ResourceChanges class. The following code provides an example for a script where a resourceChanges object is used.
/**
* Available script variables:
* Manual action:
* resource - Resource for which action is executed
*
* Create, Delete Action:
* resource - Resource for which action is executed
*
* Update Action:
* resource - Resource for which action is executed
* changes - ResourceChanges object containing information about changes done for resource entity
*
* Relation action:
* resource - Resource for which action is executed (resource-resource)
* relation - Relation object
* resourceExternalId - External resource id (resource, unit, ticket - ext resource)
*/
// Update Action Script displayPC_DesktpChangesInLog.groovy for resources in PC_Desktops
import com.consol.cmas.common.model.content.unit.UnitCommentEntry
import com.consol.cmas.common.model.content.unit.UnitAttachmentEntry
log.info 'Resource (PC_Desktop) data have been UPDATEd!'
// Are there any changes?
if (changes) {
log.info 'Yes, changes have been made to unit'
log.info 'Changes object is a ' + changes.class
}
// Have Custom Fields been changed? If yes - which?
if (changes.customFieldChangeInfo) {
log.info 'Yes, changes have been made to Custom Fields (Resource Fields)'
log.info changes.customFieldChangeInfo
log.info changes.customFieldChangeInfo.each { k, v ->
log.info "Changed field: ${k.groupName}/ ${k.fieldName}"
log.info "New value: ${v.value.value}"
log.info "Old value: ${v.previousValue.value}"
}
} else {
log.info 'No changes to Custom Fields'
}
// Have comments or attachmenst been changed? If yes - which?
log.info changes.contentChangeInfo
if (changes.contentChangeInfo){
log.info 'Yes, changes have been made in detail section'
if (changes.contentChangeInfo.value){
log.info changes?.contentChangeInfo.each { ctEntry ->
if (ctEntry?.value[0] instanceof UnitCommentEntry){
log.info 'A comment has been added.'
log.info 'Old value: ' + ctEntry?.previousValue
log.info 'New value: ' + ctEntry.value[0]?.text
log.info 'Made by the engineer ' + ctEntry.value[0]?.engineer?.name
log.info 'Creation date of the comment: ' + ctEntry.value[0]?.creationDate
} else if (ctEntry?.value[0] instanceof UnitAttachmentEntry){
log.info 'An attachment has been added.'
log.info 'Old value: ' + ctEntry?.previousValue
log.info 'New value text: ' + ctEntry.value[0]?.text
log.info 'New value file name: ' + ctEntry.value[0]?.filename
}
}
} else {
log.info 'Entry has been deleted.'
}
}
Code example 98: Resource Update script where changes are monitored and printed out to server.log
When for a resource of type PC_Desktops, the content of the two resource fields modell and name are modified, the following text is printed into the server.log file.
PC_DesktpChangesInLog.groovy] [Susan-] Resource (PDC_Desktop) data have been UPDATEd!
PC_DesktpChangesInLog.groovy] [Susan-] Yes, changes have been made to unit
PC_DesktpChangesInLog.groovy] [Susan-] Changes object is a class com.consol.cmas.common.model.resource.history.ResourceChanges
PC_DesktpChangesInLog.groovy] [Susan-] Yes, changes have been made to Custom Fields (Resource Fields)
PC_DesktpChangesInLog.groovy] [Susan-] {(modell,PC_Desktop_Fields_basic)=Modification{value=AbstractField{key=(modell,PC_Desktop_Fields_basic), value=Computer-0815-01}, previousValue=AbstractField{key=(modell,PC_Desktop_Fields_basic), value=Computer-0815}}, (name,PC_Desktop_Fields_basic)=Modification{value=AbstractField{key=(name,PC_Desktop_Fields_basic), value=My cool PC11}, previousValue=AbstractField{key=(name,PC_Desktop_Fields_basic), value=My cool PC}}}
PC_DesktpChangesInLog.groovy] [Susan-] Changed field: PC_Desktop_Fields_basic/ modell
PC_DesktpChangesInLog.groovy] [Susan-] New value: Computer-0815-01
PC_DesktpChangesInLog.groovy] [Susan-] Old value: Computer-0815
PC_DesktpChangesInLog.groovy] [Susan-] Changed field: PC_Desktop_Fields_basic/ name
PC_DesktpChangesInLog.groovy] [Susan-] New value: My cool PC11
PC_DesktpChangesInLog.groovy] [Susan-] Old value: My cool PC