• Greg Rohman 9
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Hello.

I have a business use case where we are looking to generate a unique identifier every time an Account record is created. The identifier needs to follow a specific format, so an autonumber field is not an option in this scenario.

We have a fairly simple bulkified trigger in place to generate this code. Basically, the trigger:
  • Queries all of the existing Account records for their existing unique identifiers (outside of the trigger loop) for comparison
  • Then, for each record in Trigger.new:
    • Generates a new code in the format we need
    • Compares it against all of the existing codes (from the Account query outside the loop)
      • If it's unique:
        • update the Account in Trigger.new
        • Add the code to the full Account query list so it’s included in the next comparison
      • If it's not unique, re-generate the code and repeat the process
This has worked well, but we've exceeded 50k Account records in our org, and the initial query to obtain all of the codes to check against is now failing. Since our unique code does contain the first letter of the Account name, I considered adding a WHERE clause to the Account query… but since that query has to be outside of the Trigger.new loop, I don’t believe that will work either. And, since the 50k limit is per transaction and not per query, splitting the Account query into multiple queries likely will not work either.

Any suggestions for an approach to this issue? Much appreciated. Thank you.

-Greg