In this article we will do some workaround to generate a Random Non Sequential number using trigger.
We will create a after insert trigger on Lead object to generate the random number value. Please follow below steps :
Step 1 : Create a custom number field to store the Random ID
Step 2: Create a custom auto number field with format like 00001
Step 3: Create a after insert trigger as shown below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | trigger generateRandomId on Lead(after insert) { List<Lead> leadList = [SELECT Id,AutoId__c FROM Lead where Id in:Trigger.New]; for(Lead l: leadList) { if(trigger.isAfter && trigger.isInsert){ String str = string.valueof(Math.abs(Crypto.getRandomLong())); String randomNumber1 = str.substring(0, 4); str = string.valueof(Math.abs(Crypto.getRandomLong())); String randomNumber2 = str.substring(0, 4); l.Sorting_Variable__c= randomNumber1+l.AutoId__c+randomNumber2; } } update leadList; } |
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
0 Comments