+ Start a Discussion
Jim BoudreauxJim Boudreaux 

Calling a No Argument Constructor from Another Constructor

I am writing a custom Class. It has a bunch of properties. In one constructor, when I pass one argument, an ID. It fills 1/3 of the properties. in another constructor, I pass a record ID as well as an associated record's ID as arguments. The constructor fills all the properties.

My question is this: Is there a way in the constructor with two arguments, to just call the one argument constructor and thus have 1/3 of the properties filled for free? As of this writing I am accomplishing this by instantiating a new instance of the class, and using dot notation to manually fill the properties that the one argument constructor filled. This seems rather pointless and redundant.

 

Here is my code as is:

public class MyFloor2{
        //Properties
        public floor__c                   floor     {get; set;}
        public attachment                 a         {get; set;}
        public string                     floorplan {get; set;}
        public blob                       file      {get; set;}
        public boolean                    noFA      {get; set;}
        public boolean                    noFX      {get; set;}
        public boolean                    noSH      {get; set;}
        public boolean                    noFP      {get; set;}
        public account_c__c               account   {get; set;}
        public list<FA_Device__c>         fadevices {get; set;}
        public list<FA_Result__c>         faresults {get; set;}
        public list<FX_Result__c>         fxresults {get; set;}
        public list<SH_Result__c>         shresults {get; set;}
        public list<SObject>              results   {get; set;}
        public list<floor_tenant__c>      tenants   {get; set;}
        
        //Constructors         
        public MyFloor2(){
        }
        
        public MyFloor2(string id){
            //This code utilizes a class I wrote that mimics the SQL Select * functionality. Suffice to say it represents a Select All query 
            string floorfields = new selectall('floor__c').allfields;
            string fadevicequery = 'Select '+ new selectall('fa_device__c').allfields +' from fa_devices__r order by device_number__c';
            string tenantquery   = 'Select '+ new selectall('floor_tenant__c').allfields +' from floor_tenants__r order by name';
            string floorquery ='Select '+floorfields+', ('+fadevicequery+'), ('+tenantquery+'), (Select Id From Attachments Where name = \'floorplan.jpg\') From floor__c where id=:id';
            
            floor = database.query(floorquery);
            string aid = floor.account__c;
            account = database.query(new selectall('account_c__c').soql+' where id=:aid');
            fadevices = floor.fa_devices__r;
            noFA = fadevices.isempty();
            tenants = floor.floor_tenants__r;
            if(floor.Attachments.isempty()){
                noFP = true;
            }else{
                a = floor.Attachments;
                floorplan = a.id;                
            }
        }
        
        public MyFloor2(string id, string sid){
            //This code utilizes a class I wrote that mimics the SQL Select * functionality. Suffice to say it represents a Select All query            
            string faresultquery = 'Select '+ new selectall('fa_result__c').allfields +' from fa_results1__r where system_inspection__c =:sid order by device_number__c';
            string fxresultquery = 'Select '+ new selectall('fx_result__c').allfields +' from fx_results__r where system_inspection__c =:sid order by number__c';
            string shresultquery = 'Select '+ new selectall('sh_result__c').allfields +' from sh_results__r where system_inspection__c =:sid order by device_number__c';
            string floor ='Select ('+faresultquery+'), ('+fxresultquery+'), ('+shresultquery+') From floor__c where id=:id';
            MyFloor2 myf2 = new MyFloor2(id);
            floor__c f = database.query(floor);
           
            this.floor = myf2.floor;
            this.fadevices = myf2.fadevices;
            this.tenants = myf2.tenants;
            this.floorplan = myf2.floorplan;
            this.account = myf2.account;
            
            this.faresults = f.fa_results1__r;
            this.fxresults = f.fx_results__r;
            this.shresults = f.sh_results__r;
            this.noFA = this.faresults.isempty();
            this.noFX = this.fxresults.isempty();
            this.noSH = this.shresults.isempty();
        }
        //Methods
        //....

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Jim BoudreauxJim Boudreaux

 

Scratch that. I figured out what to do almost immediately after clicking the "Submit" button.
this(ID); as the first line int he two argument constructor fills the one argument constructor's fields.

 

All Answers

Jim BoudreauxJim Boudreaux

 

Scratch that. I figured out what to do almost immediately after clicking the "Submit" button.
this(ID); as the first line int he two argument constructor fills the one argument constructor's fields.

 

This was selected as the best answer
CodeBakerCodeBaker

Another options is to remove all the code from the one-argument constructor and place it into a private "setup" method. Then call the "setup" method from both the two-argument constructor or the one-argument constructor.

 

 

public MyFloor2(string id){
	setup(id);
}
        
public MyFloor2(string id, string sid){
	//This code utilizes a class I wrote that mimics the SQL Select * functionality. Suffice to say it represents a Select All query            
	string faresultquery = 'Select '+ new selectall('fa_result__c').allfields +' from fa_results1__r where system_inspection__c =:sid order by device_number__c';
	string fxresultquery = 'Select '+ new selectall('fx_result__c').allfields +' from fx_results__r where system_inspection__c =:sid order by number__c';
	string shresultquery = 'Select '+ new selectall('sh_result__c').allfields +' from sh_results__r where system_inspection__c =:sid order by device_number__c';
	string floor ='Select ('+faresultquery+'), ('+fxresultquery+'), ('+shresultquery+') From floor__c where id=:id';
	
	setup(id);
	
	floor__c f = database.query(floor);
	
	this.faresults = f.fa_results1__r;
	this.fxresults = f.fx_results__r;
	this.shresults = f.sh_results__r;
	this.noFA = this.faresults.isempty();
	this.noFX = this.fxresults.isempty();
	this.noSH = this.shresults.isempty();
}

private void setup(string id){
	//This code utilizes a class I wrote that mimics the SQL Select * functionality. Suffice to say it represents a Select All query 
	string floorfields = new selectall('floor__c').allfields;
	string fadevicequery = 'Select '+ new selectall('fa_device__c').allfields +' from fa_devices__r order by device_number__c';
	string tenantquery   = 'Select '+ new selectall('floor_tenant__c').allfields +' from floor_tenants__r order by name';
	string floorquery ='Select '+floorfields+', ('+fadevicequery+'), ('+tenantquery+'), (Select Id From Attachments Where name = \'floorplan.jpg\') From floor__c where id=:id';
	
	floor = database.query(floorquery);
	string aid = floor.account__c;
	account = database.query(new selectall('account_c__c').soql+' where id=:aid');
	fadevices = floor.fa_devices__r;
	noFA = fadevices.isempty();
	tenants = floor.floor_tenants__r;
	if(floor.Attachments.isempty()){
		noFP = true;
	}else{
		a = floor.Attachments;
		floorplan = a.id;                
	}

This particular "setup" method may not have all the calls you need, but you get the idea.