2

Requirement:- In a visualforce Page Show list of contacts in pageblocktable. 2 command buttons in last column for Details and Remove.

One button to add new contact below pageblock table

A pageblockSection below to show the details. with save details and clear button

  1. If details button is clicked the details of contact to be shown in this pageblocksection.
  2. If remove button is clicked the contact in that row must be removed from database.
  3. If add new button is clicked Contact detail section shows up to add the details.
  4. When Save details clicked

4.1 the contact is inserted in database

4.2 Pageblocktable refreshed to show new contact also

4.3 Section is closed.

Issue faced:- I am passing id as parameter in command button but it is not retrieved in apex class. Hence none of the logic for button is working.

Could any one please help me out with this issue.

Following is my code

<apex:form id="frm1"> <apex:pageBlock >
<apex:pageBlockTable value="{!lstContact}" var="Con">
 <apex:facet name="caption">Contacts<br/><br/></apex:facet> 
 <apex:column value="{!Con.Title__c}" />
 <apex:column value="{!Con.First_Name__c}" />
 <apex:column value="{!Con.Surname__c}"/>
 <apex:column >
     <apex:commandButton value="Details" action="{!ViewContact}" rerender="frm1"> 
     <apex:param name="ContactId" value="{!Con.Id}" assignTo="{!ContactId}" />
     </apex:commandButton>
     <apex:commandButton value="Remove" action="{!RemoveContact}" rerender="frm1">
        <apex:param name="ContactId" value="{!Con.Id}" assignTo="{!ContactId}" />
    </apex:commandButton>
    </apex:column>
</apex:pageBlockTable>
<br/><br/></apex:pageBlock>
                    <apex:commandButton value="ADD NEW CONTACT" style="float:right"/>
                    <apex:outputPanel id="opContactInfo">
                        <apex:pageBlock id="pbContactInfo">
                            <apex:pageBlockSection columns="1"
                                            id="pbContactInfo"
                                            title="Contact"> 
                                <apex:inputField value="{!TestContact.Title__c}" />
                                <apex:inputField value="{!TestContact.Surname__c}" />
                                <apex:inputField value="{!TestContact.First_Name__c}" />
                            </apex:pageBlockSection>
                    <apex:pageBlockButtons >
                        <apex:commandButton value="Clear" action="{!ClearContactDetails}" rerender="frm1"/>
                        <apex:commandButton value="Save Details" action="{!SaveContact}" rerender="frm1"/>
                    </apex:pageBlockButtons>
                    </apex:pageBlock>
    </apex:outputPanel>
    </apex:form>

Class Code:-

public string ContactId {get;set;} 
public List<Contact__c> lstContact{get;set;}
Contact__c TestContact{get;set;}
public test()
 {
    lstContact =[Select Id,Title__c,First_Name__c,Surname__c 
                 From Contact__c];
     TestContact = new Contact__c();
}
public PageReference ViewContact()
{
    PageReference Pageref= null;
    ContactId =ContactId;
    system.debug('## ContactId:' + ContactId);
    TestContact = [Select Id,Title__c,First_Name__c,Surname__c from Contact__c
                        where Id =: ContactId];

    return Pageref;
}

public PageReference RemoveContact()
{
    PageReference Pageref= null;
    ContactId =ContactId;
    system.debug('## ContactId:' + ContactId);
    TestContact = [Select Id,Title__c,First_Name__c,Surname__c from Contact__c
                        where Id =: ContactId];

    lstContact.add(TestContact);
    if(lstContact != null && lstContact.size()>0)
    {
        delete TestContact;
    }                   
    return Pageref;
}
 public PageReference ClearContactDetails()
{
    PageReference Pageref= null;

    return Pageref;
}
 public PageReference SaveContact()
{
    PageReference Pageref= null;
    ContactId =ContactId;
    system.debug('## ContactId:' + ContactId);
    TestContact = [Select Id,Title__c,First_Name__c,Surname__c from Contact__c
                        where Id =: ContactId];

    lstContact.add(TestContact);
    if(lstContact != null && lstContact.size()>0)
    {
        insert TestContact;
    }                   
    return Pageref;
}
1

It will not working in your case as wished. Because of iteration the command button will always pass the ID of the last element of the lstContactcollection. Use actioFunction that placed outside of the iteration instead. Then pass the ID using onclick event of the each button:

<!-- The action function passes the ID to the controller -->
<apex:actionFunction name="passIdToController" action="{!ViewContact}" reRender="theTable">
    <apex:param name="ContactId" value="" assignTo="{!ContactId}" />
</apex:actionFunction>

<apex:pageBlockTable value="{!lstContact}" var="Con" id="theTable">
    <apex:column>
    <!-- The button passes the ID to the action function -->
    <apex:commandButton value="Details" 
                        onclick="passIdToController('{!ContactId}'); return false;"> 
    ...
| improve this answer | |

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.