+ Start a Discussion
Dave BerenatoDave Berenato 

Override Lightning Action Save Button in Custom Visualforce Page with Apex

I have a Lightning Action that is loading custom Visualforce to allow the user to update fields on a child record (Contact_Phone_Number__c) and the parent record (Lead) in a single Edit:

User-added image

I was able to find code to reactivate the Save Button at the bottom right:
 
<apex:page standardController="Contact_Phone_Number__c" extensions="PhoneUpdateLead" showHeader="false" >
    <html>
	<script type='text/javascript' src='/canvas/sdk/js/publisher.js'>
        // Activate 'Save button' when the panel shows up 
        Sfdc.canvas.publisher.subscribe({name: "publisher.showPanel", 
                                         onData:function(e) { Sfdc.canvas.publisher.publish({
                                             name:"publisher.setValidForSubmit", payload:"true"}); }}); 
        // Subscribe to the Save button to influence custom logi 
        Sfdc.canvas.publisher.subscribe({ name: "publisher.post", 
                                         onData: function(e) { 
        // **TODO** // Do whatever you want to do here when the user clicks save button //** END TODO** 
        // Gracefully close the popup when user taps save 
        
                                             {!savePhone}                                     
                                             
        Sfdc.canvas.publisher.publish({ name: "publisher.close", payload:{ refresh:"true" }}); }}); 
     </script>
  <body> 
    <apex:form >
        <apex:pageBlock mode="maindetail">
            <apex:pageBlockSection columns="2">
                <apex:inputField value="{!phone.User_Disposition__c}"/>
                <apex:inputField value="{!l.Status}"/>
                </apex:pageBlockSection>
        </apex:pageBlock>
        
        <div class="slds-align--absolute-center">
	<div class="slds-is-relative">
	 </div>
	<apex:commandButton value="Save" action="{!savePhone}"/>
	</div>
    </apex:form>
  </body>
</html>
</apex:page>

Essentially I am updating fields on both the parent and child record. Ideally the Save button would complete this "savePhone" process in my Apex.
 
public class PhoneUpdateLead {
    
    public string phoneId {get;set;}
    public string LeadId {get;set;}
    public string AppId {get;set;}
    public Contact_Phone_Number__c phone{get;set;}
    public Lead l{get;set;}
    
  public PhoneUpdateLead(ApexPages.StandardController stdcontroller) {
        
    phoneId = stdController.getId();
        
        List<Contact_Phone_Number__c> phonelist = [Select Id,Name,Lead__c,User_Disposition__c,Lead__r.Id FROM Contact_Phone_Number__c Where Id = :phoneId];
        
        if(!phonelist.isEmpty()) {phone = phonelist[0];} else{system.debug('Phone List is Empty');}
        if(!phonelist.isEmpty()) {LeadId = phonelist[0].Lead__r.Id;} else{system.debug('Lead List is Empty');}
        
        List<Lead> leadlist = [Select Id,Name,Status
                                 FROM Lead Where Id = :LeadId];
        
        if(!leadlist.isEmpty()) {l = leadlist[0];} else{system.debug('Lead List is Empty');}

  }
       public void savePhone(){
           update phone; update l;}
}

But the error message when I place {!savePhone} in the Javascript is:
 
Unknown property 'Contact_Phone_Number__cStandardController.savePhone'

 
Deepali KulshresthaDeepali Kulshrestha
Hi Dave,
Greetings to you!

- Yes, it will give an error because Contact_Phone_Number__c is a custom object it is not a standard object.
- In VF page for a standard object like Account, Contact, Lead, etc we use  'standardController' but for the custom object you have to use controller=" Contact_Phone_Number__c".
- So please use the below line of code : -
controller="Contact_Phone_Number__c"
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
Alain CabonAlain Cabon
Hi Dave,

public PageReference savePhone()
 
public PageReference savePhone() {
        try {
             update phone;
             update l;
        } catch(System.DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }      
      // return to the same page (not ideal after a correct update) we prefer to return to the detail view
        return null;
  }
Deepali KulshresthaDeepali Kulshrestha
Hi Dave,
Greetings to you!

- Yes, it will give an error because Contact_Phone_Number__c is a custom object it is not a standard object.
- In VF page for a standard object like Account, Contact, Lead, etc we use  'standardController' but for the custom object you have to use controller="Contact_Phone_Number__c" .
- So please use the below line of code : -
controller="Contact_Phone_Number__c"
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.