1

Prepare to cringe this is some poor implementation but in my defense I don't have any experience in this. I suppose this is a wrong way to use a constructor but I've been trying to achieve a structure yet again.

What I want: According to a flag which is an account field, I want to either fill values to the attributes of ObjA class (which carry the attributes of the Base class too) or fill the attributes of the ObjB class (which carry the attributes of the Base class similarly).

The problem is I could not access the ObjA/ObjB attributes I was getting an error Variable does not exist Which is understandable since when I am creating the instance I do not specify the subclasses respectively.

However I want the structure to start from this base field in the BaseCls class, I don't want any other key there.

For ObjA:

"base": {
  "id": "Haha1234",
  "name": "SomeName",
  "description": "SomeDesc",
  "objAFieldA": "aaa",
  "objAFieldB": "bbb",
  "objAFieldC": "ccc"
}

or for ObjB

"base": {
  "id": "Haha1234",
  "name": "SomeName",
  "description": "SomeDesc",
  "objBFieldA": "aaa",
  "objBFieldB": "bbb",
  "objBFieldC": "ccc"
}
public class BaseCls {
 public Base base; 

 public BaseCls(Boolean flag){
   Boolean isObjA;
   isObjA = flag;
   if(flag == true){
      this.base = new ObjA();     
   }else{
      this.base = new ObjB(); 
   }
 }

 public abstract class Base {
    public String id;
    public String name;
    public String description;
 }

 public class ObjA extends Base {
    public String objAFieldA;
    public String objAFieldB;
    public String objAFieldC;
 }

 public class ObjB extends Base {
    public String objBFieldA;
    public String objBFieldB;
    public String objBFieldC;
 }
}

Looping on some fields...

Boolean flag = false;
if(acc.flag__c == true){
  flag = true;
}
BaseCls body = new BaseCls(flag)
body.base.id = acc.Id;
body.ObjAFieldA = acc.Sth__c; //clearly not working

So done with wrappers already, help would be highly appreciated once again!

Edit x1 If public Base base was of type ObjA or ObjB I'd be able to access the fields alright but I do not want extra attributes that will unavoidably act as keys in the json. I'm starting to think it isn't possible this way.

1

Edited - I thing I know what you are asking.

This is as close as I can get without making a factory class.

public virtual class TestBase implements BaseInterface {
  public String id;
  public String name;
  public String description;

  public static BaseInterface getTestBase(Boolean flag){
    Boolean isObjA;
    isObjA = flag;
    if(flag == true){
       return new ObjA();     
    }else{
       return new ObjB(); 
    }
  }

 public class ObjA extends TestBase {
    public String objAFieldA;
    public String objAFieldB;
    public String objAFieldC;
 }

 public class ObjB extends TestBase {
    public String objBFieldA;
    public String objBFieldB;
    public String objBFieldC;
 }

 public interface BaseInterface{}
}

Invoked by this:

Account acc = [SELECT Id, Name From Account LIMIT 1];
TestBase.ObjA base = (TestBase.ObjA)TestBase.getTestBase(true);
base.objAFieldA = acc.Name;
base.id = acc.Id;

I've made the base a proper outer class and have created a creator class - I've never figured out if it's possible to have a constructor that can return different versions.

You could just move this logic out of the class and call externally also.

Not sure if this is exactly what you are after, but hopefully it gets you closer.

2
  • Hello and thanks for taking the time to reply! I had been trying on my own again before I try your implementation. Which by the way seems to be working I just need to find a way to get the key base in my structure cuz this way I only get the key-values of the attributes which is satisfactory enough I just need the final touch for now :P Thanks again I'd have never thought of the interface trick. Jun 15 '21 at 11:06
  • Glad it helped! If you ever figure out some sort of "Self factory" technique, let me know too. Jun 15 '21 at 18:21

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.