0

What needs to be added (an SOQL query perhaps) in the 'else if' statement which checks the value within the c.Name column and tells if the generated random string already exists, and if so add break/continue to re run the generator to generate a new random string.

trigger tokenGenerator on sObject__c (before insert) {
    for( sObject__c c: Trigger.new ) {
        final String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        String a = '';
        while (a.length() < 4) {
            Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), chars.length());
            a += chars.substring(idx, idx+1);
        }
        if(c.Name == ''){
            c.Name = a;          
        }
        else if(a == **********){
            ******break;/continue; 
        }
    }   
}

*****else if(a == matches any value inside the c.Name column){ then re run the code generator, if not continue}***** how to write this part

0

You can add a unique prefix to avoid duplicate name. For this use datetime class method.

//Use DateTime method to time in millisecond
DateTime dt = DateTime.now();
Strinf str = String.valueOf(dt.getTime());

//Now add this string as prefix so it will not match with existing record and for current process record you can use set to keep track. You don't need SOQL for same.
Set<String> nameSet = new set<String>();
for( Certification__c c: Trigger.new ) {
        final String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        String a = '';
        while (a.length() < 4) {
            Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), chars.length());
            a += chars.substring(idx, idx+1);
        }
        a = str  + a;
        if(c.Name == ''){
            c.Name = a;          
        }
        else if(nameSet.contains(a)){
            ******break;/continue; 
        }
    }
  • you mean, c.Name.contains(a)? – Niveth Kumar Oct 12 '18 at 6:08
  • @NivethKumar nope the set to check you are not getting duplicate name in current record. – Tushar Sharma Oct 12 '18 at 6:10
  • as per my code, it should be c.Name.contain(a) figured it out thank you. Also how can I make the code run again within the same instance. cause now that the value is duplicating, do i add a break statement or a continue statement within the else if? – Niveth Kumar Oct 12 '18 at 6:18
  • @NivethKumar in case you want find a duplicate name , Just add 2 or random character and it will work. – Tushar Sharma Oct 12 '18 at 6:21
-1

Answering my own question, I had found a solution to unique random number generation.

token class code:

public class token {
    public static String createToken(){
        final String chars = 'BCDEFGH';
        String token = '';
        while (token.length() < 4) {
            Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), chars.length());
            token += chars.substring(idx, idx+1);
        }
        return token;
    }
}

tokenGenerator trigger code:

trigger tokenGenerator on your_object(before insert) {
    List<your_object> object1 = new List<your_object>();
    object1 = [SELECT Id, Name FROM <your object>];

    Set<String> usedTokens = new Set<String>();
    for (your_object x : object1) {
        usedTokens.add(x.Name);
    }

    for (your_object x : Trigger.NEW) {
        String tokenToCheck = token.createToken();

        while (usedTokens.contains(tokenToCheck)) {
            tokenToCheck = token.createToken();
        }
        x.Name = 'WSO2'+tokenToCheck;
        usedTokens.add(x.Name); 
    }
}

above: replace "your object" with your custom object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.