Newbie question. I'm looking for a way to add a custom field to each contact that will be a unique membership ID but based on a pattern, eg. 2019-### etc. I'm using Civi with Drupal 7
I also used ContactID to hold membership ID for the same reason that a unique value is allocated by CiviCRM. There doesn't seem to me any standard way to have an autoincrement field in CiviCRM without doing some coding. You also have to think whether the membership ID should be in the membership record rather than the contact record, though because I had some historical data, I decided against this. To get the historical data in I created records for all the membership numbers up to the last one previously used, but tagged all that hadn't been used so that I could delete them all later. I think imported all these records and they were automatically allocated the matching contact id (fortunately I didn't have a membership 1 which was already allocated to the admin account. I then deleted the contacts for the numbers that hadn't been previously used. New contacts were autoincremented from the last one I had used. You might be able to do a trick to get your numbers starting 2019 etc, but that would mean creating a hell of a lot of records and then deleting them, so not sure its a sensible option, but you could try (maybe with just a 19 prefix, so you would be OK for the next 80 years).
I don't think that CiviRules has anything built in that will create an autoincrement field. Indeed I can't see something available off the shelf to set a custom field. I think you would need to write some code.
One way i could think of is to create a custom field on contacts, and use hook_civicrm_post hook using following conditions:
- $op = create
- $objectName = contact
You will get contact Id in $objectId variable, then you can use CiviCRM API to update custom field value based on whatever pattern+contactID you desire.
I would check out the CiviCRM docs to create a custom field: https://docs.civicrm.org/user/en/latest/organising-your-data/creating-custom-fields/
We used the Contact ID for our membership ID's because they are automatically a unique ID. For something like 2019-### you would probably have to manual enter each or write something to do that. You could maybe use CiviRules to help.