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
SmytaSmyta 

'Save' action override-for the standard and custom object in same VF page

Hello,

I have a VF page with standard controller 'Account' and controller extension 'AccountExtensionController' which has code to override the default controller 'Save' action and it is working fine. I am also displaying 'BillingAccount' information on the same VF page, "BillingAccount' is the Custom Object and Child of an 'Account' object, now I want to edit & Save some information on this Custom object. Tried writting code for this custom object 'Save' action in the same controller extension but it is not allowing, I guess because it is the extension for Standard controller 'Account'. 
Note :- I have 2 seperate blocks for 'Account' and 'BillingAccount' information and allowing to edit them seperately that means there are two different 'Save' buttons

Could anyone tell me how to achieve this.

Thanks,
Smita
 
Best Answer chosen by Smyta
SmytaSmyta
Hi Sohel,

 Thanks for your help, just updating on this issue.
Above thing did not work for me but got a very simple solution.. We just have to use relationship name to update the related object. That is 'Account' object is the parent of 'CustomerAccount' and if the relationship name is 'BillingAccounts' then the related object modified on the VF page can be saved using following;

public with sharing class AccountExtensionController {

public Account acc {get; set;}

public AccountExtensionController(ApexPages.StandardController controller) {
        acc = (Account) controller.getRecord();
}

public PageReference save() {

 try{
            update acc;
            update acc.BillingAccounts__r;
            ApexPages.Message msg1 = new ApexPages.Message(ApexPages.Severity.INFO, 'Account saved');
            ApexPages.addMessage(msg1); 
            return null;
           }
        catch(Exception e) {
           ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Account could not be saved');
           ApexPages.addMessage(msg); 
           return null;  
        }

Regards,
Smita
 

All Answers

SFDC GuestSFDC Guest
Hi Smita,

Please use the below code for your requirement. Using this page, we can insert Account, and BillingAccount__c object.
Here BillingAccount__c is child of Account, and Account__c is lookup field in BillingAccount__c object.
I have overrided standard "save" button with "customSave".
<apex:page StandardController="Account" extensions="AccountExtensionController">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection title="Account Information">
                <apex:inputField value="{!Account.Name}"/>
                <apex:inputField value="{!Account.AccountNumber}"/>
            </apex:pageBlockSection>
            
            <apex:pageBlockSection title="Billing Account Information">
                <apex:inputField value="{!billingAcc.Name}"/>
            </apex:pageBlockSection>
            
            <apex:pageBlockButtons>
                <apex:commandButton value="Save" action="{!customSave}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>
        
            
            
            
public class AccountExtensionController
{
    public Account acc {get; set;}
    public BillingAccount__c billingAcc {get; set;}
    public AccountExtensionController(ApexPages.StandardController controller)
    {
        acc = (Account)controller.getRecord();
        billingAcc = new BillingAccount__c();
    }
    public PageReference customSave()
    {
        try{
        insert acc;
        billingAcc.Account__c= acc.Id; //Here Account__c is the lookup field for Account
        insert billingAcc;
        upsert acc;
        PageReference p = new PageReference('/'+acc.Id);
        p.setRedirect(true);
        return p;
        }
        catch(Exception ex)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Account could not be saved'));
            return null;
        }
    }
}

Please correct the field names as there may be typos errors, and mark it as best answer if it is solved your problem.
Thank You,
Sohel Mohd
SmytaSmyta
Hi Sohel,

I am trying to update the billingAccount and it should not allow insert, tried the above solution but somehow it is not binding acc Id with the billingAccount and giving me following error

 EXCEPTION_THROWN [42]|System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

following is my code snippet;
Note : saveBillAcc() is tthe method for BillingAccount save

public with sharing class AccountExtensionController {
    
    public ApexPages.StandardController myAccountController {get; set;}
    public Account acc {get; set;}
    public CustomerAccount__c billAcc1 {get;set;}  
   
    
    public AccountExtensionController(ApexPages.StandardController controller) {
        myAccountController = controller;
        acc = (Account) controller.getRecord();
        billAcc1 = new CustomerAccount__c();
        billAcc1.Account__c = acc.Id;

       }
    
    public PageReference save() {
        try{
            update acc;
          }
        catch(Exception e) {
          ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Account could not be saved')); 
          return null;  
        }
        return null;        
     }
    
   public PageReference saveBillAcc() {
       try{
            update billAcc1;
          }
        catch(DmlException e) {
          ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'BillingAccount could not be saved')); 
          return null;  
        }
     return null;

  }
