0

UPDATE. Unable to upsert!!

I modified the code in the previous post. Here's the original one....

public class dataTableCon { public List fb;

 public List<Feedback__c> getAccounts() {
   fb= [SELECT Name,
               Description__c,
               Importance__c,
               Resolution__c,
               Status__c,
               Owner.name,LastModifiedDate,
               (SELECT Comment__c,LastModifiedDate
               FROM Feedback_Comments__r)
               FROM Feedback__c];
    return fb;
 }   

 public PageReference save()
 {
  upsert fb;
  return null;
  }

  public PageReference del()
  {
   return null;
   }
 }

Here's the VF

    <apex:page controller="dataTableCon"  id="thePage" showHeader="false" standardStylesheets="false" sidebar="false" applyBodyTag="false">
    <apex:stylesheet value="{!$Resource.style}"/>
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" />
     <apex:form >
     <apex:dataTable value="{!Accounts}" var="account" id="theTable" bgcolor="lightblue" border="5">
    <apex:column >
    <apex:repeat value="{!account.Feedback_Comments__r}" var="account">
      <apex:inputtext value="{!account.Comment__c}"  title="Save after editing comments"  size="70" /> 
     </apex:repeat>
    </apex:column>
      <apex:column>
        <apex:pageBlock >  
            <apex:commandButton action="{!save}" value="Update" style="background-image:url('{!URLFOR($Resource.button)}')" />
            <apex:commandButton action="{!del}" value="Delete" style="background-image:url('{!URLFOR($Resource.button)}')"/>
          <apex:pageBlockButtons / >
        </apex:pageBlock> 
             </apex:column>
    </apex:dataTable>
   </apex:form>
  </apex:page>
6
  • As per your posted code your are duplicating btnSave id between the inputtext and commandButton which leads to unexpected results.
    – highfive
    Commented Apr 20, 2014 at 7:50
  • i had no use of the id field. So I removed them. Same Result! Just action="{!save}" calling the controller method. Am I missing something?
    – MnZ
    Commented Apr 20, 2014 at 8:02
  • In your save method it should be upsert record,not upsert fb
    – highfive
    Commented Apr 20, 2014 at 8:15
  • I'm sorry. I copied from a wrong file! Its record in my running program. Let me update this question. So If I click the save button the page refreshes.
    – MnZ
    Commented Apr 20, 2014 at 8:19
  • Can you please double check your code and update the post? Also make sure to post the whole required code snippets. Can't see any sync between controller and page(what is account.child__r refers in controller? what you have put in value attribute of your datatable?)
    – highfive
    Commented Apr 20, 2014 at 16:41

1 Answer 1

1

There are several things wrong in this code... For example you're using the same var="account" which is a bit confusing:

<apex:dataTable value="{!Accounts}" var="account" id="theTable" bgcolor="lightblue" border="5">
    <apex:column >
        <apex:repeat value="{!account.Feedback_Comments__r}" var="account">

You have a getter function that just returns your Accounts. You don't have a setter. The changes you're sending back to the server will be discarded and the next time getAccounts() will be called it'll fetch data from the database.

Also - upsert statement only saves the top object (Feedback). To save changes to the related lists you need to save them explicitly!

Try with something like this in the controller:

public class dataTableCon {
    public List<Feedback__c> feedbacks {get; set;}

    public dataTableCon(){ // constructor
        feedbacks = [SELECT Id, Name, 
            (SELECT Comment__c FROM Feedback_Comments__r)
            FROM Feedback__c];
    }

    public void save(){
        // update feedbacks;
        List<Feedback_Comment__c> toSave = new List<Feedback_Comment__c>();
        for(Feedback__c f : feedbacks){
            if(!f.Feedback_Comments__r.isEmpty()){
                toSave.addAll(f.Feedback_Comments__r);
        }
        update toSave;
    }
}

This fetches the data only once, when constructor runs and saves the kids correctly.

<apex:dataTable value="{!feedbacks}" var="f">
    <apex:column value="{!f.Name}" />
    <apex:column headerValue="Comments">
        <apex:repeat value="{!f.Feedback_Comments__r}" var="fc">
            <apex:inputField value="{!fc.Comment__c}" />
        </apex:repeat>
    </apex:column>
</apex:dataTable>
2
  • Thanks for explaining. So what should be the value in datatable?
    – MnZ
    Commented May 1, 2014 at 11:56
  • Its working. Thanks for showing me the correct way.
    – MnZ
    Commented May 1, 2014 at 12:20

You must log in to answer this question.

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