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
BrandiTBrandiT 

URGENT HELP NEEDED!! Custom Save() Class to save 2 records from Visualforce page

I have a visualforce page whose standard controller is Contract.  It contains input fields from both the contract record and a related opportunity record (opportunity__c lookup field).

 

I need a controller extension to override the standard save function and update both the contract and the opportunity records. 

 

Here is what I have so far, but I can't figure out how to reference the related opportunity object so that both records are saved.

 

Please help!!  Thank you!

 

 

public class ContractSaveMKTG{
    ApexPages.StandardController controller;
           
public ContractSaveMKTG(ApexPages.StandardController con){
                            controller = con;
                                                       
                                                 }                                  
  public PageReference save() {
           Contract cnt = (Contract)controller.getRecord();
           Opportunity Opp = cnt.opportunity__r.id;
           Opp.save();               
           controller.save();                      
           
            return cnt;
            }}

 

 

 

Visualforce Page:

 

<apex:page standardController="Contract" showHeader="true" sidebar="false">
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!cancel}" value="Cancel"/>
</apex:pageBlockButtons>
<apex:pageblockSection >
<apex:inputField value="{!contract.opportunity__r.RPM_As_Sold__c}"/> 

/*Need this field saved to the related opportunity record and the others saved to the contract record*/
<apex:inputField value="{!contract.Distribution__c}"/>
<apex:inputField value="{!contract.Margin_Dollars__c}"/>
<apex:inputField value="{!contract.Average_Ad_Size__c}"/>
<apex:inputField value="{!contract.Margin_Percent__c}"/>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

Declare Opportunity and Contract Instance public variables.



 

public class ContractSaveMKTG{
    ApexPages.StandardController controller;
    public Contract contract {get; set;}

    public Opportunity opportunity {get; set;}

      
public ContractSaveMKTG(ApexPages.StandardController con){
                            controller = con;

contract = (Contract) controller.getRecord();

opportunity = contract.Opportunity__r;
                                                        
                                                 }                                   
  public PageReference save() {

           update opp;
           controller.save();                       
            
            return cnt;
            }

}

  

You can then reference the Opportunity and Contract directly.

 

<apex:inputField value="{!opportunity.RPM_As_Sold__c}"/> 

 

All Answers

Ritesh AswaneyRitesh Aswaney

Declare Opportunity and Contract Instance public variables.



 

public class ContractSaveMKTG{
    ApexPages.StandardController controller;
    public Contract contract {get; set;}

    public Opportunity opportunity {get; set;}

      
public ContractSaveMKTG(ApexPages.StandardController con){
                            controller = con;

contract = (Contract) controller.getRecord();

opportunity = contract.Opportunity__r;
                                                        
                                                 }                                   
  public PageReference save() {

           update opp;
           controller.save();                       
            
            return cnt;
            }

}

  

You can then reference the Opportunity and Contract directly.

 

<apex:inputField value="{!opportunity.RPM_As_Sold__c}"/> 

 

This was selected as the best answer
BrandiTBrandiT

That worked!!  Thanks so much!!