custom field auto incrementing number [FIXED]
Moderators: Developer, Contributor
custom field auto incrementing number [FIXED]
hi all,
im trying to get an auto incrementing numerical field that would start with the year plus a number that increments with each new ticket opened. for example, user opens new issue, and field autoinc would have an auto generated number of 2017-1 (which ideally would be first ticket of the corresponding year). I've seen some examples for custom date and enumerating fields but nothing for string or numerical fields. any help would be greatly appreciated.
im trying to get an auto incrementing numerical field that would start with the year plus a number that increments with each new ticket opened. for example, user opens new issue, and field autoinc would have an auto generated number of 2017-1 (which ideally would be first ticket of the corresponding year). I've seen some examples for custom date and enumerating fields but nothing for string or numerical fields. any help would be greatly appreciated.
Last edited by 7h3ju57 on 14 Jun 2017, 17:31, edited 1 time in total.
Re: custom field auto incrementing number
someone must have some idea how to implement this?
-
- Posts: 122
- Joined: 09 Mar 2016, 21:44
Re: custom field auto incrementing number
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 ?
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 ?
Re: custom field auto incrementing number
only way i can think of is to have a file at the root of mantis with a variable. When you report a new issue it would pull that variables value and if you submit that issue then it overwrite the file with the new variable value. ie: var=2 + 1
not sure if this is the best way to implement though.
not sure if this is the best way to implement though.
Re: custom field auto incrementing number
Well i had some time to tinker around and was able to get my custom field number to increment using this.
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);
}
}
szollosi@freemail.hu
Hi, where have to put that code?
Re: custom field auto incrementing number [FIXED]
https://www.mantisbt.org/docs/master/en ... ustomfuncs
User versions of these functions (overrides) are named like custom_function_override_descriptive_name, and placed in a file called custom_functions_inc.php that must be saved in MantisBT's config directory.
Re: custom field auto incrementing number [FIXED]
I am trying to do more or less the same thing, I want a custom field that automatically builds and displays a link based on a value from another custom field.
As I'm very new to php and this entire situation, I started by simply trying to implement your fix and see how it goes. If implementing your auto-incrementing custom field works, I intended to tinker it a bit to suit my situation.
So, I created the custom field, and the custom_functions_inc.php file in my config where i put your code.
However, this is the result i get:

The "fieldname" field remains empty.
I wonder what it is that I'm doing wrong, or if I misunderstood what the function does.
As I'm very new to php and this entire situation, I started by simply trying to implement your fix and see how it goes. If implementing your auto-incrementing custom field works, I intended to tinker it a bit to suit my situation.
So, I created the custom field, and the custom_functions_inc.php file in my config where i put your code.
However, this is the result i get:

The "fieldname" field remains empty.
I wonder what it is that I'm doing wrong, or if I misunderstood what the function does.
Re: custom field auto incrementing number [FIXED]
So how did you call the function and what is exactly the code?
Re: custom field auto incrementing number [FIXED]
Thank you for your reply, sorry I just saw it.
All I did was add this code from 7h3ju57's reply to the config/custom_functions_inc.php file i created (after creating the custom field 'fieldname'):
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);
}
}
As to how I called it, to be honest I didn't? all I did was add the code to the config file and that's it.
Am I supposed to call it somewhere on the source code? and if so, where?
Thank you very much.
Re: custom field auto incrementing number [FIXED]
The picture is not that clear. I also notice a sql statement at the top, can you provide a clear picuter?
Re: custom field auto incrementing number [FIXED]
Of course.
Here is a screenshot of the entire page, what's printed at the start of the page is printed at the start of every page.
Also, I noticed that what's printed on the page is the entire code I added in "custom_functions_inc.php", that was previously shared by 7h3ju57.

Re: custom field auto incrementing number [FIXED]
Well then you better check that file again because the code seems to be suppressed.
If you take the current code out, those lines will disappear.
Otherwise zip that file and attach it here so I can have a look for you
If you take the current code out, those lines will disappear.
Otherwise zip that file and attach it here so I can have a look for you