function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Saurabh SmartySaurabh Smarty 

Unique custom field on insert of new record

Hi All,

I have a scenario, wherein I have a custom field thats made of 8 characters- first four being letters and the next four digits. I need to increment the letters part every time a new record of this object is inserted and it has to be unique for every record.
I want to do this using apex triggers.
Any help appreciated !
Palani_ChinnasamyPalani_Chinnasamy
@saurabh smarty, Please make me clear, Do you want do increase last 4 digit or first 4 letters ? If you want to increment the last 4 digit number like 0001, 0002, etc. You can use Auto number field and copy to custom field in trigger/ WF. 
 
Saurabh SmartySaurabh Smarty
Hi Palani, sorry for the cofusion, I meant I have to increment the digits part, letters ll remain same.
Palani_ChinnasamyPalani_Chinnasamy

That is fine, Now you can use Auto number field to generate/Increase the nunber for every new record. If you want it to be custom editable field, use Workflow field update to copy the Custom field.  Make it the autonumber field in backend. 

 

 

Palani_ChinnasamyPalani_Chinnasamy
The below discussion may helpfull to you to create Auto-Number 
https://success.salesforce.com/answers?id=90630000000hIMVAA2 
Saurabh SmartySaurabh Smarty
Hi Palani, Thanks for the reply, actually I wanted to do this using triggers and code but anyways I have used the above autonumber functionality for now. Will share the results once my code solution works
David @ ConfigeroDavid @ Configero
Unless you have some other hidden requirements not shared here, autonumber is your best route.  There's no need to over complicate the solution and write code if you don't have to.  You can set the autonumber to a static 4 character prefix with an auto incrementing 4 digit number at the end.  Similar to ABCD{0000} for your autonumber syntax, but replace ABCD with whatever 4 character prefix you want.