Creating AutoGenerating Field

I want to Create one unique number field in Account module. This field is auto generate and alpha numeric. When account is created then this field value will auto genetare and must be unique.

The term you’re looking for is “auto-increment”

There are several threads here explaining it, like this one

i want alpha-numeric field not int
type=>‘int’ i already did, now i want to generate alpha-numeric field

You can’t do that without custom code, I think.

Maybe you could, if you add a custom field that is auto-calculated from an after-save Workflow, and that uses the value from the auto-increment (numeric) to concatenate with some string part.

1 Like

What kind of alphanumeric field do you need? Do you mean something like XXX-1234 or can any character be either number or letter? The first one is quite simple to do, if you know bit of PHP, and the latter depends on what kind of strings you’re looking for.

But I agree with @pgr, you can’t do that without custom code. Studio and/or Workflows are unfortunately too limited in this sense.

XXX-1234 will work for Me.
@TLi if you give me a hint then i will create this field

@jrmk sorry for the delay. I’m assuming you want an autoincrement for numbers while letters stay the same, ie. first one is abc-0001 followed by abc-0002, abc-0003 etc.

The problem lies in that only numbers can be autoincremented. When you add letters to the mix or even if you’re just saving numbers to a textfield, you’re handling strings, and SuiteCRM doesn’t know how to autoincrement those. What you need is a value that is treated by the server as a number, so the string needs to be converted into something that the server can handle as a number. Depending on your use case and specific needs there are a few ways to do this.

If you only need the letters for visual reasons, a simple solution would be to only save the number, incrementing only this, and append letters e.g. with JS wherever the values are shown. This would however mean that the combination is not inherited to, say, listviews, subpanels or PDF templates, and the solution wouldn’t be universal; instead you’d need to combine the values with custom code where ever they would be joined.

If you need to save both the letters and the numbers together (ie. you need the combination), there are a couple of ways.

You could use an external record (e.g. a value in config array) to keep track of the latest incremental value, and whenever a new autoincremented value is needed, fetch that and add one, resaving the new latest incremental value. While this is a simple process, it would work only as long as you keep track of the latest increment. Also you’d need to implement some way to solve conflicts if records were created at the same time, fetching the same incremental value.

Another way would be to fetch all existing records, explode the incrementing values by the separating symbol (’-’ in this case) and look for highest existing value. This way increment wouldn’t depend on any externally saved values, but the solution would be terribly inefficient.

A third solution would be to create two fields, one holding only a unique, auto-incrementing number (you could achieve auto-increment either by editing vardefs as suggested earlier, by tweaking the controller or with a logic hook) and then another field holding the combination of letters and numbers you seek, ie. the actual value visible to the user. If also letters change, you could just create a another field and process the letters as you wish. If they’re static, you could hard-code them or e.g. fetch them from config array. Either way you’d combine the number with the letters afterwards either by customizing the controller or in a logic hook fired later than the increment. Just make sure you’re combining only after the numbers and letters have been processed. This would be a more efficient solution than the previous one, if not the most elegant. You could even hide the number (and letters) field(s) from studio if you wanted to “hide” your tracks.

Sorry ,
I tried but each time I Repair , SuiteCRM needs to recreate the field. Why ?