Blog

Custom Controller in Salesforce

  • (5.0)
  • | 2378 Ratings
Custom Controller in Salesforce

Custom Controller in Salesforce

A custom controller is an Apex class that implements all of the logic for a page without leveraging a standard controller. Use custom controllers when you want your Visualforce page to run entirely in system mode, which does not enforce the permissions and field-level security of the current user.

A controller extension is an Apex class that extends the functionality of a standard or custom controller. Use controller extensions when:

  • You want to leverage the built-in functionality of a standard controller but override one or more actions such as edit, view, save, or delete.
  • You want to add new actions.
  • You want to build a Visualforce page that respects user permissions. Although a controller extension class executes in system mode, if a controller extension extends a standard controller, the logic from the standard controller does not execute in system mode. Instead, it executes in user mode, in which permissions, field-level security, and sharing rules of the current user apply.

Syntax

<apex:page Controller=”MyControllerClass”>

Sample Code#1:

To execute the below code of controller, first, we need to create “APEX Class”, and then proceed to create the Visualforce Page - Login to Salesforce → Setup → Build → Develop → Visualforce Pages(Click on it) → Click on New button.

Scenario: Build a sample, Visualforce Page, using the controller in which, if we enter the name in the field or attribute, it should show “Welcome” along with that name.

Visualforce Page Sample Code#1:

<apex:page Controller= "Example">
<apex:form>
		<apex:pageBlock title="My Content">
			<apex:pageBlockSection title="My Content Section" columns= "2">
				<apex:outputLabel>Enter Name: </apex:outputLabel>
				<apex:inputText/> value ="{!Username}"/>
				<apex:commandButton value="Click Me" Action=”{!ShowMessage}” rerender=”Message To Show”/>
				<apex:outputLabel id=”Message To Show">{!Message} </apex:outputLabel>
			</apex:pageBlockSection>
		</apex:pageBlock>
	</apex:form>
</apex:page>

APEX Class Sample Code#1:

APEX Class Sample Code#1:

Public with sharing class example{
	Public string UserName{set;get}
	Public string Message{set;get}

	Public void ShowMessage(){
		message=’Welcome’ + UserName;
}
}

samplevfp

Explanation of Code:

<apex:outputLabel>Enter Name: </apex:outputLabel>  → To show on the screen “Enter Name”.

{!XXXXXX} → It means it has connection to controller.

<apex:commandButton value="Click Me" Action=”{!ShowMessage}”/> → To show Button on the screen.

<apex:inputText/> value ="{!Username}"/>  → Value binding

Action=”{!ShowMessage}”/>  → Function binding.

How to set security for APEX Class?

By using “with sharing” keyword we can set security for the APEX Class.

Checkout Salesforce Interview Questions

AKA APEX Sharing: AKA With Sharing / Without Sharing.

When we execute any APEX Class, it works in system mode AKA → It won’t consider what user has rights or not to see some data related to data processed in the class. It works without keeping any user limits in mind, and gives full access to all data, whereas, when we execute VFP, then it will work on user mode. It will check what user has access to, and then only, it will show the things on the page.

Default APEX Code:

Sample Code:

Public <*******> class MyClass{
	//some code
}

****** = We can use “With Sharing” or “Without Sharing”.

Without Sharing: When we use this keyword before the class name, then if suppose a user Daniel is calling this class by anyway, this class is working only on Account records.

Eg: In total, there are 100 records in Account object. Daniel has access to only 50 records. In this case, also, the class returns 100 records or works on those 100 records.

It means, using “without sharing” APEX does not check what all records the user has access to. It is the default option if we do not mention anything before the class.

With Sharing: When we use “With Sharing” before class, then it will work only on which the user has access to

Eg: In total, there are 100 records in Account object. Daniel has access to only 50 records. In this case, the class returns 50 records only. It is called running APEX in User mode, and it is more secure.

Checkout SalesForce Tutorial

How many types of Bindings in Visualforce Page?

We have three types of bindings, they are:

  • Data binding.
  • Action Binding
  • Component Binding.

How to build a Component Binding?

Step to create Component Binding:

Go to Setup → Develop → Components → then write the component name “DemoComponent”.

Code#2:

<apex:component>
<apex:attribute name=”textValue” description=”This is the value for component” type=”String” required=”true”/>
<apex:attribute name=”textColor” description=”This is color for the border” type=”String” required=”true”/>
<apex:outputText value=”{!textValue}” style=”color:{!textColor}/”>
</apex:component>

For Creating New Visualforce Page:

<apex:page tabStyle= "Account">
		<apex:pageBlock>
			<apex:pageBlockSection title="My Demo Component">
				<c:DemoComponent textValue=”This text is blue” textColor=”blue” />
				<c:DemoComponent textValue=”But this is red” textColor=”red” />
				<c>
			</apex:pageBlockSection>
		</apex:pageBlock>
</apex:page>

In the next topic, we will discuss in detail about Controller Extension in Salesforce”. Keep following us for more info on Salesforce Development/Programming.

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download Now!

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

Arogyalokesh
About The Author

Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.


DMCA.com Protection Status

Close
Close