+ Start a Discussion
Devendra KeshariDevendra Keshari 

Facing issue with apex:param

Hello All,

I have one scenerios in which we have to get the current id of record on clicking "Edit"(Command Link).For this i use  a <apex:param> but its not working.Please review my code and let me know my error. I am pasting my Code as follow:-\


<apex:page controller="ContactWithAccount">
    <apex:form  id="form">
  Account <apex:inputField value="{!objContact.Accountid}"/>
 <apex:commandButton  action="{!getContact}" title="Get Contact" value="Get Contact"/> 
 
  
      <apex:pageBlock id="hiddenBlock">
          <apex:pageBlockTable value="{!lstContact}" var ="Con">
              <apex:column>
                  <apex:commandLink value="Edit" action="{!edit}" />|<apex:commandLink value="Delete" action="{!deletevalue}"/>
                      <apex:param value="{!Con.id}" assignTo="{!Selectedid}"/>
              </apex:column>
              
              
              <apex:column value="{!Con.Name}"/> 
          
          
          </apex:pageBlockTable>
          
      
      
      </apex:pageBlock>
  
  </apex:form>
</apex:page>

And My Controller is as follows:-


public class ContactWithAccount{
    public Contact objContact{get;set;}
    public List<Contact>lstContact{get;set;}
    public String Selectedid{get;set;}
    public List<Contact>lstContact1{get;set;}
    
    
    public ContactWithAccount(){
        objContact = new Contact();
        lstContact = new List<Contact>();
        lstContact1 = new List<Contact>();
    
    
    }
    public void getContact(){
       lstContact = [select Name from contact where AccountId =:objContact.AccountId];
           
     }
     public void edit(){
          lstContact1 = [select Name from contact where AccountId =:Selectedid];
          for(Contact objCon :lstContact1 ){
              if(objCon.id ==Selectedid )
              {
                  
              
              
              }
              System.debug('_______________lstContact1_____________________________________________'+lstContact1);
               System.debug('_______________Selectedid_____________________________________________'+Selectedid);
          
          }
     
     
     }
     public void deletevalue(){
         
     
     
     } 
     



}

Currently  getting value of "   System.debug('_______________Selectedid_____________________________________________'+Selectedid); "
is "null"

Awaiting for your replies!!!
Thanks in advance.

Akshay DeshmukhAkshay Deshmukh

Hi, you just closed commandLink before enclosing <apex:param> inside. Wrap <apex:param> inside <apex:commandLink> and it should work.

<apex:commandLink value="Edit" action="{!edit}" >
                      <apex:param value="{!Con.id}" assignTo="{!Selectedid}"/>
</apex:commandLink>.

If you want 2 commandLink then you need to wrap <apex:param> in the respective link.

Let me know how it works.


Thanks,
Akshay

 

David HalesDavid Hales
Hello Devendra,

you are doing good in this code but I it need some correction.Like you are using param and not assign value in controller and in the query section.
please use this code and I'm sure this code will help and resolve your issue.

/* ************************************  Visulaforce Page********************************************* */

<apex:page controller="ContactWithAccount">
<apex:message />
    <apex:form id="form">
    
  Account : <apex:inputText value="{!strAccId}"/>
 <apex:commandButton action="{!getContact}" title="Get Contact" value="Get Contact"/>  
  
      <apex:pageBlock id="hiddenBlock">
          <apex:pageBlockTable value="{!lstContact}" var="Con">
              <apex:column >
                  <apex:commandLink value="Edit" action="{!edit}" reRender="hiddenBlock2">
                      <apex:param name="ContactId" value="{!Con.id}" assignTo="{!Selectedid}"/>
                  </apex:commandLink>
                  |<apex:commandLink value="Delete" action="{!deletevalue}" reRender="hiddenBlock2">
                      <apex:param name="DelConId" value="{!Con.id}" assignTo="{!Selectedid}"/>
                  </apex:commandLink>
                  <apex:pageBlock id="hiddenBlock2" rendered="false"></apex:pageBlock>
              </apex:column>              
              
              <apex:column value="{!Con.Name}"/>
              <apex:column value="{!Con.Id}"/>
              <apex:column value="{!Con.AccountId}"/>
          </apex:pageBlockTable>

      </apex:pageBlock>

  </apex:form>
</apex:page>

/* ****************************************************************************************************** */
/* ************************************ Apex Controller *********************************************** */

public class ContactWithAccount{
    
    public string strAccId { get; set; }
    public List<Contact> lstContact { get; set; }    
    public String Selectedid {
                                get;
                                set{
                                    Selectedid = value;
                                    }
                            }    
    
    public ContactWithAccount(){
        
        lstContact = new List<Contact>();
    
    }
    public void getContact(){
        
       lstContact = [select Id,Name,AccountId from contact where AccountId =: strAccId];
           
     }
     public PageReference edit(){
         Contact con = new Contact( id = selectedid );
         System.debug('con : '+con);
         if( con != null )
         {
             PageReference SelectedConPage = new PageReference('/'+con.id+'/e?retURL=/apex/ContactWithAcc');
             SelectedConPage.setRedirect(true);
             return SelectedConPage;
         }
          return null;     
     }
     public void deletevalue(){
     
     try{
         Delete new Contact( Id = Selectedid );
     }
     catch( DmlException e ){
     
         ApexPages.addMessages(e);
     
     }
     
         
     }
}

/* ****************************************************************************************************** */

Let me know for more help on this.

Thanks & Regards
KSPL{1021}
Kloudrac Softwares Pvt.Ltd.