Menu

Automatically set a field value based on other fields

ray
2021-02-12
2021-03-30
  • ray

    ray - 2021-02-12

    Hi All,

    I hope all of you are safe and well.

    I'm using iTop 2.4 and I'm trying to implement a text field that automatically generate its value based on 2 date fields. For example, when editing a Server CI (https://demo.combodo.com/simple/pages/UI.php?operation=modify&class=Server&id=1&c[menu]=ConfigManagementOverview#), when a user enters the "purchase date" field and "end of warranty" date field, there's a new field called "years of service" which value is automatically calculated by decrementing the year in "purchase date" and "end of warranty" (please refer to below image).
    year of service field

    Would be really grateful if someone could point me to a field/extensions that already implements this kind of functionality.

    Kind regards,
    Ray

     
  • Vincent @ Combodo

    Hello Ray,
    This require to write your own extension, but such computed field is rather simple to do.
    Check here for tutorial: https://www.itophub.io/wiki/page?id=2_7_0%3Acustomization%3Acompute-field

    In your particular case, use the ComputeValues() method, force that field to be read-only and don't forget to set dependencies when declaring the new field, so it is recomputed on the fly

     
    👍
    1

    Last edit: Vincent @ Combodo 2021-02-12
    • ray

      ray - 2021-03-25

      Hi Vincent,

      Thank you very much for the help.

      I'm trying to implement the compute automation using datamodel XML (it seems that in datamodels/2.x/itop-request-mgmt/datamodel.itop-request-mgmt.xml, you could use <method id="ComputeValues">).

      My sample code at the moment (just testing using random string for now but it's not working (please refer to attached image))
      datamodel.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <itop_design xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
        <constants>
        </constants>
        <classes>
      
          <class id="PC">
            <fields>
      
              <field id="year_of_service" xsi:type="AttributeString" _delta="define">
                <sql>year_of_service</sql>
                <default_value/>
                <is_null_allowed>true</is_null_allowed>
              </field>
      
            </fields>
            <presentation>
              <details>
                <items>
                  <item id="col:col2">
                    <items>
                      <item id="fieldset:Server:Date">
                        <items>
                          <item id="year_of_service" _delta="define">
                            <rank>40</rank>
                          </item>
                        </items>
                      </item>
                    </items>
                  </item>
                </items>
              </details>
            </presentation>
      
            <methods>
      
              <method id="ComputeValues" _delta="define">
                    <static>false</static>
                    <access>public</access>
                    <type>Overload-DBObject</type>
                    <code><![CDATA[    public function ComputeValues()
                        {
      
                        //$oStartDate = new DateTime($this->Get('purchase_date'));
                        //$oEndDate = clone $oStartDate;
                        //$iDuration = 2*3600 ; // 2 hours in seconds
                        //$oEndDate->modify( '+'.$iDuration.' seconds');
                        //$this->Set('year_of_service',$oEndDate->Format(AttributeDateTime::GetInternalFormat()));
                        $this->Set('year_of_service','hello world');
      
                        return parent::ComputeValues();
                        }]]></code>
                </method>
      
            </methods>
          </class>
      
        </classes>
        <menus>
        </menus>
        <user_rights>
          <groups>
          </groups>
          <profiles>
          </profiles>
        </user_rights>
      </itop_design>
      

      I would really appreciate if someone could let me know what's wrong. I will try to force the field as read-only as well after this (perhaps the issue is because I haven't made it read-only)? I will try using <method id="GetAttributeFlags"> and <method id="GetInitialStateAttributeFlags"> for making it read-only

      Thank you and I really appreciate any help.

      Kind regards,
      Ray

       
  • Vincent @ Combodo

    If you save your object, is it saved with "hello word" ?
    What happen is that your field "year_of_service" has no dependency on other fields, so also it is computed, it is not refreshed. The refresh mechanism works only if a field on which you depend was modified in the screen, then the computed one is refreshed.
    It your goal is just to prefill values, then check the tutorial on prefill forms.

     
    • ray

      ray - 2021-03-26

      Hi Vincent,

      Thank you very much for the help!

      Yes it will save the attribute with value "hello world" when clicking save. As proposed, I have added some dependency to the field so that the computation is refreshed on value change of the dependency field.

      If anyone is interested, please find below the changes to the field
      datamodel.xml

      <field id="year_of_service" xsi:type="AttributeString" _delta="define">
                <sql>year_of_service</sql>
                <default_value/>
                <is_null_allowed>true</is_null_allowed>
                <dependencies>
                  <attribute id="purchase_date"/>
                </dependencies>
              </field>
      
       
      • Pierre Goiffon

        Pierre Goiffon - 2021-03-30

        Good to know this is working now, and thanks to have shared the final code !

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.