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
lekkala malyadri 8lekkala malyadri 8 

Custom Logic

Hi how to implement a custom save logic using standard controller on a custom sobject
Best Answer chosen by lekkala malyadri 8
nagendra 6989nagendra 6989
Hello,

As you want to implement a custom save logic on a custom object please follow the below code snippet.

VF Page:

<apex:page standardController="faculty__c" extensions="CustomSaveLogic">
    {!greeting} <p/>
    <apex:form >
     <apex:pageBlock title="Using standard controller implementing a custom save logic" >
      <apex:pageBlockButtons >
      <apex:commandButton value="Save" action="{!save}"/>
      <apex:commandButton value="Cancel" action="{!cancel}"/>
      </apex:pageBlockButtons>
       <apex:pageBlockSection >
        <apex:inputField value="{!faculty__c.name}"/> <p/>
        <apex:inputField value="{!faculty__c.Salary__c}"/> <p/>
        
       </apex:pageBlockSection>
     </apex:pageBlock>
        
    </apex:form>
</apex:page>

Controller Page : 

public class CustomSaveLogic {

    private final Faculty__c fct;
    
    // The extension constructor initializes the private member
    // variable fct by using the getRecord method from the standard
    // controller.
    public CustomSaveLogic(ApexPages.StandardController fctController) {
        this.fct = (faculty__c)fctController.getRecord();
    }

    public String getGreeting() {
        return 'Hello nagendra' + fct.name + ' (' + fct.id + ')';
    }
}

Mark it as solved if it helps you.

Thanks & Regards,
Nagendra.P

All Answers

nagendra 6989nagendra 6989
Hello,

As you want to implement a custom save logic on a custom object please follow the below code snippet.

VF Page:

<apex:page standardController="faculty__c" extensions="CustomSaveLogic">
    {!greeting} <p/>
    <apex:form >
     <apex:pageBlock title="Using standard controller implementing a custom save logic" >
      <apex:pageBlockButtons >
      <apex:commandButton value="Save" action="{!save}"/>
      <apex:commandButton value="Cancel" action="{!cancel}"/>
      </apex:pageBlockButtons>
       <apex:pageBlockSection >
        <apex:inputField value="{!faculty__c.name}"/> <p/>
        <apex:inputField value="{!faculty__c.Salary__c}"/> <p/>
        
       </apex:pageBlockSection>
     </apex:pageBlock>
        
    </apex:form>
</apex:page>

Controller Page : 

public class CustomSaveLogic {

    private final Faculty__c fct;
    
    // The extension constructor initializes the private member
    // variable fct by using the getRecord method from the standard
    // controller.
    public CustomSaveLogic(ApexPages.StandardController fctController) {
        this.fct = (faculty__c)fctController.getRecord();
    }

    public String getGreeting() {
        return 'Hello nagendra' + fct.name + ' (' + fct.id + ')';
    }
}

Mark it as solved if it helps you.

Thanks & Regards,
Nagendra.P
This was selected as the best answer
lekkala malyadri 8lekkala malyadri 8
Thanks nagendra for the help. i have tried the above code snippet its working.
Amritesh SahuAmritesh Sahu
Hi,

Reference following code :
VF - 
----------------------------------
<apex:page standardController="OBJECTNAME" extensions="EXTENSIONCONTROLLERNAME">
..
..
<apex:commandButton value="Save" action="SaveAction"/>
..
..
</apex:page>


Controller - 
----------------------------------
public class EXTENSIONCONTROLLERNAME {

	OBJECTNAME objRecord;

	public EXTENSIONCONTROLLERNAME(ApexPages.StandardController controller) {
        objRecord = (OBJECTNAME) controller.getRecord();
    }
	
	public Pagereference SaveAction() {
		insert objRecord;
		return new Pagereference('\' + objRecord.Id);
	} 
	
}


Regards,
Amritesh