Activity Forums Salesforce® Discussions I want to generate a random string,which contain alphabet and number both,please help to write a code.

  • I want to generate a random string,which contain alphabet and number both,please help to write a code.

    Posted by Tanu on August 16, 2016 at 6:16 AM

    Hi All,

    I want to generate a random string of length 6 which contain alphabet between A-Z and number 0-9, so that I can use then as an OTP, so please help me in writing code.

    Himanshu replied 10 years ago 2 Members · 1 Reply
  • 1 Reply
  • Himanshu

    Member
    August 16, 2016 at 8:20 AM

    Hi Tanu,

    Use the below code:-

    Integer len = 6;
    Blob blobKey = crypto.generateAesKey(128);
    String key = EncodingUtil.convertToHex(blobKey);
    String pwd = key.substring(0,len);
    System.debug('************ '+pwd);

    Hope this helps you.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos