You need to sign in to do that
Don't have an account?
GeorgeP
Can I create a class from the value in a string?
Is it possible to create or instantiate a class of a type the is equal to the value of a String? So something along these lines;
//Normal code
private MynewClass MyNewClass;
MyNewClass = new myNewClass();
// Is something like this possible instead?
private MynewClass MyNewClass;
String tempString = 'MyNewClass';
MyNewClass = new tempString(); // ie somehow get the value of tempString' as the type of class to create.
cheers
George
//Normal code
private MynewClass MyNewClass;
MyNewClass = new myNewClass();
// Is something like this possible instead?
private MynewClass MyNewClass;
String tempString = 'MyNewClass';
MyNewClass = new tempString(); // ie somehow get the value of tempString' as the type of class to create.
cheers
George
There is Type class in apex, which could be helpful in your case, try something like:
Marek
All Answers
myNewClass should be MyNewClass above.
There is Type class in apex, which could be helpful in your case, try something like:
Marek
Here is my attempt:
private Object PricingClass;
String tempString = 'CuaBundledCSVProcessing';
Type t = Type.forName(tempString);
PricingClass = new t.newInstance();
Its falling over on the last line, the error is: Invalid type: t.newInstance
any idea?
cheers
Next question though is how do i access a method of the class type that I have instantiated using this approach?
oh, I didn't realize that.. you still have to cast return value of .getInstance method to proper type: I'm not aware of any other way to do that, sorry :(
This was the code I was hoping to run:
// here the Pricing_class__c variable has the name of anyone of 4 classes that all have the same methods but do different things depending on the type //of contract they are for.
Object PricingClass;
List<Offer_Type_Of_Contract_Mapping__c> tocList = [Select Pricing_Class__c from Offer_Type_Of_Contract_Mapping__c
where Type_Of_Contract__c = :typeOfContract limit 1];
Type classType = Type.forName(tocList.get(0).Pricing_Class__c);
PricingClass = classType.newInstance();
errorNum = PricingClass.getErrorNum();
Was hoping toprevent this sort of thing a bit later in the program:
if (typeOfContract == 'CUA Bundled') {
myList = CuaBundledPricing.validateData(csvRecordData, OppId, OppID_c, lineNum, errorNum);
errorNum = CuaBundledPricing.getErrorNum();
if (noErrors) noErrors = CuaBundledPricing.getNoErrors();
if (!acctnumberspresent) acctnumberspresent = CuaBundledPricing.getAcctNumbersPresent();
}
if (typeOfContract == 'CUA Unbundled') {
myList = CuaUnBundledPricing.validateData(csvRecordData, OppId, OppID_c, lineNum, errorNum);
errorNum = CuaUnBundledPricing.getErrorNum();
if (noErrors) noErrors = CuaUnBundledPricing.getNoErrors();
if (!acctnumberspresent) acctnumberspresent = CuaUnBundledPricing.getAcctNumbersPresent();
}
etc, ie repeat above code for each contract type that needs its own dedicated but similar class
oh, well, back to the drawing board!
cheers