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
- If details button is clicked the details of contact to be shown in this pageblocksection.
- If remove button is clicked the contact in that row must be removed from database.
- If add new button is clicked Contact detail section shows up to add the details.
- 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;
}