Trigger to get 7 digits Random ID in Salesforce



In this article I am creating a trigger to get a 7 digits Random ID and store it in my Contact record.

Please follow the code below : 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
trigger generateRandom on Contact (before insert) {
    for(Contact con: Trigger.new)
    {
        if(trigger.isBefore && trigger.isInsert){
            String str = string.valueof(Math.abs(Crypto.getRandomLong()));
            String randomNumber = str.substring(0, 7);
            con.Random_ID__c= randomNumber;
        }
    }
}


If you have any question please leave a comment below.
If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Keep Coding 😊


Post a Comment

0 Comments