Page 1 of 1

custom field auto incrementing number [FIXED]

Posted: 05 May 2017, 20:49
by 7h3ju57
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.

Re: custom field auto incrementing number

Posted: 24 May 2017, 19:15
by 7h3ju57
someone must have some idea how to implement this?

Re: custom field auto incrementing number

Posted: 26 May 2017, 12:51
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 ?

Re: custom field auto incrementing number

Posted: 26 May 2017, 19:42
by 7h3ju57
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.

Re: custom field auto incrementing number

Posted: 14 Jun 2017, 17:29
by 7h3ju57
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

Posted: 10 Feb 2022, 08:44
by szollosi
Hi, where have to put that code?

Re: custom field auto incrementing number [FIXED]

Posted: 10 Feb 2022, 09:42
by atrol
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]

Posted: 28 Apr 2022, 12:17
by Soumaya
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:
Image
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]

Posted: 29 Apr 2022, 07:00
by cas
So how did you call the function and what is exactly the code?

Re: custom field auto incrementing number [FIXED]

Posted: 04 May 2022, 14:56
by Soumaya
cas wrote: 29 Apr 2022, 07:00 So how did you call the function and what is exactly the code?
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);
        }
}
And whenever I open a page, what I shared in the pic shows up.

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]

Posted: 05 May 2022, 13:45
by cas
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]

Posted: 05 May 2022, 14:41
by Soumaya
cas wrote: 05 May 2022, 13:45 The picture is not that clear. I also notice a sql statement at the top, can you provide a clear picuter?
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.
Image

Re: custom field auto incrementing number [FIXED]

Posted: 05 May 2022, 14:51
by cas
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