Using data fields in scripts

Accessing data fields

The basic methods for accessing data fields are the same for cases, contacts and resources.

Cases

You need four methods of the class Ticket to work with case fields:

Contacts

You need four methods of the class Unit to work with contact fields:

Alternatively, you can separate the field group and field name by a colon.

When working with contact data, you also need access to the different contact objects.

Resources

You need four methods of the class Resource to work with resource fields:

The content of a data field depends on the data type, it can be either a value or an object. See section Working with different data types for details about handling the different data types.

Working with different data types

The following section shows examples of working with data fields of different data types. The examples refer to case fields. Nevertheless, the handling is the same for contact and resource fields.

Simple data types

Simple data types are data fields which contain a single value or object. The following list shows the available simple data types with the classes of their values:

Retrieving and setting simple field values

The following example shows how to retrieve a boolean value of a case field to use it in a decision script:

boolean vip_info = ticket.get("am_fields.vip")

 

if(vip_info == true){

return true

}

else {

return false

}

Code example 24: Decision script where a boolean value is checked

The same behavior can be achieved by directly returning the value:

return ticket.get("am_fields.vip")

Code example 25: Decision script where a boolean value is checked, short version

The following example shows how to set a value in a case field. As the field has the data type date, it requires a Date object, which can be created directly in the statement.

ticket.set("fields.reaction_time", new Date());

Code example 26: Setting a case field value for a field of type DATE

When you work with number or date fields, you can even calculate with the field values in a very comfortable way, see following example.

ticket.add("fields.orders_number", 1)

Code example 27: Adding one to the number of orders

Searching by field values

You can search for cases, contacts and resourced by values in their data fields using getByCriteria methods in the respective services (ticketService, unitService and resourceService).

def tickets = ticketService.getByCriteria("HelpDesk_1st_Level", [

"helpdesk_standard:priority": "high"

])

Code example 28: Search for all cases from the Helpdesk 1st Level queue with the priority “high”

ticketService.getByCriteria("HelpDesk_1st_Level", [

"helpdesk_standard:priority": "high"

], { ticketCriteria ->

ticketCriteria.setStatus(TicketCriteria.Status.OPEN)

})

Code example 29: Search for all open cases from the Helpdesk 1st Level queue with the priority “high”

Retrieving and setting enum values

An enum field is a field where the value is one of the predefined list values, for example, a list with priorities. To retrieve the value of an enum field, you can use the same syntax as for simple data types. The get method returns an object of the class EnumValue. Use the getName() method, or .name in Groovy, to retrieve the technical name of the value.

def prio = ticket.get("helpdesk_standard.priority")

log.info 'Priority is now ' + prio.getName()

Code example 30: Retrieving an enum value for a case field

If you want to retrieve the localized value of an enum, you have to use LocalizationService.

def my_enum_field = ticket.get("helpdesk_standard.priority")

def my_enum_field_localized = localizationService.getLocalizedProperty(EnumValue.class, "name", my_enum_field.id, engineerService.getCurrentLocale())

Code example 31: Retrieving a localized enum value

When setting an enum value, the second parameter must be the technical name of an existing enum value of the enum referenced by the case field.

ticket.set("helpdesk_standard.priority", "URGENT");

Code example 32: Setting an enum value

Retrieving hierarchical list values and paths

Hierarchical lists are handled like regular enums when it comes to retrieving and setting values.

The following examples show the basic methods:

Lists and tables

Lists and tables are containers for other data fields. A data field of the type list contains either one field of a simple data type or a field of the type struct which consists of several fields of simple data types. A list field can contain several values. There is one row for each value.

Lists

Each row of a list contains a value of a simple data type. In the following example, this is a case field of type date, which has the parameter Belongs to pointing to the list.

Tables (lists of structs)

The data construct list of structs is the technical basis for a multi-column table structure. The list is the parent object which contains rows. Each row is an instance of a struct. The struct contains as many data fields (table columns) as required.

To retrieve the data from a list of structs you can work with an iteration over the rows. In the following example, we work with a table where ...