Find your content:

Search form

You are here

Unable to upsert a value from

 
Share

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>

Attribution to: MnZ

Possible Suggestion/Solution #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>

Attribution to: eyescream
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33189

My Block Status

My Block Content