2

I created a section for appointment record (channel type). Each appointment record has different fields, i.e. customer name, phone#, address, etc... I would like to set a unique appointment_record_id (Numeric, i.e. 234) for each record. Is there any way to create it?

  • Not 100% sure I'm following... is appointment_record_id a custom field? If so, what type of field is it? Do you want it to be auto-populated on a new entry or when an entry is saved? – Brad Bell Aug 8 '17 at 20:44
  • @BradBell yes, appointment_record_id is a custom field. I set that field is Number. I want auto-populated on a new entry. – Angela Chan Aug 8 '17 at 21:03
1

Thanks @Brad, I created a plugin with appointment_record_id (PK with auto increment), customer_id and timestamp table. I insert a record when system creates Appointment Record, and then I reference the appointment_record_id as my id.

1

If you were to do this on entry save, it'd be much easier/cleaner.

You'd write a simple plugin that listened to the onBeforeSaveEntry event.

The plugin would check the isNewEntry param to make this this is a new entry and not an existing one.

If it's new, you could generate a random numeric value with mt_rand, assign it to your custom field and left the request continue so Craft can save it to the entry.

A string field for the unique identifier would be better though, since mt_rand will not always produce unique numbers (for unique numbers you'd need to keep a separate database table and obtain id's within a transaction). If you'd use strings you could use uniqid('', true) if you deploy on a single server, or better use a uuid/guid.

If you didn't do it on save, but wanted to be auto-populated when someone clicked "New Entry", you'd have to do something much more hacky like have JS loaded load, try to target the field in the particular section with JavaScript and inject the random number into the DOM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.