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 

Is there no help for the widows son?

I have a visual force page and apex class for editing multiple records at once, I am trying to make a button that creates a new record...I have the save function working, but cannot figure out how to create a new record. Any help would be appreciated!

 

Apex Class:

 

 

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();
    }
}

 

 

VF Page:

 

 

<apex:page controller="BulkEdit" tabStyle="Client__c">

<apex:form id="theForm">
  <apex:pageBlock title="Payment Schedule" mode="edit">

<!-- our Save button tied to our Save method -->
            <apex:pageBlockButtons >
                <apex:commandButton action="{!savePayments}" value="Save"/>
                <apex:commandButton action="{!back}" value="Back to Client Record"/>
            </apex:pageBlockButtons>

<!--
     Here we create a table using the Visualforce tag <apex:pageBlockTable>
     {!payments} calls "getPayments" method in our controller.
     Note: the word "get" in accessor methods is implicit in Visualforce.
     We're also assigning a name variable of "currentPayment" for each accessed record.
     The IDs need to be present to perform the updates but we can keep them
     invisible to our user through rendered="false"
-->
            <apex:pageBlockTable value="{!payments}" var="currentPayment" id="theRepeat">
              <apex:outputText rendered="false">{!currentPayment.Client__r.Id}</apex:outputText>
              <apex:outputText rendered="false">{!currentPayment.Client__c}</apex:outputText>

<!--
     We create our columns, along with headers, for our user worksheet
     Depending on whether or not we want to allow the user to edit a field, we use
     the inputField or outputField tags.
-->
                <apex:column ><apex:facet name="header">Name</apex:facet>
                    <apex:outputField value="{!currentPayment.name}"/>
                </apex:column>
                <apex:column width="80px"><apex:facet name="header">Amount</apex:facet>
                    <apex:inputField value="{!currentPayment.Payment_Amount__c}" style="width:80px"/>
                </apex:column>
                <apex:column ><apex:facet name="header">Sched Date</apex:facet>
                    <apex:inputField value="{!currentPayment.Payment_Scheduled_Date__c}"/>
                </apex:column>
                <apex:column ><apex:facet name="header">Clear Date</apex:facet>
                    <apex:inputField value="{!currentPayment.Payment_Cleared_Date__c}"/>
                </apex:column>
                <apex:column ><apex:facet name="header">Status</apex:facet>
                     <apex:inputField value="{!currentPayment.Payment_Status__c}" style="{!IF((currentPayment.Payment_Status__c = 'Cleared'),'color:green','color:red')}"/>
                </apex:column>
                <apex:column ><apex:facet name="header">Processing Fee</apex:facet>
                    <apex:inputField value="{!currentPayment.Processing_Fee__c}"/>
                </apex:column>
                <apex:column ><apex:facet name="header">Retainer Fee</apex:facet>
                    <apex:inputField value="{!currentPayment.Fee_Payment__c}"/>
                </apex:column>
                <apex:column ><apex:facet name="header">Accumulation</apex:facet>
                    <apex:inputField value="{!currentPayment.Set_Payment__c}"/>
                </apex:column>
                
            </apex:pageBlockTable>
  </apex:pageBlock>
</apex:form>
              <apex:iframe id="xyz" src="/apex/Bulk_Edit_Offer_Payments?id={!$CurrentPage.parameters.Id}"></apex:iframe>

</apex:page>

 

 

 

 

 

jmburnsjmburns

I found this example which seems as if it should work but am having trouble getting it to, can some one please assist with adding this to my class

 

public class xyz {

public List<Customer__c> customers = new List<Customer__c>();
public List<Customer__c> getCustomers(){
return customers;
}
public void addrow(){
customers.add(new Customer__c());
}
public PageReference save(){
insert customers;
}
}
jmburnsjmburns

I think perhaps I am getting closer, getting error:

 


System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []    Unit_Tests.cls   Debt/src/classes    line 23    Force.com run test failure

 

Here is the class with code added:

 

 

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();
        
        
    }
    }

 

 

Here is the section of unit tests that is failing:

 

 

// *********************************
// Unit test for the BulkEdit class
// *********************************
public static testMethod void TestBulkEdit()
{
PageReference pageRef = Page.Bulk_Edit;
Test.setCurrentPage(pageRef);

// Instantiate a new controller with all parameters in the page
BulkEdit b = new BulkEdit();
PageReference nextPage = b.savePayments();

// Verify that page fails without parameters
System.assertEquals(nextPage, null);

Client__c pi = [SELECT Id FROM Client__c WHERE IsDeleted = false LIMIT 1];

// Add parameters to page URL
ApexPages.currentPage().getParameters().put('id', pi.Id);

b = new BulkEdit();
b.back();
nextPage = b.savePayments();
}

 Thank you

 

 

jmburnsjmburns

I removed the test code from the test class and saved, this worked..except:

 

Now the add row functionality is working but the original functionality is not. (Display all Payment_History__c records associated with Client__c)