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
forceappforceapp 

Save on edit in own VF page.

My problem is that,  

   

          I have own VF page, using controller.The obj used is department it has fieds like name,email & phone. Now in a page block table i have

 

 Action        Name        Email                    Phone

 

Edit              xxxx             x@mail.com         0000

 

 

when i click edit , it should convert to (save)  and   name,email & phone field values should convert to text boxes.   After that   when i click save it should be saved..

Best Answer chosen by Admin (Salesforce Developers) 
rohitsfdcrohitsfdc

sorry for that.

change your controller to this and let us know the results.

public with sharing class Employ{
  
public Employ__c emp;

      
    public Employ__c getemplst{
get{
      emp=[select name,id,Email__c,Phone__c from Employ__c limit 1];
      return emp;}
set;
  }

  public PageReference save() {
        table2=false;
        table1=true;
        update con;
        return null;
    }


    public boolean getTable2() {
        return Table2;
    }


 boolean Table1=true;
 boolean Table2=false;
 public boolean gettable1(){
 return table1;
 }
 

    public PageReference edit() {
        table1=false;
        table2=true;
        return null;
    }
  }

 

All Answers

rohitsfdcrohitsfdc

Please share your code

forceappforceapp

This is my code:

 

<apex:page controller="Employ"  sidebar="false" showHeader="true" id="pg">
<apex:pageBlock title="Employee List" id="pb2">
<apex:form id="f1" >
<apex:pageBlockTable value="{!emplst}" var="emp" id="pbt">
<apex:column headerValue="Action">
<apex:commandLink action="{!mylst}" />
<apex:outputLink style="font-weight:bold" >Edit</apex:outputLink></apex:column>
<apex:column value="{!emp.name}"/>
<apex:column value="{!emp.Email__c}"/>
<apex:column value="{!emp.Phone__c}"/>
<apex:column value="{!emp.id}"/>
</apex:pageBlockTable>
</apex:form>
</apex:pageBlock>
</apex:page>

 

public with sharing class Employ{
   public String sname{get;set;}
      public String semail{get;set;}
         public String sphone{get;set;}

         public PageReference mylst()
     {
         List<Employ__c> mylst;
         mylst=[select name,Email__c,Phone__c from Employ__c ];
                  sname=mylst.get(0).Name;
         semail=mylst.get(0).Email__c;
         sphone=mylst.get(0).Phone__c;
                
         return null;
      } 
   
  List<Employ__c> emplst;
  public List<Employ__c> getemplst()
  {
      emplst=[select name,id,Email__c,Phone__c from Employ__c];
      return emplst;
  }
  }

forceappforceapp

Do u have any  solution for this..................

rohitsfdcrohitsfdc

change your page to following code:

<apex:page controller="Employ"  sidebar="false" showHeader="true" id="pg">

<apex:pageBlock title="Employee List" id="pb2">
<apex:form id="f1" >
 <apex:outputPanel rendered="{!table1}">
<apex:pageBlockTable value="{!emplst}" var="emp">
<apex:column headerValue="Action">
<apex:column >
             <apex:commandLink value="edit" action="{!edit}"/>
         </apeX:column>

<apex:column value="{!emp.name}"/>
<apex:column value="{!emp.Email__c}"/>
<apex:column value="{!emp.Phone__c}"/>
<apex:column value="{!emp.id}"/>
</apex:pageBlockTable>
</apex:outputPanel>

 <apex:outputPanel rendered="{!table2}">
<apex:pageBlockTable value="{!emplst}" var="emp">
<apex:column headerValue="Action">

<apex:column >
<apex:commandLink value="save" action="{!save}"/>
</apeX:column>

<apex:column value="{!emp.name}"/>
<apex:column value="{!emp.Email__c}"/>
<apex:column value="{!emp.Phone__c}"/>
<apex:column value="{!emp.id}"/>
</apex:pageBlockTable>
</apex:outputPanel>

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

 and controller:

public with sharing class Employ{
  
public List<Employ__c> emp = new List<Employ__c>();

      
    public List<Employ__c> getemplst{
get{
      emp=[select name,id,Email__c,Phone__c from Employ__c limit 1];
      return emplst;}
set;
  }

  public PageReference save() {
        table2=false;
        table1=true;
        update con;
        return null;
    }


    public boolean getTable2() {
        return Table2;
    }


 boolean Table1=true;
 boolean Table2=false;
 public boolean gettable1(){
 return table1;
 }
 

    public PageReference edit() {
        table1=false;
        table2=true;
        return null;
    }
  }

 let me know if any issue happens

 

forceappforceapp

First thanks for ur reply and

 

   The error is

 

           Error: Unknown property 'String.name'

and it continued for email, phone...

 

 

another thing is, the field valus name, email & phone should be chenged to text boxes..... when we enter new record in the boxes, it should be changed.

 

Thanks & Regards,

forceapp.

rohitsfdcrohitsfdc

sorry for that.

change your controller to this and let us know the results.

public with sharing class Employ{
  
public Employ__c emp;

      
    public Employ__c getemplst{
get{
      emp=[select name,id,Email__c,Phone__c from Employ__c limit 1];
      return emp;}
set;
  }

  public PageReference save() {
        table2=false;
        table1=true;
        update con;
        return null;
    }


    public boolean getTable2() {
        return Table2;
    }


 boolean Table1=true;
 boolean Table2=false;
 public boolean gettable1(){
 return table1;
 }
 

    public PageReference edit() {
        table1=false;
        table2=true;
        return null;
    }
  }

 

This was selected as the best answer
forceappforceapp

Thank you, I got it..............

forceappforceapp

Thank you the functionality is working.

 

And one thing in the test class   the code coverage is 100%  but a exception

 

                        System.NullpointerException:Attempt to dereference a null object

How can it be solved...

   

Another is,as in the senario the entire page is refreshing. Is there possibility  to refresh single record when it is edited and saved..... 



Thanks & Regards,

forceapp

rohitsfdcrohitsfdc

post your testClass code

forceappforceapp

 This is my Test class

@isTest
private class Test_Employ {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
         boolean Table1=true;
         boolean Table2=false;
        Employ e=new Employ();
        Employ__c a =new Employ__c();
        a.name='ramana';
        a.email__c='b@gmail.com';
        a.phone__c='9896898';
        table2=false;
        table1=true;
        insert a;
        update a;
         e.edit();
         e.getemplst();
         e.gettable1();
         e.getTable2();
        
        
    }
    static testMethod void myUnitTest1() {
        // TO DO: implement unit test
         boolean Table1=true;
         boolean Table2=false;
        Employ e=new Employ();
        Employ__c a =new Employ__c();
         a.name='ramana';
        a.email__c='b@gmail.com';
        a.phone__c='9896898';
        table2=false;
        table1=true;
        insert a;
        
        e.save();
         e.edit();
         e.getemplst();
         e.gettable1();
         e.getTable2();     
    }
}