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
DJ Rock your own styleDJ Rock your own style 

save button in visualforce(URGENT)

Hi All, I have created a list of all contacts in a vf page and an edit and save button beside each name in the list, Can someone please help me out with the class (WITHOUT STANDARD CONTROLLER) for edit and save, such that once i click EDIT, only that particular contact in the list should be editable, and once i click SAVE, only that contact should be updated. Thanks, Dave
Navatar_DbSupNavatar_DbSup

Hi,
Try the below code as reference:

/////////////////////////// VF Page ////////////////////////////////////////////////////
<apex:page controller="EditAccount">
<style TYPE="text/css">
.rows
{
   
    right:5px;
    font-weight: bold;
    padding-right:3em;   
    color: #333333;
    width: 100px;
    height: 20px;
}
</style>
    
<script type="text/javascript">
function popup(oid) 
{
        
       var url='/apex/ma4?id=' + oid;   
       window.open(url,"Homepage","resizable=yes,status=yes,scrollbars=yes,width=400,height=200,target=_blank");
}

function popu(aid)
{
    if(aid != '')
    {
        document.getElementById('xyz').value = aid;
        alert('?????'   + aid);
    }
}

function ppppp()
{
    var acid = document.getElementById('xyz').value;
    alert('>>>>>>'  + acid);
    fetchdata1(acid);
}

function change()
{
   
   var MyElement = document.getElementById("MTB");
  
   return true;
}

function HideShow(rId,Id,mode)
{
    alert('ABC');
    var rowId = rId;
    var accountId = Id;
    var editrowId = 'edit:'+ Id;
    if(mode == 'n')
    {
        document.getElementById(rowId).style.display = 'none';
        document.getElementById(editrowId).style.display = 'block';
    }
    else if(mode == 'e')
    {
        
        var rrowId = 'detail:'+ Id;
   
        document.getElementById(rrowId).style.display = 'block';
        document.getElementById(rowId).style.display = 'none';
        
    }
}

</script>

<body>
<apex:form >
                <h1><b><u><apex:outputText value="Account details:"/></u></b></h1>
