You need to sign in to do that
Don't have an account?
Bob.390672021994282E12
How to Call a Constructor
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;
}
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;
}
should be:
newObject.name = generateRandomString(<insert the length of the random string you want here>);
so something like:
newObject.name = generateRandomString(10); // should return a 10 character random string
All Answers
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_constructors.htm
should be:
newObject.name = generateRandomString(<insert the length of the random string you want here>);
so something like:
newObject.name = generateRandomString(10); // should return a 10 character random string
Thanks