Menu

Autoincrement attribute

2022-07-07
2022-09-22
  • Cristian Ozan

    Cristian Ozan - 2022-07-07

    Hello

    Is there any chance to create a new autoincremental attribute field in a Class?
    In my case, I need a new autoincremental attribute in the PC Class that has a prefix followed by a number that change every time I create a new PC.

    Thanks a lot.
    Cristian

     
  • Jeffrey Bostoen

    Jeffrey Bostoen - 2022-07-07

    Would it be safe to assume that this is always new, and never needs to be modified manually or anything?

    Theoretically you could auto-calculate a field and apply some logic there to do exactly this. I've done something similar myself for invoices in iTop :)

    https://www.itophub.io/wiki/page?id=latest%3Acustomization%3Acascade-update

     
  • Cristian Ozan

    Cristian Ozan - 2022-08-09

    Hello
    I tried this in the CLASS PC but dont work
    What is wrong?

      <methods>
        <method id="DBInsertNoReload">
            <static>false</static>
            <access>public</access>
            <type>Overload-DBObject</type>
            <code><![CDATA[
    public function DBInsertNoReload()
    {
          $iNextId = ItopCounter::IncClass(get_class($this));
          $ramdomNumber = $this->GetAutoIncremental($iNextId);
          $this->SetIfNull('ramdomNumber', $nroInv);
          $iKey = parent::DBInsertNoReload();
          return $iKey;
    }
        ]]></code>
        </method>
        <method id="GetAutoIncremental">
                    <static>false</static>
                    <access>protected</access>
                    <type>Overload-DBObject</type>
                    <code><![CDATA[
        protected function GetAutoIncremental($iNextId)
        {
                return sprintf(static::GetFormat(), $iNextId);
        }
        ]]></code>
                </method>
                <method id="GetFormat">
                    <static>true</static>
                    <access>public</access>
                    <type>Overload-DBObject</type>
                    <code><![CDATA[
        public static function GetFormat()
        {
                return 'CPE-%06d';
        }
        ]]></code>
                </method>
            </methods>
    
     
  • Pierre Goiffon

    Pierre Goiffon - 2022-08-30

    Hello,

    Can you explain what was your initial functionnal need ?
    Also, what error are you getting and when ?
    What is your iTop version ?

     
    • Cristian Ozan

      Cristian Ozan - 2022-09-02

      Yes, I need that when will create a new physical device (PC, Printer, etc) a new field that his value be an autoincremental number, for example INV-00001, next, INV-00002 and so on.
      My iTop version is 3.01.
      Thanks for replay.
      Cristian

       
    • Pierre Goiffon

      Pierre Goiffon - 2022-09-15

      Yes, I need that when will create a new physical device (PC, Printer, etc) a new field that his value be an autoincremental number, for example INV-00001, next, INV-00002 and so on.

      Do you mean you need all PhysicalDevice instances to have a specific reference field, like the UserRequest do ?
      You didn't answer to my question about what 'it don't work" means ?

      In the code you posted :
      $iNextId = ItopCounter::IncClass(get_class($this));
      The counter should be set to the same key whatever the class is. Here you'll get a different counter for every classes (PhysicalDevice, PC, ...)
      So you should change to something like :
      $iNextId = ItopCounter::IncClass('PhysicalDevice');

      This is a problem I see, but please explain precisely what is wrong with your implementation ?

       
      • Cristian Ozan

        Cristian Ozan - 2022-09-16

        Good Morning Pierre
        Thanks for your answer, I will try you suggest.
        My problem is that when create a new physical device there isnt any value. But ass you said, maybe my error was specify in your comment. Thanks again. if i can do it i will tell you

         
        • Cristian Ozan

          Cristian Ozan - 2022-09-16

          EDIT 21/09 to add code blocks

          Hi Pierre
          This is my code, which does not generate any error but does not show me any value in the field asset_number. Alway appear empty.

          Where do you thinks is the error.

          <class id="PhysicalDevice" _delta="must_exist">
              <methods>
                          <method id="GetAttributeFlags" _delta="define">
              <static>false</static>
              <access>public</access>
              <type>Overload-DBObject</type>
              <code>
                  <![CDATA[public function GetAttributeFlags($sAttCode, &$aReasons = array(), $sTargetState = '')
                          {
                              if (($sAttCode == 'asset_number'))
                                  return(OPT_ATT_READONLY | parent::GetAttributeFlags($sAttCode, $aReasons, $sTargetState));
                              return parent::GetAttributeFlags($sAttCode, $aReasons, $sTargetState);
                          }]]>
                </code>
          </method>
          <method id="GetInitialStateAttributeFlags" _delta="define">
              <static>false</static>
              <access>public</access>
              <type>Overload-DBObject</type>
              <code>
                  <![CDATA[public function GetInitialStateAttributeFlags($sAttCode, &$aReasons = array())
                      {
                          if (($sAttCode == 'asset_number'))
                              return(OPT_ATT_HIDDEN | parent::GetInitialStateAttributeFlags($sAttCode, $aReasons));
                          return parent::GetInitialStateAttributeFlags($sAttCode, $aReasons);
                      }]]>
              </code>
          </method>
          <methods>
                          <method id="DBInsertNoReload">
                              <static>false</static>
                              <access>public</access>
                              <type>Overload-DBObject</type>
                              <code><![CDATA[
              public function DBInsertNoReload()
              {
                    $iNextId = ItopCounter::IncClass(PhysicalDevice);
                    $sRef = $this->MakeNI($iNextId);
                    $this->SetIfNull('asset_number', $sRef);
                    $iKey = parent::DBInsertNoReload();
                    return $iKey;
              }
                  ]]></code>
                          </method>
                          <method id="MakeNI" _delta="define">
                              <static>false</static>
                              <access>protected</access>
                              <type>Overload-DBObject</type>
                              <code><![CDATA[
                  protected function MakeNI($iNextId)
                  {
                          return sprintf(static::GetNIFormat(), $iNextId);
                  }
                  ]]></code>
                          </method>
                          <method id="GetNIFormat" _delta="define">
                              <static>true</static>
                              <access>public</access>
                              <type>Overload-DBObject</type>
                              <code><![CDATA[
                  public static function GetNIFormat()
                  {
                          return 'INV-%06d';
                  }
                  ]]></code>
                          </method>
                      </methods>
          </methods>
          
           

          Last edit: Pierre Goiffon 2022-09-21
          • Jeffrey Bostoen

            Jeffrey Bostoen - 2022-09-22

            There seems to be a missing

            _delta="define"
            

            on your DBInsertNoReload() method?

             
            • Cristian Ozan

              Cristian Ozan - 2022-09-22

              Yes!!!, problem solve!.

              Now I can automatically assign a new inventory number to each physical device.
              Thanks a lot Jeffrey!

               
              👍
              1
        • Pierre Goiffon

          Pierre Goiffon - 2022-09-21

          The SetIfNull method won't update the field if it doesn't contain a NULL value... Maybe make the field read only for the users, and just call Set() directly ?

           
  • Pierre Goiffon

    Pierre Goiffon - 2022-08-30

    Wooops forgot my previous answer sorry
    You already explain the functionnal need :D
    Can you reply and both other questions though ?

     

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.