function readOnly(count){ }
Don't have an account?
Search for an answer or ask a question of the zone or Customer Support.
You need to sign in to do that
Sign in to start searching questions
Signup for a Developer Edition
Sign in to start a discussion
trigger UniqueCodeGenerator on Account (before insert) { for(Account acc : trigger.new){ Integer len = 10; Blob blobKey = crypto.generateAesKey(128); String key = EncodingUtil.convertToHex(blobKey); String pwd = key.substring(0,len); System.debug('************ '+pwd); acc.Unique_Code__c = pwd; } }
This Can be done by 2 ways-
1. With Apex and
2. WIth Formula field
If you want to do this by apex than here is the code -
First Solution(Apex Code) :
String hashString = '1000' + String.valueOf(Datetime.now().formatGMT('yyyy-MM-dd HH:mm:ss.SSS'));
Blob hash = Crypto.generateDigest('MD5',
Blob.valueOf(hashString));
String hexDigest = EncodingUtil.convertToHex(hash);
system.debug('##########' + hexDigest );
Second Solution(Formula) :
LEFT(TEXT(
SQRT(
(VALUE(
(LEFT(RIGHT(TEXT( CreatedDate ),6),2))&
TEXT(DAY(DATEVALUE(CreatedDate)))&
TEXT(MONTH(DATEVALUE(CreatedDate)))&
TEXT(YEAR(DATEVALUE(CreatedDate)))&
(LEFT(RIGHT(text(CreatedDate),6),2))) )*10 ) ) ,3 ) &
LEFT(Your_FIeld, 3)
Thanks
All Answers
This Can be done by 2 ways-
1. With Apex and
2. WIth Formula field
If you want to do this by apex than here is the code -
First Solution(Apex Code) :
String hashString = '1000' + String.valueOf(Datetime.now().formatGMT('yyyy-MM-dd HH:mm:ss.SSS'));
Blob hash = Crypto.generateDigest('MD5',
Blob.valueOf(hashString));
String hexDigest = EncodingUtil.convertToHex(hash);
system.debug('##########' + hexDigest );
Second Solution(Formula) :
LEFT(TEXT(
SQRT(
(VALUE(
(LEFT(RIGHT(TEXT( CreatedDate ),6),2))&
TEXT(DAY(DATEVALUE(CreatedDate)))&
TEXT(MONTH(DATEVALUE(CreatedDate)))&
TEXT(YEAR(DATEVALUE(CreatedDate)))&
(LEFT(RIGHT(text(CreatedDate),6),2))) )*10 ) ) ,3 ) &
LEFT(Your_FIeld, 3)
Thanks
Greetings to you!
Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas