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
SaranshSaransh 

Why value of AccountId is coming as NULL in below code.(Below code I have created just for testing purpose)

Hi, I am just checking if custom component work the way I want it to be. Please if anyone of you can guide me why 
//Account Visualforce Page

<apex:page standardController="Account" extensions="multiCycle" showHeader="false" sidebar="false">
<style>
td
{
vertical-align:top;
}
</style>
<apex:form >
 <apex:panelGrid columns="2" id="theGrid" border="1" rules="group" width="100%">
    <apex:pageBlock title="Accounts">
    <apex:pageBlockSection >
        <apex:pageBlockTable width="" value="{!lstAccount}" var="Account">
                <apex:column >
                <apex:facet name="header">Name</apex:facet>
                    <apex:actionSupport event="onclick" reRender="getContactId">
                        {!Account.name}
                        <apex:param value="{!AccountId}" name="ParamAccId"/>
                    </apex:actionSupport>
                </apex:column>            
            <apex:column value="{!Account.phone}"/>
            <apex:column value="{!Account.Industry}"/>
            <apex:column value="{!Account.type}"/>
        </apex:pageBlockTable>
    </apex:pageBlockSection>
</apex:pageBlock>

<c:contacts getContact="{!$CurrentPage.Parameters.ParamAccId}" id="getContactId"> </c:contacts>

</apex:panelGrid>
</apex:form>

</apex:page>

//multiCycle apex class

public with sharing class multiCycle {

public list<account> lstAccount{get;set;}
public String AccountId{get;set;}

    public multiCycle(ApexPages.StandardController controller) {
        lstAccount = new list<Account>();
        AccountId = '';
        lstAccount = [select id,name,phone,type,Industry from Account limit 3];
    }

}


//Component "Contacts"

<apex:component controller="getContacts" allowDML="true">
<apex:attribute name="getContact" assignTo="{!getAccountId}"  type="String" description="Provide Account Id"/>
<apex:pageBlock title="Contacts" >
    <apex:pageBlockSection>
        <apex:pageBlockTable value="{!lstofcontacts}" var="contact">
            <apex:column value="{!contact.name}"/>
            <apex:column value="{!contact.Account.name}"/>
            <apex:column value="{!contact.Phone}"/>
            <apex:column value="{!contact.Email}"/>
        </apex:pageBlockTable>
    </apex:pageBlockSection>
</apex:pageBlock>
</apex:component>

//Component Class

public class getContacts{

public string getAccountId{get;set;}
public list<contact> lstofcontacts{get;set;}

public getcontacts(){
    lstofcontacts = new list<Contact>();

    system.debug('Debug AccountID = '+getAccountId); //getAccountId is coming as NULL here.

    if(!String.isEmpty(getAccountId))
            {
                lstofcontacts = [Select id,name,account.name,AccountId,phone,email 
                                            from contact 
                                            where AccountId =:getAccountId];
            } 
}

}



 
KaranrajKaranraj
In your apex controller you are setting the AccountId as null. If you want to pass Account ID as parameter just try the below code
 
<apex:page standardController="Account" extensions="multiCycle" showHeader="false" sidebar="false">
<style>
td
{
vertical-align:top;
}
</style>
<apex:form >
 <apex:panelGrid columns="2" id="theGrid" border="1" rules="group" width="100%">
    <apex:pageBlock title="Accounts">
    <apex:pageBlockSection >
        <apex:pageBlockTable width="" value="{!lstAccount}" var="Account">
                <apex:column >
                <apex:facet name="header">Name</apex:facet>
                    <apex:actionSupport event="onclick" reRender="getContactId">
                        {!Account.name}
                        <apex:param value="{!Account.Id}" name="ParamAccId"/>
                    </apex:actionSupport>
                </apex:column>            
            <apex:column value="{!Account.phone}"/>
            <apex:column value="{!Account.Industry}"/>
            <apex:column value="{!Account.type}"/>
        </apex:pageBlockTable>
    </apex:pageBlockSection>
</apex:pageBlock>

<c:contacts getContact="{!$CurrentPage.Parameters.ParamAccId}" id="getContactId"> </c:contacts>

</apex:panelGrid>
</apex:form>

</apex:page>

Remove the accountid variable from your apex class
 
public with sharing class multiCycle {

public list<account> lstAccount{get;set;}

    public multiCycle(ApexPages.StandardController controller) {
        lstAccount = new list<Account>();
        lstAccount = [select id,name,phone,type,Industry from Account limit 3];
    }

}

 
SaranshSaransh
Hi Karan,

I was actually getting NULL account Id in my component's class. I know why it was happening because Ajax reRender of component(Contacts) will not going to fire constructor of my Component's class again because that one was already called during my initial page load. Can you suggest other way around, like calling a seperate method on Ajax reRender to this component so that it could fetch all contacts of an Account on Account name Click event.