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
NewToForceDevelopmentNewToForceDevelopment 

Help needed with update of Custom object with input values from Apex page

I have a payment object that has 2 properties amount and date. I have the following code in the Apex page and Controller that displays existing payments. I want to update amount of one payments and save it. What scode should i write in the save method to acheive this. I know i have to call update payment on the save method but i am not able to retrieve the user input from the page in my controller.

 

 

 

public with sharing class PaymentDetailsController

{    

public Decimal amount_c{get; set;}    

public Datetime date_c{get; set;}            

public PageReference save()

{        

Payment__c pc = new Payment__c();        

pc.Amount__c = amount_c;               

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'Amount - '+ pc.Amount__c );         ApexPages.addMessage(myMsg);        

return null;    

}

List<Payment__c> payments = new List<Payment__c>();            

public PaymentDetailsController()    

{             

processLinkClick();       

}             

public void processLinkClick()

{

       payments = [SELECT Id , Payment__c.Contact__c, Amount__c, Date__c FROM Payment__c WHERE Amount__c > 0 and Payment__c.Contact__c =: ApexPages.currentPage().getParameters().get('contactid')];    

}

        

public List<Payment__c> GetPaymentDetails()   

 {       

 return payments;       

}            

}

 

 

 

<apex:page controller="PaymentDetailsController" tabStyle="Payment__c" >
<apex:form >
        <h1>{!$CurrentPage.parameters.contactid}</h1>
 
        <apex:pageBlock title="Payment Details">
<apex:pageblockSection >
<h1>Page Messages:</h1>
<apex:pageMessages showDetail="true" id="msg"/>
</apex:pageblockSection>             
            <apex:pageBlockTable value="{!PaymentDetails}" var="pd">
                <apex:column HeaderValue="Amount">
                    <apex:inputField value="{!pd.Amount__c}" id="amount_c" />
                </apex:column>               
                <apex:column headerValue="Date">
                    <apex:inputField value="{!pd.Date__c}" id="date_c"/>
                </apex:column>
            </apex:pageBlockTable>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
       
    </apex:form>
</apex:page> 

Best Answer chosen by Admin (Salesforce Developers) 
SFDC-SDSFDC-SD

Please check this out...  this will work for your requirements.

Controller:

public with sharing class PaymentDetailsCon
{   
    private List<Payment__c> Payments;

    public List<Payment__c> getPayments()
    {
       return Payments;
    }    
    
    public void getData()
    {
       String query = 'SELECT Id ,Payment__c.Contact__c,Amount__c,Date__c FROM Payment__c';
       Payments = Database.query(query);
    }
    
    public PaymentDetailsCon()
    {
        getdata();
    }    

    public pagereference save()
    {
        try
        {  
            update payments;
        }
        catch( DMLexception e){
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Error,'Update Failed '+e);         
            ApexPages.addMessage(myMsg); }
                 
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Confirm,'Update Successful!' );         
        ApexPages.addMessage(myMsg);     
        return null;
    }              
}

 

 

 

 

Visualforce Page

<apex:page controller="PaymentDetailsCon" tabStyle="Payment__c" >
<apex:form >
<apex:pageBlock title="Payment Details" mode="edit">
    <apex:pageblockSection >
            <apex:pageMessages id="msg"/>
    </apex:pageblockSection>             

    <apex:pageBlockTable value="{!Payments}" var="pd">
            <apex:column HeaderValue="Amount">
             <apex:inputField value="{!pd.Amount__c}"/>
            </apex:column>                               
            <apex:column headerValue="Date">
                    <apex:inputField value="{!pd.Date__c}"/>
            </apex:column>                
    </apex:pageBlockTable>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>       
    </apex:form>
</apex:page>

 

 

 

 

 

All Answers

SFDC-SDSFDC-SD

Please check this out...  this will work for your requirements.

Controller:

public with sharing class PaymentDetailsCon
{   
    private List<Payment__c> Payments;

    public List<Payment__c> getPayments()
    {
       return Payments;
    }    
    
    public void getData()
    {
       String query = 'SELECT Id ,Payment__c.Contact__c,Amount__c,Date__c FROM Payment__c';
       Payments = Database.query(query);
    }
    
    public PaymentDetailsCon()
    {
        getdata();
    }    

    public pagereference save()
    {
        try
        {  
            update payments;
        }
        catch( DMLexception e){
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Error,'Update Failed '+e);         
            ApexPages.addMessage(myMsg); }
                 
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Confirm,'Update Successful!' );         
        ApexPages.addMessage(myMsg);     
        return null;
    }              
}

 

 

 

 

Visualforce Page

<apex:page controller="PaymentDetailsCon" tabStyle="Payment__c" >
<apex:form >
<apex:pageBlock title="Payment Details" mode="edit">
    <apex:pageblockSection >
            <apex:pageMessages id="msg"/>
    </apex:pageblockSection>             

    <apex:pageBlockTable value="{!Payments}" var="pd">
            <apex:column HeaderValue="Amount">
             <apex:inputField value="{!pd.Amount__c}"/>
            </apex:column>                               
            <apex:column headerValue="Date">
                    <apex:inputField value="{!pd.Date__c}"/>
            </apex:column>                
    </apex:pageBlockTable>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>       
    </apex:form>
</apex:page>

 

 

 

 

 

This was selected as the best answer
NewToForceDevelopmentNewToForceDevelopment

Thanks alot. It worked perfectly...