SFDC GuestSFDC Guest
Hi Smyta,

We can save the parent and child records in one insert call. Please use the below code.

public with sharing class AccountExtensionController {
    
    public ApexPages.StandardController myAccountController {get; set;}
    public Account acc {get; set;}
    public CustomerAccount__c billAcc1 {get;set;}  
   
    
    public AccountExtensionController(ApexPages.StandardController controller) {
        myAccountController = controller;
        acc = (Account) controller.getRecord();
        billAcc1 = new CustomerAccount__c();
    
       }
    
    public PageReference save() {
        try{
            upsert acc;
            upsert billAcc1;
            billAcc1.Account__c = acc.Id;
            updsert billAcc1;
            PageReference p = new PageReference('/'+acc.Id);
            p.setRedirect(true);
            return p;
          }
        catch(Exception e) {
          ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Account could not be saved')); 
          return null;  
        }
      
     }
  }

Please mark it as best answer if it is solved your problem.

Thank You,
Sohel Mohd
SmytaSmyta
Hi Sohel,

Thanks for your help, but this is not working it's still throwing same error.

--Smita
SFDC GuestSFDC Guest
Hi Smyta,

Please use the below code.

public with sharing class AccountExtensionController {
    
    public Account acc {get; set;}
    public CustomerAccount__c billAcc1 {get;set;}  
   
    
    public AccountExtensionController(ApexPages.StandardController controller) {
        acc = (Account) controller.getRecord();
        billAcc1 = new CustomerAccount__c();
    
       }
    
    public PageReference save() {
        try{
            insert acc;
            billAcc1.Account__c = acc.Id;
            updsert billAcc1;
            PageReference p = new PageReference('/'+acc.Id);
            p.setRedirect(true);
            return p;
          }
        catch(Exception e) {
          ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Account could not be saved')); 
          return null;  
        }
      
     }
  }

<apex:page StandardController="Account" extensions="AccountExtensionController">
    <apex:form>
    
    <apex:pageBlock>
        <apex:pageBlockSection  title="Account Information">
            <apex:inputField value="{!acc.Name}"/>
        </apex:pageBlockSection>
        
        <apex:pageBlockSection title="Billing Information">
            <apex:inputField value="{!billAcc1.Name}"/>
        </apex:pageBlockSection>
        
        <apex:pageBlockButtons>
            <apex:commandButton value="Save" action="{!save}"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
    </apex:form>
</apex:page>


Please mark it as best answer if it is solved your problem.

Thank You,
Sohel Mohd
SmytaSmyta
Hi Sohel,

 Thanks for your help, just updating on this issue.
Above thing did not work for me but got a very simple solution.. We just have to use relationship name to update the related object. That is 'Account' object is the parent of 'CustomerAccount' and if the relationship name is 'BillingAccounts' then the related object modified on the VF page can be saved using following;

public with sharing class AccountExtensionController {

public Account acc {get; set;}

public AccountExtensionController(ApexPages.StandardController controller) {
        acc = (Account) controller.getRecord();
}

public PageReference save() {

 try{
            update acc;
            update acc.BillingAccounts__r;
            ApexPages.Message msg1 = new ApexPages.Message(ApexPages.Severity.INFO, 'Account saved');
            ApexPages.addMessage(msg1); 
            return null;
           }
        catch(Exception e) {
           ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Account could not be saved');
           ApexPages.addMessage(msg); 
           return null;  
        }

Regards,
Smita
 
This was selected as the best answer