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
Stephanie Boggs 17Stephanie Boggs 17 

apex class write to account from opp

Hello,

I have a VF page named "Contract Request" that is accessed from the Opportunity (standard object) and has input fields for both the Opp and the related Account (standard object). I need to be able to write back field updates to both the Opportunity and the related Account upon Save. 

Let me know if there are any questions.

Thanks!
Raj VakatiRaj Vakati
can you share the code ..?You need to do to do buy using .. 
  • Wrapper class 
  • Sobject
  • Standard DML etc... 
Stephanie Boggs 17Stephanie Boggs 17
The VF page is:
 
<apex:page standardcontroller="Opportunity" lightningStylesheets="true">
<apex:messages />
    <apex:sectionheader title="{!$ObjectType.Opportunity.label} Contract Review Request" subtitle="{!IF(ISNULL(Opportunity.Name), 'New Opportunity',Opportunity.Name)}"/>

   
    <apex:form >
        <apex:pageblock > 
            <apex:pageblocksection title="" showheader="false" columns="1">
      
        <apex:pageblock mode="edit" title="{!$ObjectType.Opportunity.label} Contract Review Request">
            <apex:pageblockbuttons >
                <apex:commandbutton value="Save & Submit" action="{!Save}"/>
                <apex:commandbutton value="Cancel" action="{!Cancel}"/>
            </apex:pageblockbuttons>

                <apex:pageblocksection title="Internal Information" showheader="true" columns="2">
                    <apex:inputfield value="{!Opportunity.Contract_Type__c}" required="true"/>
                    <apex:inputfield value="{!Opportunity.Type}" required="true"/>
                    <apex:inputfield value="{!Opportunity.Contract_Form_Type__c}" required="true"/>
                </apex:pageblocksection>
                <apex:pageblocksection title="Account Information" showheader="true" columns="2">
                    <apex:inputfield value="{!Opportunity.AccountId}" required="true"/>
                    <apex:inputfield value="{!Opportunity.Account.BillingStreet}" required="true"/>
                    <apex:inputfield value="{!Opportunity.Account.BillingCity}" required="true"/>
                    <apex:inputfield value="{!Opportunity.Account.BillingState}" required="true"/>
                    <apex:inputfield value="{!Opportunity.Account.BillingPostalCode}" required="true"/>
                    <apex:inputfield value="{!Opportunity.Account.BillingCountry}" required="true"/>
                </apex:pageblocksection>  
                <apex:pageblocksection title="Contract Detail Confirmation" showheader="true" columns="2">
                    <apex:inputfield value="{!Opportunity.Details_are_Accurate__c}" required="true"/>
                </apex:pageblocksection>
                <apex:pageblocksection showheader="false" columns="1">
                    <apex:pageBlockSectionItem >
                        <apex:pagemessage severity="warning" title="Submitting for approval will lock the record for editing until the request is approved or rejected." strength="3" summary="Click Cancel to return to the Opportunity if you must make any other changes."/>
                    </apex:pageBlockSectionItem>
                </apex:pageblocksection>
        </apex:pageblock>  
    </apex:form>
</apex:page>

 
Raj VakatiRaj Vakati
Class
 
public class myExt {
    
    public ApexPages.StandardSetController stdCntrlr {get; set;}
    public String currentId {get;set;}
    public Opportunity opp {get;set;}
    public myExt(ApexPages.StandardController controller) {
        //  stdCntrlr = controller;
        currentId = controller.getrecord().id;
        if(opp!=null){
            opp =[Select Id from Opportunity where id=:currentId] ;
        }else{
            opp = new  Opportunity();
        }
        
    }
    
    
    
    public PageReference save() {
        PageReference pr ;
        try{
            insert opp;
            
            Account acc = opp.Account ; 
            acc.BillingState = opp.Account.BillingState ; 
// Add other fields 
            update acc;
            
        } catch(Exception  e){
            system.debug('Error Message==>'+e);
        }
        
        return pr;
    } 
    
}

Page :

<apex:page standardcontroller="Opportunity" lightningStylesheets="true" extensions="myExt">
    <apex:messages />
    <apex:sectionheader title="{!$ObjectType.Opportunity.label} Contract Review Request" subtitle="{!IF(ISNULL(opp.Name), 'New Opportunity',opp.Name)}"/>
    
    
    <apex:form >
        <apex:pageblock > 
            <apex:pageblocksection title="" showheader="false" columns="1">
                
                <apex:pageblock mode="edit" title="{!$ObjectType.Opportunity.label} Contract Review Request">
                    <apex:pageblockbuttons >
                        <apex:commandbutton value="Save & Submit" action="{!Save}"/>
                        <apex:commandbutton value="Cancel" action="{!Cancel}"/>
                    </apex:pageblockbuttons>
                    
                    <apex:pageblocksection title="Internal Information" showheader="true" columns="2">
                        
                        <apex:inputfield value="{!opp.Type}" required="true"/>
                    </apex:pageblocksection>
                    <apex:pageblocksection title="Account Information" showheader="true" columns="2">
                        <apex:inputfield value="{!opp.AccountId}" required="true"/>
                        <apex:inputfield value="{!opp.Account.BillingStreet}" required="true"/>
                        <apex:inputfield value="{!opp.Account.BillingCity}" required="true"/>
                        <apex:inputfield value="{!opp.Account.BillingState}" required="true"/>
                        <apex:inputfield value="{!opp.Account.BillingPostalCode}" required="true"/>
                        <apex:inputfield value="{!opp.Account.BillingCountry}" required="true"/>
                    </apex:pageblocksection>  
                    
                    <apex:pageblocksection showheader="false" columns="1">
                        <apex:pageBlockSectionItem >
                            <apex:pagemessage severity="warning" title="Submitting for approval will lock the record for editing until the request is approved or rejected."
                                              strength="3" summary="Click Cancel to return to the Opportunity if you must make any other changes."/>
                        </apex:pageBlockSectionItem>
                    </apex:pageblocksection>
                </apex:pageblock>
            </apex:pageblocksection>
        </apex:pageblock>
    </apex:form>
</apex:page>

 
Stephanie Boggs 17Stephanie Boggs 17
Thanks, Raj! This is not pulling in the existing field data onto the VF page for the related Opp and account, just blank fields.
Raj VakatiRaj Vakati
How are you using this page ? Can you give me your controller 
Stephanie Boggs 17Stephanie Boggs 17
I just realized that I didn't add the other fields on the class! Let me do that and get back to you.
Raj VakatiRaj Vakati
ok
Raj VakatiRaj Vakati
working ??
Stephanie Boggs 17Stephanie Boggs 17
Sorry this took my so long!

In prod, the account field data is being pulled into the VF page. The goal is to have it write back to the account if updated on the VF page. With the extension, it is no longer pulling in the existing field data, so I cannot see if it is updating. 

It also seems like it is trying to create a new opportunity, when I want it to update the existing opportunity they pushed the button from.