custom field auto incrementing number [FIXED]

Post about your customizations to share with others.

Moderators: Developer, Contributor

Post Reply
7h3ju57
Posts: 6
Joined: 05 May 2017, 20:36

custom field auto incrementing number [FIXED]

Post 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.
Last edited by 7h3ju57 on 14 Jun 2017, 17:31, edited 1 time in total.
7h3ju57
Posts: 6
Joined: 05 May 2017, 20:36

Re: custom field auto incrementing number

Post by 7h3ju57 »

someone must have some idea how to implement this?
NandoNaldo
Posts: 122
Joined: 09 Mar 2016, 21:44

Re: custom field auto incrementing 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 ?
7h3ju57
Posts: 6
Joined: 05 May 2017, 20:36

Re: custom field auto incrementing number

Post 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.
7h3ju57
Posts: 6
Joined: 05 May 2017, 20:36

Re: custom field auto incrementing 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.

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
Posts: 7
Joined: 12 May 2020, 17:09

szollosi@freemail.hu

Post by szollosi »

Hi, where have to put that code?
atrol
Site Admin
Posts: 8366
Joined: 26 Mar 2008, 21:37
Location: Germany

Re: custom field auto incrementing number [FIXED]

Post 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.
Please use Search before posting and read the Manual
Soumaya
Posts: 5
Joined: 27 Apr 2022, 12:30

Re: custom field auto incrementing number [FIXED]

Post 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.
cas
Posts: 1586
Joined: 11 Mar 2006, 16:08
Contact:

Re: custom field auto incrementing number [FIXED]

Post by cas »

So how did you call the function and what is exactly the code?
Soumaya
Posts: 5
Joined: 27 Apr 2022, 12:30

Re: custom field auto incrementing number [FIXED]

Post 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.
cas
Posts: 1586
Joined: 11 Mar 2006, 16:08
Contact:

Re: custom field auto incrementing number [FIXED]

Post by cas »

The picture is not that clear. I also notice a sql statement at the top, can you provide a clear picuter?
Soumaya
Posts: 5
Joined: 27 Apr 2022, 12:30

Re: custom field auto incrementing number [FIXED]

Post 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
cas
Posts: 1586
Joined: 11 Mar 2006, 16:08
Contact:

Re: custom field auto incrementing number [FIXED]

Post 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
Post Reply