Count Ticket reopening
- learning:
- Count transitions occurence
- level:
- Beginner
- domains:
- XML, PHP, Automation, Dictionary, Lifecycle
- methods:
- GetAttributeDef, IsValidAttCode
- min version:
- 2.1.0
Goal
In this usecase, we want to count how many times a User Request
was re-open.
For this will:
-
add a counter field to UserRequest,
-
desactivate history tracking (optional)
-
modify UserRequest lifecycle to call the method during the transition from
resolved
toassigned
-
modify the presentation to display the counter
-
add a label for the counter field
with the Designer
Prerequisite: You must be a Combodo's customer
Add a field
-
Go to class UserRequest, tab Schema to create a new field
-
Enter reopen_counter as field Code (lower letter only, no blank, dash or other non alphabetical characters),
-
chose Number (Integer) for type,
-
Define a label
-
Deactivate history tracking (optional)
-
Modify lifecycle
-
Go to the tab Lifecycle:
-
Select in the drawing the transition from
resolved
toassigned
-
In the right panel, click on the button at the bottom of the list of actions.
-
In the dialog box
-
Select the method AddValue,
-
Select “reopen_counter” in the Target field (the newly created counter field)
-
Enter 1 in the Value
-
-
Modify presentation
-
Go to tab Presentation to add the "reopen_counter" to the details of the UserRequest in the Console
with an iTop Extension
Prerequisite: You must be familiar with the Syntax used in Tutorials and have already created an iTop extension.
For readability, we present this 6 steps with 6 different blocks of XML, but in reality, you just need to write one XML structure with all of them.
Add a counter field
- itop_design
-
<classes> <class id="UserRequest" _delta="must_exist"> <fields> <field id="reopen_count" xsi:type="AttributeInteger" _delta="define"> <sql>reopen_count</sql> <default_value>0</default_value> <is_null_allowed>true</is_null_allowed> </field> </fields> </class> </classes>
Deactivate history tracking
You may decide that it is useless to record in the history, each
time reopen_count
counter is changed
Then add
<tracking_level>none</tracking_level>
to
the field definition and it will do the job. This is just an
option, you don't have to do it. If you don't define that tag, iTop
will track the changes made on that field, as it is the default
when omitted.
- itop_design / classes / class@UserRequest / fields
-
<field id="reopen_count"> <!-- No tracking level, mean no entry in History log for this field --> <tracking_level>none</tracking_level> </field>
-
an ever changing value, mastered by an external source
-
a computed counter, whose modification dates might be useless
Call the method on transition
Let's increment the counter each time the ticket move from
resolved
to assigned
states.
For this we will benefit from an existing method
- itop_design / classes / class@UserRequest / lifecycle / states
-
<!-- When we are in "resolved" state of UserRequest lifecycle --> <state id="resolved"> <!-- We look at the possible transitions from that state --> <transitions> <!-- if an "ev_reopen" stimulus is applied --> <transition id="ev_reopen"> <actions _delta="redefine"> <!-- Because an action has no id, you must redefine ALL actions for that transition --> <action> <!-- Execute on current object the method with id="AddValue" --> <verb>AddValue</verb> <!-- with the following parameters --> <params> <!-- attribute code of the counter to increment --> <param xsi:type="attcode">reopen_count</param> <!-- also the default value is set in PHP, it must be done again in XML --> <param xsi:type="int">1</param> </params> </action> </actions> </transition> </transitions> </state>
Declare the PHP method
If the method that you want to trigger does not exist within the available methods, you can create yours:
- class:UserRequest
-
public function MyLifecycleMethod($sAttCode, $iIncrement=1) { // Defensive programming, ensure that: the field code is valid on current class if (MetaModel::IsValidAttCode(get_class($this), $sAttCode) // and the increment provided is numeric && is_numeric($iIncrement) // and the field code correspond to an Integer type of attribute && (MetaModel::GetAttributeDef(get_class($this),$sAttCode) instanceof AttributeInteger)) { $iNew = $this->Get($sAttCode) + $iIncrement; $this->Set($sAttCode, $iNew); } return true; }
Remember that PHP method need to be embedded in a XML structure
Add counter to presentation
Let's add this reopen_count
on the UserRequest
details page, in the 3rd column and in the SLA report
fieldset
In order to be able to write such delta XML, you need to decide
-
in which column of the details to display that field: col:col1 / col:col2 / col:col3, if there are columns defined.
-
retrieve the fieldset code, if any defined
-
guess a rank to use to position a particular field after or before another one, in default datamodel rank have a 10 increment between fields within a common container: fieldset, column or page (?).
- itop_design / classes
-
<class id="UserRequest" _delta="must_exist"> <presentation> <details> <items> <item id="col:col3"> <items> <item id="fieldset:Ticket:SLA"> <items> <item id="reopen_count" _delta="define"> <rank>80</rank> </item> </items> </item> </items> </item> </items> </details> </presentation> </class>
Add counter label
- itop_design / dictionaries / dictionary@EN US / entries
-
<entry id="Class:UserRequest/Attribute:reopen_count" _delta="define">Reopening counter</entry>
If you declared it on class Ticket instead of UserRequest, then it can be used on any Change classes as well