Skip to main content
Hi.. I have a list of accounts with some of its field in a vf page using a datatable component. i want to add a link to each record which on clicking it, should navigate me to another vf page where i willl have those account fields and i can edit them and save  or cancel it. i tried using a command button and a page refernce function in a custom controller to link it to another page but i am not getting required result. any hint will be appreciated.

  Thank you
9 respuestas
  1. 28 abr 2016, 12:44
    Following is the code, that will most likely solve your problem.

    Apex:

    public class TestClass2 {

    //Data type changed below

    public Id acId{get; set;}

    public List<Account> ac1{get; set;}

    //new variable added

    public Account ac{get;set;}

    //removed List<account> ac2

    public TestClass2(){

    ac1=new List<Account>();

    ac = new Account();

    ac1=[select id,name, billingstate, phone, fax from account];

    }

    //changed Function name

    public pageReference editAcc(){

    if(acId==null)

    return null;

    else{

    for(Account a: ac1){

    if(a.id==acId){

    ac = a;

    break;

    }

    }

    if(ac.id!=null){

    //use actual page name of your 2nd VF page in place of test3

    PageReference pr = new PageReference('/apex/test3');

    pr.setRedirect(false);

    return pr;

    }

    }

    return null;

    }

    //new method added

    public void saveAc(){

    if(ac.id!=null)

    update ac;

    else

    System.debug('some error occured');

    ac=new Account();

    }

    }

    VF Page 1:

    <apex:page controller="TestClass2">

    <apex:form >

    <apex:pageBlock >

    <apex:dataTable value="{!ac1}" var="a">

    <apex:column headerValue="Name" value="{!a.name}"/>

    <apex:column headerValue="Phone" value="{!a.phone}"/>

    <apex:column headerValue="Fax" value="{!a.fax}"/>

    <apex:column headerValue="Billing state" value="{!a.billingstate}"/>

    <apex:column headervalue="Edit">

    <apex:commandLink value="Edit" action="{!editAcc}">

    <apex:param name="acId" id="acId" value="{!a.id}" assignTo="{!acId}"/>

    </apex:commandLink>

    <!-- alternatively you can use apex:commandLink if you don't like above approach -->

    <!--

    <button type="button" onclick="editFn()">Edit</button>

    <apex:actionFunction name="editFn" action="{!editAcc}">

    <apex:param name="acId" id="acId" value="{!acId}"/>

    </apex:actionFunction>

    -->

    </apex:column>

    <!-- following part not needed

    <apex:column >

    <apex:facet name="header">Account Id</apex:facet>

    <apex:outputLink value="/{!a.id}/e">click</apex:outputLink>

    </apex:column>

    -->

    </apex:dataTable>

    </apex:pageBlock>

    </apex:form>

    </apex:page>

    VF Page 2

    <apex:page controller="TestClass2" >

    <apex:form >

    <apex:pageBlock >

    <apex:pageBlockTable value="{!ac}" var="a">

    <apex:column headerValue="Name" >

    <apex:inputText value="{!a.name}"/>

    </apex:column>

    <apex:column headerValue="Phone" >

    <apex:inputText value="{!a.phone}"/>

    </apex:column>

    <apex:column headerValue="Fax" >

    <apex:inputText value="{!a.fax}"/>

    </apex:column>

    <apex:column headerValue="Billing state" >

    <apex:inputText value="{!a.billingstate}"/>

    </apex:column>

    <apex:column >

    <apex:commandButton action="{!saveAc}" value="Save"/>

    </apex:column>

    </apex:pageBlockTable>

    </apex:pageBlock>

    </apex:form>

    </apex:page>

    Mark it as Best Answer, if it Solves your problem.
0/9000