How To add Auto Increment in module default ID fields Suit CRM

僤鯓⒐⒋嵵緔 提交于 2019-12-11 08:30:42

问题


Is it possible to add auto increment in module default ID field in Suite CRM. ID field contains 36 char type.

Is there any settings options available in SuiteCRM ?


回答1:


In my experience, the goal of auto-incrmenting fields is usually to create a human-friendly ID field, e.g. Case ID, Quote ID, Account Number. This is different than the CRM system's GUID, though both are effectively unique identifiers. It's is simply that the human-friendly ID field is easier to communicate with colleagues ("Hey Jim, bring up account 1505" is easier than "bring up account 6ccd780c-baba-1026-9564-5b8c656024db").

With that in mind, the goal here is to add a new integer field entirely, and ensure that it auto-increments. SugarCRM supports auto-incrementing integer fields out-of-the-box, but it is a code level customization. This is not applicable to the default id field, since this is a varchar field.

Let's look at the Professional edition's Quote field "Quote Number" as an example,

The vardef is thus defined in modules/Quotes/vardefs.php:

<?php
$dictionary['Quote']['fields'][] = 
    'quote_num' => array(
        'name' => 'quote_num',
        'vname' => 'LBL_QUOTE_NUM',
        'type' => 'int',
        'auto_increment' => true,
        'readonly' => true,
        'required' => true,
        'unified_search' => true,
        'full_text_search' => array('enabled' => true, 'boost' => 3),
        'disable_num_format' => true,
        'enable_range_search' => true,
        'options' => 'numeric_range_search_dom',
    );

The vardefs further define an index to set the auto-increment feature in the database level:

<?php
$dictionary['Quote']['indices'][] = 
    array(
        'name' => 'quote_num',
        'type' => 'unique',
        'fields' => array('quote_num', 'system_id')
    );

Note that the index for your custom field will probably not need the system_id reference, it is more typical to definite it as simply 'fields' => array('quote_num')

Another out-of-the-box example is in the Cases module with the field case_number. It also employs a vardef and index definition.

This strategy can be extracted to a new custom field on any out-of-box or custom module. One would define both the field and the index in custom/Extension/modules/MyModule/Ext/Vardefs/customfield.php. Note that when using the Extension framework, Studio will create a field in this directory like sugarfield_myfield.php and it would be best to not define your vardef in the same file, because Studio will overrite them. Further, it is best to not define conflicts in your file that Studio may attempt to generate, or else one change or the other will be overridden. Define in customfield.php only what is necessary to accomplish your goal, and format the array such that it does not entirely re-define itself when the code is executed, e.g.

 $dictionary['Quote']['fields']['quote_num']['auto_increment'] = true;
 $dictionary['Quote']['fields']['quote_num']['disable_num_format'] = true;



回答2:


The way I have implemented auto-increment fields in SuiteCRM is like this:

  • Create custom field of type TextField by using SuiteCRM Studio. For example add the field product_number to the module Products. This will create the field product_number_c in the database table aos_products_cstm. The field should not be required.

  • Remove the newly created field product_number from the EditView of the module Products.

  • In your database, create the table product_number_seq, that has only the field id, which is an auto-increment field. The name product_number_seq can be changed to whatever you prefer.

  • Create trigger on the table aos_products_cstm:

    CREATE DEFINER=`root`@`localhost` TRIGGER product_number_insert
    BEFORE INSERT ON aos_products_cstm
    FOR EACH ROW
    BEGIN
      INSERT INTO product_number_seq VALUES (NULL);
      SET NEW.product_number_c = LAST_INSERT_ID();
    END
    

If you do this, before every insert on the table aos_products_cstm, the trigger will make an insert to the table product_number_seq and provide NULL as value, so the next int will be inserted (because it is an auto-increment field). Then it will set the field product_number_c of the record that is being inserted in aos_products_cstm, to the result of LAST_INSERT_ID() - which will be the recently inserted value to product_number_seq.

It is obviously hack, so you might not prefer it, but I have used it and it works.



来源:https://stackoverflow.com/questions/43203152/how-to-add-auto-increment-in-module-default-id-fields-suit-crm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!