1

Here is my below code and I have a simple default save as shown below and when I click the save button it does not throw me error and it does refresh the page but I do not see the data being saved..

<apex:commandButton action="{!save}" value="Save" id="btnSave"/>  

my VFP code is:

    <apex:page controller="accconhierarchy">
  <apex:form >
      <apex:pageBlock >
      <style type="text/css">
        .showContacts {
            background: transparent url('/img/alohaSkin/twisty_sprite.png') 0 0 no-repeat;
        }
        .hideContacts {
            background: transparent url('/img/alohaSkin/twisty_sprite.png') 0 -11px no-repeat;
        }
    </style>

                  <apex:outputPanel layout="none"
                        rendered="{!accountList.size!=0}">
                        <table id="tableAccount" class="list" border="0" cellpadding="0" cellspacing="0">
                            <thead>
                                <tr class="headerRow">
                                    <th class="actionColumn">Action</th>
                                    <th>Account Name</th>
                                    <th>Industry</th>
                                    <th>Billing Country</th>
                                    <th>Createdby</th>
                                </tr>
                            </thead>
                            <tbody>
                            <apex:repeat id="repeatAccount"
                                value="{!accountList}"
                                var="item">
                                <apex:variable var="acc" value="{!item.oAccount}" />   
                                <tr id="{!$Component.repeatAccount}:account" class="dataRow">
                                    <td class="actionColumn">
                                        <apex:inputCheckbox id="selectAccountRow"
                                            value="{!item.IsSelected}"
                                            onclick="javascript:toggleSelectAll(this, '{!$Component.repeatAccount}');"/>
                                    </td>
                                    <td class="dataCell">
                                        <apex:outputLink id="linkShowHide" value="javascript:toggleContactRow('{!$Component.repeatAccount}')">
                                            <img id="{!$Component.repeatAccount}:Img" src="/s.gif" class="showContacts" border="0" height="11px" width="11px" style="padding-right: 4px;"/>
                                            <apex:outputField value="{!acc.Name}" />
                                        </apex:outputLink>
                                    </td>
                                    <td class="dataCell"><apex:outputField value="{!acc.Industry}" /></td>
                                    <td class="dataCell"><apex:outputField value="{!acc.billingcountry}" /></td>
                                    <td class="dataCell"><apex:outputField value="{!acc.CreatedById}" /></td>
                                </tr>

                                <tr id="{!$Component.repeatAccount}:Contact" class="dataRow" style="display:none;">
                                    <td colspan="100" style="padding:10px; padding-left: 45px;">
                                        <h3>Contacts</h3>
                                        <br/>      
                                         <apex:commandButton action="{!save}" value="Save" id="btnSave"/>
                                        <br/>                                    
                                        <apex:outputPanel layout="none">                                            
                                            <table id="tableAccountContacts" class="list" border="0" cellpadding="0" cellspacing="0">
                                                <thead>
                                                    <tr class="headerRow">
                                                        <th class="actionColumn">Action</th>
                                                        <th>Name</th>
                                                        <th>Email</th>
                                                        <th>Phone</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                <apex:repeat id="repeatAccountContacts"
                                                    value="{!item.contactSet}"
                                                    var="subitem">
                                                    <apex:variable value="{!subitem.oContact}" var="con" />
                                                    <tr class="dataRow">
                                                        <td class="actionColumn"><apex:inputCheckbox id="selectContactRow" styleclass="ContactCheckbox" value="{!subitem.IsSelected}"/></td>
                                                        <td class="dataCell"><apex:outputField value="{!con.Name}" /></td>
                                                        <td class="dataCell"><apex:outputField value="{!con.email}"/></td>
                                                        <td><apex:inputText id="phone" value="{!con.phone}"/></td>   

                                                    </tr> 
                                                </apex:repeat>

                                                </tbody>
                                            </table>
                                        </apex:outputPanel>
                                    </td>
                                </tr>
                            </apex:repeat>
                            </tbody>
                        </table>
                    </apex:outputPanel> 
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
 <script>
        jQuery.noConflict();
        function toggleContactRow(rowId) {
            try {
                var AccountImg = jQuery(document.getElementById(rowId + ":Img"));
                var ContactsRow = jQuery(document.getElementById(rowId + ":Contact"));
                if (AccountImg.hasClass("hideContacts")) {
                    AccountImg.attr("class", "showContacts");
                    ContactsRow.hide();
                }
                else {
                    AccountImg.attr("class", "hideContacts");
                    ContactsRow.show();
                }
            }
            catch (e) { alert(e); }
        }  

        function toggleSelectAll(elCheckbox, rowId) {
            try {
                var SelectAll = jQuery(elCheckbox);
                var ContactsRow = jQuery(document.getElementById(rowId + ":Contact"));
                ContactsRow.find(".ContactCheckbox").each(function() {
                    jQuery(this).prop("checked", SelectAll.is(":checked"));
                });
            }
            catch (e) { alert(e); }
        }

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

