Activity Forums Salesforce® Discussions How To Convert 15 To 18 Digit Id Using Apex In Salesforce?

  • MOHIT

    Member
    May 11, 2020 at 2:25 pm

    String idSalesforce = 'a0D30000001n7Pi'; // change with your 15 chars ID
    if(idSalesforce.length() == 18){
    system.debug('Input Id is 18 char');
    }
    else if(idSalesforce.length() != 15){
    system.debug('Input Id error');
    }
    else{
    String suffix = '';
    String idOut= '';
    String InChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345';
    for(Integer i = 0; i<3; i++){
    Integer flags = 0;
    for(Integer j = 0; j<5; j++){
    String c = idSalesforce.substring(i*5+j, i*5+j+1);
    if((c.compareTo('A')>=0) && (c.compareTo('Z')<=0)){
    flags += 1 << j;
    }
    }
    suffix = suffix + InChars.substring(flags, flags+1);
    }
    idOut = idSalesforce + suffix;
    system.debug('Id Output 15: '+idSalesforce);
    system.debug('Id Output 18: '+idOut);
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos