Serial number with Prefix or Suffix

Is there any way to add automated serial numbers with prefixes and suffixes in records?
Parents
  • What you need is first a running number, like the case number or bug number .

    This can be realized by an autoincrement field in your module. If the module does not have an autoincrement field you can create your own by a vardef extension, e.g. for opportunities in custom/Extension/modules/Opportunities/Ext/Vardefs/ with a file like this:

    <?php
    $dictionary['Opportunity']['fields']['opportunity_index'] =
        array (
          'name' => 'opportunity_index',
          'vname' => 'LBL_OPP_INDEX',
          'type' => 'int',
          'readonly' => true,
          'len' => 11,
          'required' => true,
          'auto_increment' => true,
          'unified_search' => true,
          'full_text_search' => 
          array (
            'boost' => 3,
          ),
          'comment' => 'Visual unique identifier',
          'duplicate_merge' => 'disabled',
          'disable_num_format' => true,
          'studio' => 
          array (
            'quickcreate' => false,
          ),
        );
    	
    $dictionary['Opportunity']['indices']['number'] =	
        array (
          'name' => 'oppnumk',
          'type' => 'unique',
          'fields' => 
          array (
            0 => 'opportunity_index',
          ),
        );
    
    $dictionary['Opportunity']['indices'][1] =	
        array (
          'name' => 'opportunity_index',
          'type' => 'unique',
          'fields' => 
          array (
            0 => 'opportunity_index',
          ),
        );
    ?>

    During QR&R you must make sure that all SQL commands for the opportunity table are exectuted in one SQL statement.

    Then you can create a custom field e.g. opportunity_number_c and add a prefix and/or suffix in a logic_hook after_save.to the running index e.g. like this:

    <?php
    
    class my_OpportunitiesHookImpl
    {
        public function afterSave($bean, $event, $arguments)
        {
    		$prefix = "OPP#";
    		$suffix = "_crm";
    		//Prevent endless loop by checking alread set number:
            if ($bean->opportunity_number_c == "") { 
                $number = $bean->opportunity_index;
                $number = "000000" . $number;
                $number = substr($number, -6);
                $opp = BeanFactory::getBean('Opportunities', $bean->id);
                $opp->opportunity_number_c = $prefix;
                $opp->opportunity_number_c .= $number;
                $opp->opportunity_number_c .= $suffix;
                $opp->save();
            }
    	}
    
    }

    The format can be just as you want, you can even add some parte of the date to it.

    Harald Kuske
    Principal Solution Architect – Professional Services, EMEA
    hkuske@sugarcrm.com
    SugarCRM Deutschland GmbH

Reply Children
No Data