I have trigger which generates random String whenever a record is created. whenever i try to create a record (On clicking the save button), the following error is popping System.LimitException: Apex CPU time limit exceeded. The code that i am using within the trigger is method that i am using within the trigger is take from a similar post right here in StackExchange.
trigger ITAssetTrigger on IT_Asset_Management__c (before insert) {
for(IT_Asset_Management__c asset : Trigger.new){
asset.Asset_Id__c = generateRandomString(Datetime.now().getTime().intValue());
}
public static String generateRandomString(Integer len) {
final String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
String randStr = '';
while (randStr.length() < len) {
Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), chars.length());
randStr += chars.substring(idx, idx+1);
}
return randStr;
}
}
Can you Help me in updating the code so that i wont get this issue again
Thank you.