<marquee><b><h1><apex:outputText id="t1" value="{!propDT}" /></h1></b></marquee>

   <table border="3">
   <tr>
     <th width='150' bgcolor="#00FFFF">AccountName</th>
     <th width='153' bgcolor= "#00FFFF">Phone</th>
     <th width='151' bgcolor="#00FFFF">Billing City</th>
     <th width='150' bgcolor="#00FFFF">Created Date</th>
   </tr>
    </table>

    <apex:outputPanel id="opnl">
 
         <div style="width:100%">
            <apex:repeat id="rptAccount" value="{!Account}" var="a" >
        
                <div id="detail:{!a.id}" style="width:61%;display:block;" ondblclick="HideShow(this.id,'{!a.id}','n');">
                <table border="2">
               <tr><td width='25%'><a href="#" onclick="popu('{!a.id}');fetchdata('{!a.id}');fetchdata1('{!a.id}');" ondbclick="change();"><h2>{!a.name}</h2></a></td>
           
                                                   
                         <td width='25%'><h2>  <span style="width:45%">{!a.phone}</span></h2></td>
                         
                          <td width='25%'><h2> <span style="width:25%" >{!a.billingcity}</span></h2></td>
                         <td width='25%'>  <span style="width:25%" class='rows'>{!a.CreatedDate}</span></td>
                   </tr></table>
                </div>
        
                <div id="edit:{!a.id}" style="width:35%;display:none;" >
                  <table border="2"><tr>
                  <td width='25%'><h2><span style="width:25%"><apex:inputField value="{!a.name}" id="MTBName" /></span></h2></td>
                  <td width='25%'> <h1><span style="width:25%"><apex:inputField id="MTBPhone" value="{!a.phone}"/></span></h1></td>
                   <td width='25%'> <h2><span style="width:25%"><apex:inputField id="MTBBCity" value="{!a.billingcity}"/></span></h2></td>
                  <td width='25%'> <h2><span style="width:25%"><apex:inputField id="MTBCDate" value="{!a.createddate}"/></span></h2></td>
                   <td width='25%'><input id="pqr" type="button" class="btn" value="save" onclick="HideShow('edit:{!a.id}','{!a.id}','e');savedata('{!a.id}');"/></td><td><input type="button" class="btn" value="cancel" onclick="HideShow('edit:{!a.id}','{!a.id}','e');"/></td>
             </tr>  </table>
                </div>
              
            </apex:repeat><br/>
                    <apex:commandButton value="SaveALL" action="{!dosave}"/>
        </div>
             
                  
    <!--
        <apex:pageBlockTable value="{!Account}" var="a" cellpadding="21" border="1">
            <apex:column >
                <a href="#" onclick="popu('{!a.id}');fetchdata('{!a.id}');fetchdata1('{!a.id}');" ondbclick="change();">{!a.name}</a>
                <apex:inputField id="MTB" value="{!a.name}"/>
                <input type="text" size="35" id="MyTextBox" value=""/>
              
             </apex:column>
            <apex:column value="{!a.phone}"/>
            
            <apex:column value="{!a.billingcity}"/>
            <apex:column value="{!a.createddate}"/>
            <apex:column >
               <apex:commandButton action="{!save}" value="save"/>
               <apex:commandButton action="{!cancel}" value="cancel"/>
            </apex:column>
        </apex:pageBlockTable>
        -->
  
  <br/><apex:outputText value="Contact Details:" style="font-size:13px;color:#00FFFF;font-weight: bold"/>
    <apex:outputPanel id="opnlCont">
        <apex:pageBlock >
            <apex:pageBlockTable value="{!propAllContacts}" var="c">
                <apex:column > 
               <apex:outputLink value="/{!c.Id}">{!c.name}</apex:outputLink>
                   <apex:facet name="header"> Contact Name</apex:facet>
                 
                </apex:column>
               <apex:column value="{!c.Phone}"> 
                   <apex:facet name="header">Contact Phone</apex:facet>
                </apex:column>
                <apex:column value="{!c.Department}"> 
                   <apex:facet name="header">Contact Department</apex:facet>
                </apex:column>
                <apex:column value="{!c.title}"> 
                   <apex:facet name="header">Contact title</apex:facet>
                </apex:column>
                <apex:column value="{!c.email}" breakBefore="true" colspan="4">
                  <apex:facet name="header">Contact Name</apex:facet>
                </apex:column>
                
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:outputPanel>
    
<apex:outputText value="Opportunity Details:" style="font-size:13px;color:#00FFFF;font-weight: bold"/>
<apex:pageBlock >
   <apex:outputPanel id="opnlOpp">
            <apex:pageBlockTable value="{!propLstWO}" var="o">
                <apex:column value="{!o.propName}">
                 <apex:facet name="header">Opportunity Name</apex:facet>
                </apex:column>
                <apex:column value="{!o.propAmount}">
                 <apex:facet name="header">Opportunity Amount</apex:facet>
                </apex:column>
                <apex:column value="{!o.propAttachNames}">
                  <apex:facet name="header">Opportunity Attachments</apex:facet>
                </apex:column>
                <apex:column ><input type="button" value="upload" onclick="popup('{!o.propId}')"/>
                  <apex:facet name="header">Browse</apex:facet>
                </apex:column>
           </apex:pageBlockTable>
    </apex:outputPanel>
</apex:pageBlock>    
</apex:outputPanel>


    <apex:actionFunction id="getcontacts" name="fetchdata" action="{!GetAllContacts}" reRender="opnlCont">
        <apex:param name="contactid" assignTo="{!propcontactid}" value="" />
     </apex:actionFunction>

   
    <apex:actionFunction id="getopportunity" name="fetchdata1" action="{!GetAllOpportunity}" reRender="opnlOpp">
        <apex:param name="opportunityid" assignTo="{!propopportunityid}" value="" />
    
    </apex:actionFunction>
    
   <input id="xyz" type="hidden" onclick="ppppp();" />
   <apex:actionPoller action="{!refresh}" reRender="t1" interval="10"/>
    <apex:actionFunction id="saveaccount" name="savedata" action="{!doSave}" reRender="opnl" oncomplete="alert('Name has been changed')" />
      <apex:param name="accountsave" assignTo="{!propviewdata}" value=""/>
   </apex:form>
