function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC_DevloperSFDC_Devloper 

How To pass AccountId from Apex class to Vf page

Hi All,

VisualForce Page:
<apex:page standardController="Account" showHeader="true"
tabStyle="account" >
<style>
.activeTab {background-color: #236FBD; color:white;
background-image:none}
.inactiveTab { background-color: lightgrey; color:black;
background-image:none}
</style>
<apex:tabPanel switchType="client" selectedTab="tabdetails"
id="AccountTabPanel" tabClass='activeTab'
inactiveTabClass='inactiveTab'>
<apex:tab label="Details" name="AccDetails" id="tabdetails">
<apex:detail relatedList="false" title="true"/>
</apex:tab>
<apex:tab label="Contacts" name="Contacts" id="tabContact">
<apex:relatedList subject="{!account}" list="contacts" />
</apex:tab>
<apex:tab label="Opportunities" name="Opportunities"
id="tabOpp">
<apex:relatedList subject="{!account}"
list="opportunities" />
</apex:tab>
<apex:tab label="Open Activities" name="OpenActivities"
id="tabOpenAct">
<apex:relatedList subject="{!account}"
list="OpenActivities" />
</apex:tab>
<apex:tab label="Notes and Attachments"
name="NotesAndAttachments" id="tabNoteAtt">
<apex:relatedList subject="{!account}"
list="CombinedAttachments" />
</apex:tab>
</apex:tabPanel>
</apex:page>

We need to specify the ID of a particular account :https://na1.salesforce.com/apex/tabbedAccount?id=001D000000IRt53.

After you add in an account ID,

My page should display as follows:

User-added image


here I dnt want pass url like this:https://na1.salesforce.com/apex/tabbedAccount?id=001D000000IRt53.

here I want pass AccountId from Apex class to VF Page.

Thanks
Best Answer chosen by SFDC_Devloper
SFDC_DevloperSFDC_Devloper
Hi Vinth and laks,

Thanks for immediate responce..now  its working fine no issues.

I got error like :System.QueryException: List has no rows for assignment to SObject

I fixed thir error :
public class tabPanelExt {

  private final Account pos;
  public String id{get;set;}
  public Id posId       {get;set;}

  public tabPanelExt(ApexPages.StandardController controller)
  {
   
   pos=(Account)controller.getRecord();
   List<Account> acc = [select id from Account where id=:pos.id];
   System.debug('***acc ***'+acc);
   if(acc.size()>0)
   id = acc[0].id;    
   System.debug('***id***'+id);
  posId= controller.getRecord().Id; 

  }
   
     public PageReference recordDetails()
     {
   PageReference requestPage = new pagereference('/apex/tabPanelPage?id='+id);
    System.debug('***requestPage ***'+requestPage );
    requestPage.setRedirect(true);
    return null;
    

}

     

}

Thanks,
Rockzz

All Answers

Vinit_KumarVinit_Kumar
How are you getting your record id in Apex class.Is it through ApexPages.standardController or something else ??



SFDC_DevloperSFDC_Devloper
ApexPages.standardController
Vinit_KumarVinit_Kumar
Then,you can store it in variable and show it on the page,something like below :-

public String id{get;set;}

// In your constructor
id= con.getRecord().id; // con= instance of ApexPages.standardController
SFDC_DevloperSFDC_Devloper
Hi,
  
  I am getting error like this:Id value is not valid for the Account standard controller

public class tabPanelExt {

  private final Account pos;
  public String id{get;set;}
  
  public tabPanelExt(ApexPages.StandardController controller)
  {
   
   id=controller.getRecord().Id;
   System.debug('***id***'+id);
  }
   
     public PageReference recordDetails()
     {
    PageReference requestPage = new pagereference('/apex/tabPanelPage?id='+id);
    System.debug('***requestPage ***'+requestPage );
    requestPage.setRedirect(true);
    return requestPage;

}

     

}

Thanks
Vinit_KumarVinit_Kumar
Try below code :- 
public class tabPanelExt {

  private final Account pos;
  public String id{get;set;}
  
  public tabPanelExt(ApexPages.StandardController controller)
  {
   
   id=(Account)stdController.getRecord().id;
   System.debug('***id***'+id);
  }
   
     public PageReference recordDetails()
     {
    PageReference requestPage = new pagereference('/apex/tabPanelPage?id='+id);
    System.debug('***requestPage ***'+requestPage );
    requestPage.setRedirect(true);
    return requestPage;

}

     

}

SFDC_DevloperSFDC_Devloper
Hi Vinith..Thansk for immediate responce..

I  tried this way also but I am getting compile error like below attached image:

User-added image

below line:
id=(Account)stdController.getRecord().id;

Thanks
Vinit_KumarVinit_Kumar
Try doing this :-

change from 

id=(Account)stdController.getRecord().id;

to

id = controller.getId();
Vinit_KumarVinit_Kumar
or you can do this ,both should work :)

pos=(Account)stdController.getRecord();

id = pos.id;
SFDC_DevloperSFDC_Devloper
Hi vinith,

I tried this:id=controller.Id;

I am getting below compile error:
User-added image

And I triend this one also:
pos=(Account)stdController.getRecord();
id = pos.id;


I am getting below error:

User-added image



Thanks
Vinit_KumarVinit_Kumar
ok for first one it is not 

id=controller.Id;

it is 

id=controller.getId();

for 2nd one,try querying id field

id = [select id from Account where id=:pos.id].id;
SFDC_DevloperSFDC_Devloper
Hi Vinith..Thanks for your immediate responce..


I have tried both but still I  am facing same issue:

User-added image

Thanks
Vinit_KumarVinit_Kumar
Not sure why are not getting,try this one :-

