Creating an Autoincrement Field

Hello guys,

I do have a question (as every day :P), i want to create a field that is autoincremental, for example the field will be name as n_ invoice_1, and the format would be something like that, INV-01, INV-02, etc…

I searched on internet and i found some interesting post, they said that in studio it is not possible to do that, that you have to do it by yourself (php),
i found a code made by maranemil, but i dont understand it completely.
My field is named n_invoice_1, and the prefix is INV, what do i need to change from that code, to implement it on my suitecrm?
Here is the code.

Thx :slight_smile:

check tjhis post https://suitecrm.com/forum/suitecrm-7-0-discussion/1841-how-to-add-a-autoincrement-field-for-account-and-contacts?limitstart=0#7591

and after that you’ll need to create a logic hook that concatenates your prefix with that autoincrement number

best regards

Hi mikebeck, thx for the reply.

I tried what Will said, I created an integer field and a php file with this code(with my modules and fields names):

<?php $dictionary['YOUR_MODULE']['fields']['NAME_OF_AUTO_INC_FIELD']['auto_increment'] = true; ?>

But it’s not working for me.

<?php
	 $dictionary['YOUR_MODULE']['fields']['NAME_OF_AUTO_INC_FIELD']['auto_increment'] = true; 
?>

Also, my english it’s not good, i don’t know what logic hook is.

Hey mikebeck, i got it!

I instaled a module that were in the post you mentioned, and now i can create autoincremental fields.

Thanks!

I don’t recommend that module, I had troubles with it in the past

best regards

Then, what do you recommend?

I had issues with that module after reaching the 99999 it just stopped counting because I set the format to be 00001, and there’s no way to correct that (plus, I don’t like the zeros), I’m just using a simple database auto increment field now

best regards

1 Like

Hello Mike

Please can you guide me on how to create the auto-increment field with the database so i can add that in my suitecrm. Please guide me through also how to use it on the studio at the particular modules I wanted to.

Thanks in advance.

Hi.
I do this easily from phpmyadmin.

  1. I select the field and select the Auto Increase option and save changes.
  2. If I want to select from which number I start the counter, I select the operations option and write the number.

see the attached image.

https://imgur.com/a/bYilwNG

I hope it works for you.

Thanks a lot for your answer. But after creating this autoincrement on the phpmyadmin, I wanted to know how I can use it on the studio. or the module that I wanted to use on. what should i do on the Suite Crm ?

Hello,

Just crate an integer field in your module by studio and add it to the detail view. Then make the changes in phpmyadmin.

its really simple …to great to be true… i try whit my current sistem under suitecrm and … tell you later…

I did this the other day for a custom module.

  1. Create a integer field in studio like normal.
  2. Edit the vardefs.php for the module and change the field so it has the values below
   '[fieldname]' =>
    array (
      'name' => '[fieldname]',
      'vname' => 'Number',
      'type' => 'int',
      'readonly' => true,
      'len' => 11,
      'auto_increment' => true,
      'disable_num_format' => true,
      'inline_edit' => false,
    ),
  1. Within the main ‘fields’ array in vardefs.php add an indices array (if it doesn’t exist)
 'indices' =>
  array (
    '[fieldindexname]' =>
    array (
      'name' => '[fieldindexname]',
      'type' => 'unique key',
      'fields' => array ( '[fieldname]')),
    ),
  1. Run a rebuild and repair

The only bug I found with this method is that rebuild/repair seems to infinitely say there’s an consistency issue between the database and vardefs which seems to relate to the way the repair module checks if the field is a unique key.

Thanks

Urbanite

1 Like

Hola, Lo hice por el phpadmin y realmente fue muy fácil, no requiere de código…

Muchas gracias…

Just correcting above…

type must be 'unique" , not “unique key”

1 Like

Correct! My Bad.

For someone who need it… Exact and correct code which works for me…

‘auto_number’ =>
array(
‘name’ => ‘auto_number’,
‘vname’ => ‘Serial No’,
‘type’ => ‘int’,
‘len’ => 11,
‘required’=>true,
‘auto_increment’ => true,
),
shareeditdelete

ah, so the length of those bean ids can be shortened, they don’t have to look like nuclear launch codes?! :smiley:

You know, example: record=92a2bf72-237d-84ef-9ccd-5d2264f10161

I’m looking to use a short unique id for vendor_id and profit_center in my Accounts module so I can do separate accounting reporting on budgets, revenues, profit, loss in different operations

Please can you specify where I can add this code to have the serial number