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
jmburnsjmburns 

Insert a record in table...driving me crazy someone please help

This class displays all Payment-History__c records associated with Client__c and allows to edit and save. I am trying to create the ability to insert a new line in the table basically an add row functionality. I cannot seem to make it work, here is the original class without the add row function:

 

 

public with sharing class BulkEdit {
    public List<Payment_History__c> payments;
    public Id thePH;
    
  // class constructor 
    public BulkEdit() {
        thePH = ApexPages.currentPage().getParameters().get('id');
        payments = getPayments();
    }
    
  // get all the children for this Client__c
    public List<Payment_History__c> getPayments() {
      payments = [SELECT Name, Payment_Amount__c, Payment_Scheduled_Date__c, Payment_Cleared_Date__c, Payment_Status__c, Fee_Payment__C, Set_Payment__c, Processing_Fee__c
                    FROM Payment_History__c  WHERE Client__r.Id = :thePH];

       return payments;
    }
    
    public PageReference back()
    {      
        return new PageReference('/' + thePH);
    }
    
  // the Save button's method.  This will update both the parent and children records.
    public PageReference savePayments() {
        update payments;
        payments = getPayments();
        
        if (thePH == null)
        {
      return null;
        }
                
        Client__c phToUpdate = [ SELECT Id FROM Client__c WHERE Id = :thePH LIMIT 1];
        update phToUpdate;
        //return new PageReference('/' + thePH);
        
        // refresh current page
        return ApexPages.currentPage();
    }
}

 

 

Here is my attempt at adding the add row function, on this one the ad row function works..but the original function to display and save does not...lol what am i doing wrong????

 

 

public with sharing class BulkEdit {
    public List<Payment_History__c> payments {get; set;}
       public Id thePH;
 
        
  // class constructor 
    public BulkEdit() {
        thePH = ApexPages.currentPage().getParameters().get('id');
        payments = getPayments();
        payments = new List<Payment_History__c>();
        payments.add(new Payment_History__c());

    }
    public void addrow(){
        
        payments.add(new Payment_History__c());  
    }
    
  // get all the children for this Client__c
    public List<Payment_History__c> getPayments() {
      payments = [SELECT Name, Payment_Amount__c, Payment_Scheduled_Date__c, Payment_Cleared_Date__c, Payment_Status__c, Fee_Payment__C, Set_Payment__c, Processing_Fee__c
                    FROM Payment_History__c  WHERE Client__r.Id = :thePH];

       return payments;
    }

    public PageReference back()
    {      
        return new PageReference('/' + thePH);
    }
    
  // the Save button's method.  This will update both the parent and children records.
    public PageReference savePayments() {
        update payments;
        insert payments;
        payments = getPayments();
                             
        if (thePH == null)
        {
      return null;
        }
                
        Client__c phToUpdate = [ SELECT Id FROM Client__c WHERE Id = :thePH LIMIT 1];
        update phToUpdate;
        
        //return new PageReference('/' + thePH);
        
        // refresh current page
        return ApexPages.currentPage();
        
        
    }
    }

 

 

I have received a lot of help form these boards and greatly appreciate the opportunity to learn, my developer is on vaction until monday (shes been gone 2 weeks!) and I am trying to make sure we dont miss our deadline. Thank you for any help!

 

Jason Burns

mikefmikef

jmburns:

 

My advice is you should never mess with your developers code while they are on vacation. :)

 

but I think I know what the issue is.

You can't update and insert the same list because if your list of sobjects has an ID salesforce will error out on insert. and if your list of sobjects doesn't have an ID then salesforce will error out on update.

 

So you have to create two lists, you already have the update list, the orginial payments list. Now you need to get your insert list. Here is a quick way to do that.

 

 

  // the Save button's method.  This will update both the parent and children records.
    public PageReference savePayments() {
        List<Payment_History__c> updateList = new List<Payment_History__c>();
        List<Payment_History__c> insertList = new List<Payment_History__c>();

        for(Payment_History__c  payment : payments){
          if(payment.Id != null){
             updateList.add(payment);
          }else{
             insertList.add(payment);
          }
        }        

        if(updateList.size() > 0){
           update updateList;
        }

        if(insertList.size() > 0){
           insert insertList;
        }
                             
        if (thePH == null)
        {
      return null;
        }
                
        Client__c phToUpdate = [ SELECT Id FROM Client__c WHERE Id = :thePH LIMIT 1];
        update phToUpdate;
        
        //return new PageReference('/' + thePH);
        
        // refresh current page
        return ApexPages.currentPage();
        
        
    }

 

 

Please keep in mind this is not production ready code and you will need to write a testMethod to test the business process.

It just a quick and dirty way to get you on the right track.

Hope this helps.

jmburnsjmburns

Thank you very much

 

Jaosn Burns

jmburnsjmburns

I do not think this is quite what I was looking to do, I am looking for a seperate function so that i may call it from a visual force page.  Thank you very much for trying to help though

 

JB