public class tabPanelExt {

  private final Account pos;
  public String id{get;set;}
  
  public tabPanelExt(ApexPages.StandardController controller)
  {
   
   pos=(Account)stdController.getRecord();
   Account acc = [select id from Account where id=:pos.id limit 1];
   id = acc.id;    
   System.debug('***id***'+id);
  }
   
     public PageReference recordDetails()
     {
    PageReference requestPage = new pagereference('/apex/tabPanelPage?id='+id);
    System.debug('***requestPage ***'+requestPage );
    requestPage.setRedirect(true);
    return requestPage;

}

     

}

If you are still not getting,I would suggest to debug the logs and see what's happening there !!
lakslaks
Hi, 

Shouldn't you be doing pos=(Account)controller.getRecord(); and not 

pos=(Account)stdController.getRecord();

since controller is the name of the ApexPages.StandardController object.

Lakshmi.
SFDC_DevloperSFDC_Devloper
Hi Vinith,

  My Vf page not calling my extension class (tabPanelExt class ) and its method..

This is my debug log:
User-added image

can you please check my VF page ..


Thanks
SFDC_DevloperSFDC_Devloper
This is my VF Page:

<apex:page standardController="Account"  extensions="tabPanelExt" action="{!recordDetails}">
     <style>
         .activeTab{background-color:#236FBD; color:white; background-image:none}
         .inactiveTab{background-color:lightgrey; color:white; background-image:none}
     </style>
    
     <apex:tabPanel switchType="client" selectedTab="tabdetails" id="AccountTabPanel" tabClass="activeTab" inactiveTabClass="inactiveTab">
         <apex:tab label="Details" name="AccDetails" id="tabdetails" style="font-color:blue">
             <apex:detail relatedList="false"/>
         </apex:tab>
         <apex:tab label="Contats" name="Contacts" id="tabContact">
             <apex:relatedList subject="{!account}" list="Contacts" />         
         </apex:tab>
         <apex:tab label="Opportunitis" name="Opportunities" id="tabOpportunity">
             <apex:relatedList subject="{!account}" list="Opportunities"/>
         </apex:tab>
         <apex:tab label="Open Activites" name="OpenActivities" id="tabOpenact">
             <apex:relatedList subject="{!account}" list="OpenActivities"/>
         </apex:tab>
         <apex:tab label="Notes and Attachments" name="NotesAndAttachments" id="tabAttchments">
             <apex:relatedList subject="{!account}" list="CombinedAttachments"/>
         </apex:tab>
     </apex:tabPanel>
    
</apex:page>


Vinit_KumarVinit_Kumar
I think there should be another log being generated.This log is only for viewstate.Check the debug logs again.

Also,Lakshmi is right,I just saw , you should be using 

pos=(Account)controller.getRecord();
lakslaks
Hi,

From the code above I understand that what you want to do is load the page with a tabbed view of the related lists.

You wouldn't need to do a redirect in recordDetails() method called from the action tag for the same.

You can change the code as given below to make it work :

public PageReference recordDetails()
    { 
        PageReference requestPage = new PageReference('/apex/tabPanelPage?id='+id);
        System.debug('***requestPage ***'+requestPage );
      
        return null;
    }

If the action method returns null, the page simply refreshes.


Lakshmi.
SFDC_DevloperSFDC_Devloper
Hi Laks,

public class tabPanelExt {

  private final Account pos;
  public String id{get;set;}
  
  public tabPanelExt(ApexPages.StandardController controller)
  {
   
   pos=(Account)controller.getRecord();
   List<Account> acc = [select id from Account where id=:pos.id limit 1];
   System.debug('***acc ***'+acc);
   if(acc.size()>0)
   id = acc[0].id;    
   System.debug('***id***'+id);
  }
   
     public PageReference recordDetails()
     {
    PageReference requestPage = new pagereference('/apex/tabPanelPage?id='+id);
    System.debug('***requestPage ***'+requestPage );
    //requestPage.setRedirect(true);
    return null;

}

     

}

still i am getting same error:Id value is not valid for the Account standard controller
Vinit_KumarVinit_Kumar
Can you post your debug log as what's coming in these lines:-

System.debug('***acc ***'+acc);

and

System.debug('***id***'+id);
lakslaks
Hi,

Hope you are specifying the correct page name in the address bar -
:https://na1.salesforce.com/apex/tabPanelPage?id=<Valid account id>     

(Go to the account which you want to view as tabbed and get the account id from the url)

Lakshmi.
SFDC_DevloperSFDC_Devloper
Hi Vinth and laks,

Thanks for immediate responce..now  its working fine no issues.

I got error like :System.QueryException: List has no rows for assignment to SObject

I fixed thir error :
public class tabPanelExt {

  private final Account pos;
  public String id{get;set;}
  public Id posId       {get;set;}

  public tabPanelExt(ApexPages.StandardController controller)
  {
   
   pos=(Account)controller.getRecord();
   List<Account> acc = [select id from Account where id=:pos.id];
   System.debug('***acc ***'+acc);
   if(acc.size()>0)
   id = acc[0].id;    
   System.debug('***id***'+id);
  posId= controller.getRecord().Id; 

  }
   
     public PageReference recordDetails()
     {
   PageReference requestPage = new pagereference('/apex/tabPanelPage?id='+id);
    System.debug('***requestPage ***'+requestPage );
    requestPage.setRedirect(true);
    return null;
    

}

     

}

Thanks,
Rockzz

This was selected as the best answer
SFDC_DevloperSFDC_Devloper
Hi Vinth ,

  it's really helpful for me thanks for your Support .

Thanks,
Rockzz