Controller :

public class accconhierarchy {
    Private list<account> acc;

    public List<accountWrap> accountList { get; set; }
    public accconhierarchy (){
        List<contactWrap> cw;
        accountList = new List<accountWrap>();
        acc = [select id,name,industry,billingcountry,createdbyid,(select id,name,email,phone from contacts) from account limit 20];
        for(account a:acc){
            cw = new list<contactWrap>();
            For(contact co : a.contacts){
               cw.add(new contactWrap(co));
            }
            accountList.add(new accountWrap(a,false,cw));
        }
    }

    public class accountWrap{
        public account oAccount{get;set;}
        public boolean isSelected{get;set;}
        public List<contactWrap> contactset{get;set;}
        public accountWrap(account a,boolean b, List<contactWrap> c){
            oAccount=a;
            isSelected=b;
            contactset =c;            
        }
    }

    public class contactWrap{
        public contact oContact{get;set;}
        public boolean isSelected{get;set;}        
        public contactWrap(contact a){
            oContact=a;
            isSelected=false;                       
        }
    }

}
  • 2
    The controller code you have posted is missing the save method. There is no default save method unless you are writing a controller extension and that does not appear to be the case. Always have an apex:pagemessages tag in your page - a common reason for a failed save is failing validation that you don;t see without that tag. – Keith C Jul 4 '15 at 22:24
  • I'm new to vfp and still learning but I thought if you are overriding then you need to define the save method in the controller otherwise you don't. is that not true? – Nick Kahn Jul 4 '15 at 22:26
  • Yep if you want to replace the method you do. But it is also possible to leverage the StandardController and its save method - probably not appropriate in your case. – Keith C Jul 4 '15 at 23:33
  • Keith: I am not sure I understand, what do you mean not needed in my case? – Nick Kahn Jul 5 '15 at 2:19
  • 1
    I mean that your save logic needs to be very different to the standard controller one so a completely custom save method in your own controller is the way to go. But no-one can help you here as you haven't included your save method code. – Keith C Jul 5 '15 at 8:53
1

As pointed out in the comments, since you are using a Custom Controller, you will need to define your save method. The docs here should help you understand the difference between using a standard controller, an extension and a custom controller as in your case:

Custom controllers cannot have any arguments passed to their constructor. In the extension controller example, an instance of ApexPages.StandardController is passed to the constructor, allowing you to call getRecord() and magically obtain the Account record with the ID referenced in the URL. In a custom controller, you’ll have to implement this functionality yourself as shown in Listing 3-5.

Listing 3-5. Calling a custom controller from a Visualforce page.

public class CustomController {

     public Account acct {get;set;}

     public CustomController() {
          String acctId = ApexPages.currentPage().getParameters(). get(‘id’);
          acct = [Select Rating From Account Where Id =&nbsp;:acctId];
     }

     public PageReference save() {
          update acct;
          return new PageReference(‘/’ + acct.Id);
     }
}

<apex:page controller=”CustomController” >
     <apex:form>
          <apex:pageBlock >
               <apex:inputField value=”{!acct.Rating}” />
               <apex:commandButton action=”{!save}” value=”Save”>
          </apex:pageBlock>
     </apex:form>
</apex:page>
| improve this answer | |
0

I think you have used controller thats why the save button is not working. If you use standard controller rather then controller then the standard save button will work. If you want that the save button to work than you have to create your own custom save action in your controller.

for eg:

public pageReference saveValue()
{
Account a = new Account();
a.name='abc';
a.AccountNumber='123';
insert a;
 return null;
}
| improve this answer | |
0

The default save method will work only if you're using standardcontroller. In your case, you're using custom controller. So you need to define your own save method in your controller to make this work.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.