+ Start a Discussion
Matthew BirckbichlerMatthew Birckbichler 

VF Extension Class, trouble with accessing current records variables

Hey guys, I made a VF page with a Controller Extension.
public class createOpportunityContactExtension {
    public final Opportunity_Contact__c oppCon;
    
    public createOpportunityContactExtension(ApexPages.StandardController stdController){
        this.oppCon = (Opportunity_Contact__c)stdController.getRecord();
        oppCon.Opportunity__c = apexpages.currentpage().getparameters().get('oppId');
    }
    
    public static Boolean lookup = true;
    public static Boolean pickList = false;
    
    public static void toggleContact(){
        IF(oppCon.Account__c != null){
            createOpportunityContactExtension.lookup = false;
            createOpportunityContactExtension.pickList = true;
        }
    }  
      
    public Boolean viewLookup{get{return lookup;}}
    public Boolean viewPickList{get{return pickList;}}
    
}
I cannot seem to figure out how to access the current Opportunity_Contact__c (oppCon), to check if the Account__c field is null
public static void toggleContact(){
        IF(oppCon.Account__c != null){
            createOpportunityContactExtension.lookup = false;
            createOpportunityContactExtension.pickList = true;
        }
    }
Deepak GulianDeepak Gulian
IF you are accessing Account__c field on VF page then only you able to access it inside apex class, else you need to get Account__c value by query.
Amit Chaudhary 8Amit Chaudhary 8
You need to do below :-
1) Add getter setter for controller record
2) Query your field in controller like below
if(oppCon.Id != null)
		{
			// PLease add all required field you need in Vf page and controller
			oppCon = [select id,Opportunity__c,Account__c from Opportunity_Contact__c where id =:oppCon.Id limit 1] ; 
		}



PLease try below code :-
public class createOpportunityContactExtension 
{
    public Opportunity_Contact__c oppCon {get;set;} // If you need to access on VF 
    public static Boolean lookup = true;
    public static Boolean pickList = false;
    public Boolean viewLookup{get{return lookup;}}
    public Boolean viewPickList{get{return pickList;}}
    
    public createOpportunityContactExtension(ApexPages.StandardController stdController)
	{
        this.oppCon = (Opportunity_Contact__c)stdController.getRecord();
		if(oppCon.Id != null)
		{
			// PLease add all required field you need in Vf page and controller
			oppCon = [select id,Opportunity__c,Account__c from Opportunity_Contact__c where id =:oppCon.Id limit 1] ; 
		}
        oppCon.Opportunity__c = apexpages.currentpage().getparameters().get('oppId');
    }
    
    
    public static void toggleContact()
	{
        IF(oppCon.Account__c != null)
		{
            createOpportunityContactExtension.lookup = false;
            createOpportunityContactExtension.pickList = true;
        }
    }  
      
    
}

Please let us know if this will help you