• Peter Thurston 12
  • NEWBIE
  • 25 Points
  • Member since 2014

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
I have a method and a constructor. I know how to call a method but not a constructor. I would think that it would be like the below. Any suggestions? I want to place my generaterandomstring into my object name. Everytime the record saved it would insert the logic for the constructor in the name

public PageReference PCR() {

         Object__c newObject;
        
         object = [select id, Name, number__c from Object__cwhere id = :object.id];

    //     newObject.name =  RandomNumbersMethod();
         newObject.name = generateRandomString();
                 insert newObject;


}

My Method

public double RandomNumbersMethod()
{
return Math.random() * 10;
}

My Constructor 

public String generateRandomString(Integer len) {
 
    final String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
    String randStr = '';

    while (randStr.length() < len) {
       Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), 62);
       randStr += chars.substring(idx, idx+1);
    }
    return randStr;
  
}
I have a method and a constructor. I know how to call a method but not a constructor. I would think that it would be like the below. Any suggestions? I want to place my generaterandomstring into my object name. Everytime the record saved it would insert the logic for the constructor in the name

public PageReference PCR() {

         Object__c newObject;
        
         object = [select id, Name, number__c from Object__cwhere id = :object.id];

    //     newObject.name =  RandomNumbersMethod();
         newObject.name = generateRandomString();
                 insert newObject;


}

My Method

public double RandomNumbersMethod()
{
return Math.random() * 10;
}

My Constructor 

public String generateRandomString(Integer len) {
 
    final String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
    String randStr = '';

    while (randStr.length() < len) {
       Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), 62);
       randStr += chars.substring(idx, idx+1);
    }
    return randStr;
  
}