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

CC BY-SA 3.0
2017-08-11T16:08:15.353Z
2017-08-11T16:08:15.353Z
Angela Chan
<p>If you were to do this on entry save, it'd be much easier/cleaner.</p>

<p>You'd write a <a href="https://pluginfactory.io" rel="nofollow noreferrer">simple plugin</a> that listened to the <a href="https://craftcms.com/docs/plugins/events-reference#entries-onBeforeSaveEntry" rel="nofollow noreferrer"><code>onBeforeSaveEntry</code></a> event.</p>

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

<p>If it's new, you could generate a random numeric value with <a href="http://php.net/mt_rand" rel="nofollow noreferrer">mt_rand</a>, assign it to your custom field and left the request continue so Craft can save it to the entry.</p>

<p>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 <a href="http://php.net/manual/en/function.uniqid.php" rel="nofollow noreferrer">uniqid('', true)</a> if you deploy on a single server, or better use a <a href="http://codeaid.net/php/generate-a-random-guid" rel="nofollow noreferrer">uuid/guid</a>.</p>

<p>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 <a href="https://github.com/lindseydiloreto/craft-cpjs" rel="nofollow noreferrer">JS loaded</a> load, try to target the field in the particular section with JavaScript and inject the random number into the DOM.</p>

CC BY-SA 3.0
2017-08-08T21:45:27.087Z
2017-08-11T19:30:30.017Z
2017-08-11T19:30:30.017Z
Brad Bell
<p>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?</p>

CC BY-SA 3.0
2017-08-08T20:44:56.567Z
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
CC BY-SA 3.0
2017-08-08T21:03:10.813Z
@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
<entries><sections><channels><id>
Any ID I could reference in sections
# Any ID I could reference in sections

(Asked by: username_0 on 2017-08-08)

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?

**Tags:** entries, sections, channels, id

> **Comments:**
> **username_1**: 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?  
> **username_0**: @BradBell yes, `appointment_record_id` is a custom field. I set that field is `Number`. I want auto-populated on a new entry.  

---

## 2 answers

### Answer by username_0 on 2017-08-11. Score: 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.


---

### Answer by username_1 on 2017-08-08. Score: 1

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

You'd write a [simple plugin](https://pluginfactory.io) that listened to the [`onBeforeSaveEntry`](https://craftcms.com/docs/plugins/events-reference#entries-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](http://php.net/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)](http://php.net/manual/en/function.uniqid.php) if you deploy on a single server, or better use a [uuid/guid](http://codeaid.net/php/generate-a-random-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](https://github.com/lindseydiloreto/craft-cpjs) load, try to target the field in the particular section with JavaScript and inject the random number into the DOM.


---

