Costum filed suggest the next sequencial number.

Post about your customizations to share with others.

Moderators: Developer, Contributor

Post Reply
NandoNaldo
Posts: 122
Joined: 09 Mar 2016, 21:44

Costum filed suggest the next sequencial number.

Post by NandoNaldo »

Hello,

I'am trying to find the same thing. I want to add a costum field, where i can put the id of my clients and everytime i have a new client, this filed could suggest the next sequencial number. Is this possible ?
NandoNaldo
Posts: 122
Joined: 09 Mar 2016, 21:44

Re: Costum filed suggest the next sequencial number.

Post by NandoNaldo »

I noticed in the phpmyadmin that the id has an extra comment: AUTO_INCREMENT

I was thinking: if i add a new filed with this extra comment, am i able to have a field that increments by it self ?
Attachments
Autoincrement php.JPG
Autoincrement php.JPG (98.62 KiB) Viewed 6410 times
atrol
Site Admin
Posts: 8366
Joined: 26 Mar 2008, 21:37
Location: Germany

Re: Costum filed suggest the next sequencial number.

Post by atrol »

Using AUTO_INCREMENT is the right way in MySQL for sequences (does not work with other databases).
But you can't use this for standard MantisBT custom fields. You can use it just for columns of tables that you created by your own.
Please use Search before posting and read the Manual
7h3ju57
Posts: 6
Joined: 05 May 2017, 20:36

Re: Costum filed suggest the next sequencial number.

Post by 7h3ju57 »

Well i had some time to tinker around and was able to get my custom field number to increment using this.
Hope this helps :)

Code: Select all

function custom_function_override_issue_create_notify( $p_issue_id ) {

#Get custom field id by field name
$t_id = custom_field_get_id_from_name( 'fieldname' );

#Get the current yeat
$c_year = date("Y");

#Get the last created tickets field value
$latest_x = custom_field_get_value($t_id ,($p_issue_id - 1));

#Substring manipulation (extracting the year from the custom field)
$x_year = substr($latest_x, 0, 4);

#Substring manipulation (extract incrmental number)
$x_num = substr($latest_x, 5);

#Increment the number by 1
$new_x = ($x_num + 1);

#Verify that we are in the same year.
        if($x_year == $c_year){

			#If we are, increment the number 
                custom_field_set_value( $t_id, $p_issue_id, $c_year . '-' . $new_x, $p_log_insert = false);
        } else {

			#If not set number to 1
                custom_field_set_value( $t_id, $p_issue_id, $c_year . '-' . '1', $p_log_insert = false);
        }
}
Post Reply