0

Visualforce page which generates accounts and contacts. I can edit the record contact field, when I use the save button. It does not save my update to the corresponding record. My debug log outputs the complete list of accounts.

Visualforce:

<apex:page controller="actocons" tabStyle="Account">
    <apex:form >
        <apex:pageBlock mode="inlineEdit">
            <apex:pageBlockButtons >
                <apex:commandbutton value="save" action="{!save}"/>
            </apex:pageBlockButtons>

            <apex:repeat value="{!acct}" var="a">
                <apex:pageBlockSection id="section1" title="{!a.name}" onclick="document.getElementById(‘{!$Component.section1}’).childNodes[0].childNodes[0].click();">  
<script>twistSection(document.getElementById(“{!$Component.section1}').childNodes[0].childNodes[0]);document.getElementById(“{!$Component.section1}').childNodes[0].style.cssText = “cursor:pointer;' </script>
                    <apex:outputField value="{!a.Name}"/>
                    <apex:pageBlockTable value="{!a.Contacts}" var="c" id="j1">
                        <apex:column value="{!c.id}" />
                        <apex:column value="{!c.lastname}"/>
                        <apex:column value="{!c.lastname}"/>
                    </apex:pageBlockTable>
                </apex:pageBlockSection>
            </apex:repeat>
        </apex:pageBlock>
<script>
twistSection = (function() {
    var cached_function = twistSection;
    return function(twisty, sectionId) {
        cached_function.apply(this, arguments); // use .apply() to call the original twistSection function provided by Salesforce
        if (event.stopPropagation) {
            event.stopPropagation();
        } else {
            event.cancelBubble = true;
        }
    };
}());

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

Controller:

public class actocons

{

    Private Id accID;
    public List<Contact> contactList{get;set;}


    public list <contact> myContact {set;get;}
    public list<account> acct{set;get;}
    public actocons()
    {
        acct=[select id,name,(select id,lastname from contacts)from account limit 5];
        myContact = [SELECT Id, Name FROM Contact WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public PageReference save() {


        contactList = new List<Contact>();
        accID=  ApexPages.currentPage().getParameters().get('acId');
        contactList =  [SELECT FirstName,LastName,Email,Phone FROM Contact WHERE AccountID = : accID];

        system.debug('contactList '+ contactList);
        update contactList;
        return null;
    }

}
3
  • I've edited your question to improve the formatting. You've been around for a few months now though, so I'd like to see you do your own formatting (and spell-checking/using punctuation) on your own questions. Proper indentation is important for actually being able to read and understand code (not doing so makes it harder for people to help you, which decreases your chances of getting a decent answer). Looking through the help page on formatting will help. I may start downvoting your questions if you don't put in the effort to format them.
    – Derek F
    Commented Aug 31, 2017 at 19:40
  • @Derek Thanks for the tip. I do some formatting but sometimes it seems like the quotes do not format correctly I already made a remark on this, but thanks anyway.
    – Jenssen
    Commented Aug 31, 2017 at 19:49
  • The big thing is that blocks of code (that is, anything longer than a single line of code) do better formatted as a code block than they do as 'inline' code. Using backticks (`) formats code as inline. Formatting code as a block means having at least 4 blank spaces at the start of each line + an empty line above and below the code. The easiest way to do that is to highlight your code block and push the {} button at the top of the text editor window or press ctrl + k (cmd + k on Apple Macbooks).
    – Derek F
    Commented Aug 31, 2017 at 20:16

1 Answer 1

0

About the only issue I can see is that your Visualforce page is showing a.Contacts (via a pageBlockTable), but your controller is running a separate query for Contact records in your save() method.

If the changes you're making to the Contacts on your Visualforce page are being tracked anywhere (and I don't know if they are. That usually requires using <apex:inputField>, and I don't know if setting the inlineEditing mode is enough), it would be in acct.Contacts.

Without writing up a Visualforce page of my own (I don't have time for that right now), my only suggestion would be to make the following changes to your controller's save() method:

public PageReference save() {

    contactList = new List<Contact>();

    // If you did need to query for Contacts, you could simply fetch the Id from
    //   one of the Accounts you're keeping in the variable `acct` (which itself
    //   could stand to be better named).
    //accID=  ApexPages.currentPage().getParameters().get('acId');
    //contactList =  [SELECT FirstName,LastName,Email,Phone FROM Contact WHERE AccountID = : accID];

    //system.debug('contactList '+ contactList);

    // Since the changes you make via your page will be stored in the 'Contacts'
    //   list associated to each Account, that's the Contacts you'll want to update.
    // We'll need to fetch the Contacts, and put them all into a single list.
    // Have I mentioned that acct is a really bad name for a list of Accounts?
    // I'd personally, at the very least, make the variable name plural (i.e. accts)
    //   if not putting 'List' in the variable name (e.g. accountsList)
    for(Account accnt :acct){
        // We use the child relationship name to access the List of Contacts related
        //   to the current Account.
        // In this case, that's 'Contacts'.
        // Since that gives us a `List<Contact>` as a result, we can't just use
        //   the List.add() method (which is for adding a single record).
        // Instead, we need to use addAll() (which accepts a List as an argument).
        contactList.addAll(accnt.Contacts);
    }

    update contactList;
    return null;
}
2
  • Thanks for the detailed explanation, but it didn't solve the issue
    – Jenssen
    Commented Aug 31, 2017 at 20:22
  • 1
    @Jenssen If this didn't solve your issue, then I would recommend editing your question to go into more detail about what your issue is. Details like 'This is what happens when I try to save my records now. <explanation>', and 'This is the current result I see after running my code. <more explanation>'. Basically, explain what you mean when you say "it doesn't save correctly". If you don't explain, people have to guess at what your issue actually is.
    – Derek F
    Commented Aug 31, 2017 at 20:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .