Skip to main content
Want to auto generate unique ID with 2-digit Statecode, 4 random digits.

eg- Unique Id = AK1234 for Alaska
1 answer
  1. Sep 18, 2019, 12:43 PM
    Hi Shiva,

    Greetings to you!

    Here is example to create unique id with state code.Use below code to solve your problem.

    And you can modify accordingly

     

    public class CreateAutoNumber {

        public static string autoNumber(){

            Integer len=5;

            String str = string.valueof(Math.abs(Crypto.getRandomLong()));

            String randomNumber = str.substring(0, len);

            String State = 'AK';

            String StateCode = State + randomNumber;

            system.debug('Random Number-' + randomNumber);

            system.debug('StateCode-' + StateCode);

            return randomNumber;

        }

    }

    I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

    Thanks and Regards,

    Deepali Kulshrestha

    www.kdeepali.com

     
0/9000