</body>
</apex:page>

///////////////////////////// Controller //////////////////////////////////
public class EditAccount
{
    public string propviewdata{ get ; set ;}
    String datetimeStr = '';
    public String propDT { get { return datetimeStr; } set { datetimeStr = value; }}
    public void save(){}
    public void cancel(){}
    List<Contact> con;
    public List<Account> acc;
    public List<Opportunity> opp;
    List<WrapperOpportunity> lstWO = new List<WrapperOpportunity>();
    public string propcontactid { get; set; }
    public string propopportunityid{get;set;}
    public List<Contact> propAllContacts    { get { return con; } set { con = value; } }//End Prop propAllContacts    
    public List<Opportunity> propAllOpportunity    {  get { return opp;}  set { opp = value; } }//End Prop propAllOpportunity
   // private String sortDirection = 'ASC';
   // private String sortExp = 'name';
    public void refresh() 
    {
       datetimeStr = String.valueOf(DateTime.Now()).split(' ')[1];
    }
    public void doSave() 
    {   
        for(account ac : acc)
        {
           update ac;
        }
    }
    public List<Account> getAccount()   
    {
       return (List<Account>) acc; 
    }//End getAccount
    
    public void setAccount(List<Account> Acc)
    {
     this.acc = Acc;
    }  
    public List<WrapperOpportunity> propLstWO { get { return lstWO; } set { lstWO = value; } } 
    public EditAccount() 
    {
        acc = [select id,Name, Phone,BillingCity,CreatedDate from Account limit 1000];
    }//End Constructor
    
    public void GetAllContacts()
    {
        if(propcontactid != null && propcontactid != '')
        {
            if(con != null && con.size() > 0)
                con.clear();
            
            //con = [select id,name,email,title,Languages__c , Level__c,TestCreatedDate__c,Department,Phone from Contact WHERE AccountId =: propcontactid];
            con = [select id,name,email,title,Department,Phone from Contact WHERE AccountId =: propcontactid];
        }
    }//End GetAllContacts
    
    public void GetAllOpportunity()
    {
        if(propopportunityid != null && propopportunityid != '')
        {
             if(lstWO != null && lstWO.size() > 0)
                lstWO.clear();
            if(opp != null && opp.size() > 0)
                opp.clear();
            opp = [SELECT o.id, o.Amount, o.CreatedDate, o.Name, (Select Id, Name From Attachments) FROM Opportunity o  WHERE o.AccountId =: propopportunityid];
            if(opp != null && opp.size() > 0)
            {
               for(Opportunity oo : opp)
                {
                   WrapperOpportunity wo = new WrapperOpportunity();
                   wo.propId = oo.id;
                   wo.propName = oo.name;
                   wo.propAmount = String.valueOf(oo.amount);
                   //wo.propCreatedDate = oo.CreatedDate;
                   if(oo.Attachments != null && oo.Attachments.size() > 0)
                    {
                        String strAttachNames = '';
                        for(Integer j = 0; j < oo.Attachments.size(); j++)
                        {
                           strAttachNames = strAttachNames + oo.Attachments[j].name + ',   ';
                        }
                            wo.propAttachNames = strAttachNames.substring(0,strAttachNames.length()-4);
                   }
                   lstWO.add(wo);
                }
           }
        }
    }//End GetAllOpportunity
    public class WrapperOpportunity
    {
        public String propId { get; set; }
        public String propName { get; set; }
        public String propAmount { get; set; }
        //public String propCloseDate { get; set; }
        public String propAttachNames { get; set; }
    }
}

 Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

Rajat MahajanRajat Mahajan

Not tried the code yet, but does this open all the particular contact's fields as textbox (together) , when you click edit ?

 

Thanks

Rajat.

Rajat MahajanRajat Mahajan

Hi,

 

I did try out the code this time, thanks very much, it provides what i had asked for.

I appreciate the help.

 

Just wanted to know one thing,

 

When i click the SaveAll button, after editing more than one record, i get the last modified date and time changed to my user's name for all records, even if some are not edited.

 

Could you please help me with this ?

 

Regards

Rajat.