November 2, 2024

Salesforce Developer Tutorial: Get Started With Salesforce Programming

Are you aspiring to be a software application developer? Do you want to build your own application on the Force.com platform? If your answer to these questions are yes, then you should definitely consider becoming a Salesforce developer.

In my previous blogs, I have discussed about Salesforce, Salesforce certifications and also shown you to build a custom application using the declarative options available in Salesforce. In this blog, I will discuss about the programmatic options available in Salesforce to develop your application.

MVC Architecture

Before I dive into building an application using Visualforce and Apex, I will first discuss about the Salesforce Model-View-Controller architecture. Below is a diagram that outlines the Salesforce Model-View-Controller architecture along with the different Salesforce components.

Model: The model is your Salesforce data objects, fields and relationships. It constitutes of standard (Account, Opportunity, etc) and custom objects (objects you create).

View: The view represents the presentation of the data i.e the user interface. In Salesforce, the view constitutes of the visualforce pages, components, page layouts and tabs.

Controller: The controller is the building block of the actual application logic. You can perform actions whenever the user interacts with visualforce.

Salesforce in Action

To be a Salesforce developer, you need to first know how Salesforce applications work and getting Salesforce PD1 Certification would be the best choice. Below is an image which gives you the complete picture of Salesforce in action. The client or user either requests or provides information to the Salesforce application. This is generally done using Visualforce. This information is then passed on to the application logic layer, written in Apex. Depending upon the information, data is either inserted or removed from the database. Salesforce also provides you with the option of using web services to directly access the application logic.

A Salesforce developer can approach development either using the declarative or programmatic options. Below is an image which provides you with details on both the declarative and programmatic approaches available at each of the user interface, business logic and data model layer. To build your user-interface, you can either use the declarative approach which is using page layouts and records types or use programmatic approach like visualforce pages and components. Generally, you should use programmatic approach only when you can not achieve the necessary user-interface using the declarative approach. To develop your application’s business logic layer you can either use the Salesforce declarative options of workflow, validation rules and approval processes or use programmatic approach like triggers, controllers and classes. To access the data model, you can use the declarative approach using objects, fields and relationships. You can also access the data model programmatically using metadata API, REST API and bulk API.

We have seen how Salesforce applications work, the MVC architecture used for development in Salesforce and the two different approaches that are available for a Salesforce developer. Now, let me discuss about Visualforce and Apex.

Visualforce

To build applications on the Salesforce platform you need to know how to develop user-interface and write application logic. As a Salesforce developer, you can develop the user-interface using Visualforce. Visualforce is the user-interface framework for the Force.com platform. Just like how you can use javascript Angular-JS framework to build user-interfaces for your websites, you can use Visualforce to design and build user-interfaces for your Salesforce applications. You can learn more from the Salesforce admin training.

You can use visualforce whenever you need to build custom pages. Few examples of situations where you can use Visualforce are:

  • To build email templates
  • To develop mobile user-interface
  • To generate PDF’s of data stored in Salesforce
  • To embed them into your standard page layouts
  • To override a standard Salesforce page
  • To develop custom tabs for your application

A visualforce page consists of two primary elements:

  • Visualforce Markup – The visualforce markup includes the visualforce tags, HTML, JavaScript or any other web-enabled code.
  • A Visualforce Controller – The visualforce controller contains the instructions that specify what happens when a user interacts with a component. The visualforce controller is written using Apex programming language.

You can take a look at a simple Visualforce page code along with the different components below:

Below I have shown you the steps to write a simple visualforce page for displaying countries and their currency values:

Step 1: From Setup, enter Visualforce Pages in Quick Find box, then select Visualforce Pages and click New.

Step 2: In the editor add the following code to display country and its currency value:

<apex:page standardController = “country__c”>

<apex:pageBlock title=”Countries”>

<apex:column value=”{!country__c.Name}”/>

<apex:column value=”{!country__c.currency_value__c}”/>

</apex:pageBlock>

</apex:page>

To learn in-depth about Workflow in Salesforce, sign up for an industry-based Salesforce course.

Apex

Once you are done developing the user-interface, as a Salesforce developer you need to know how to add custom logic to your application. You can write controller code and add custom logic to your application using the Apex programming language. Apex is an object oriented programming language that allows you to execute flow and transaction control statements on the Force.com platform. If you have used the java programming language before then you can easily learn Apex. Apex syntax is 70% similar to that of java.

You can use Apex whenever you want to add custom logic to your application. Few examples of situations where you can use Apex are:

  • When you want to add web and email services to your application
  • When you want to perform complex business processes
  • When you want to add complex validation rules to your application
  • When you want to add a custom logic on operations like saving a record

Below is a screenshot of Apex code along with its different components like looping statement, control-flow statement and SOQL query:

Now that we have understood what Apex is and when to use it, let me dive deep into Apex programming.

Programming In Apex

If you have understood the concepts described above, then you are halfway through your journey in becoming a Salesforce developer. In this section, I will dive deeper into Apex by providing you with information on the different data types and variables, different ways of retrieving data from the database and showing you how to write a class and method.

Related Article: Salesforce Developer Interview Questions

Datatypes And Variables

Salesforce offers you with 4 different data types and variables. The below table provides you with information on each of the 4 data types:

Data Types And Variables Description Example
Primitive Primitive data types in Salesforce include boolean, date, integer, object, string and time. Boolean isSunny = true;Integer I = 1;

String myString = “Hello World”;

sObjects sObject refers to any object that can be stored in the database. Account a = new Account();MyCustomObj__c obj = new MyCustomObj__c();
Collections Apex has the following types of collections: List<String> var_lst = new List<String>();Set<String> setOne = new Set<String>();

Map<String, String> var_map = new Map<String, String>();

Enums Enums are abstract data types with values that take on a finite set of identifiers. Public enum Seasons {Winter, Spring, Summer, Fall};

SOQL And SOSL

Developing software applications requires you to know how to insert and retrieve data from databases. In Salesforce, you can retrieve data from the databases using SOQL and SOSL. If you want to be a Salesforce developer, then you must know both of these query languages. I have provided you with a detailed explanation of these languages below:

  • SOQL stands for Salesforce Object Query Language. Using SOQL statements, you can retrieve data from the database as a list of sObjects, a single sObject or an Integer for count method. You can think of the SOQL as an equivalent of a SELECT SOQL query. I have provided an example of a SOQL query below:

List<Account> accList = [SELECT Id, Name FROM Account WHERE Name=”YourName”];

  • SOSL stands for Salesforce Object Search Language. You can use SOSL statements to retrieve a list of sObjects, where each list contains the search results for a particular sObject type. You can think of SOSL as an equivalent to a database search query. I have provided an example of a SOSL query below:

List<List<sObject>> searchList = [FIND ‘map*’ IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead];

You can use SOQL when you know which object the data resides in and use SOSL when you don’t know the name of the object where the data resides.

Classes And Methods

Like in every other object oriented programming language, you can develop classes and methods using Apex. You can think of a class as a blueprint using which individual objects are created and used. You can think of a method as a subprogram, which acts on data and returns a value. I have provided you with the syntax to write a class and method below:

I will now show you how to add a class and method in Apex:

Step 1: From setup enter Apex Classes in QuickFind Box, then select Apex Classes and click New.

Step 2: In the editor add the following class definition:

Public class HelloWorld {

}

Step 3: Add a method definition between the class opening and closing brackets:

Public static void helloWorldMethod(Country__c[] countries) {

For ( Country__c country : countries){

country.currency_value__c *= 1.5;

}

}

Step 4: Click on Save and you should have your full class as:

Public class HelloWorld {

Public static void helloWorldMethod(Country__c[] countries) {

For ( Country__c country : countries){

country.currency_value__c *= 1.5;

}

}

You can use the syntax and example shown above to develop your own classes and methods for your Salesforce application. To become a Salesforce developer you need to know more than just writing classes and methods. In the next few sections, I will discuss topics which make developing applications on the Salesforce platform simple and easy.

Triggers

Every Salesforce developer must know the concept of Salesforce triggers. You might have previously come across triggers while working with other databases. Triggers are nothing but stored programs that get invoked when you perform actions before or after changes to Salesforce records. For example, triggers can run before an insert operation is performed or when an update operation is performed. There are two types of triggers:

  • Before trigger – You can use before triggers to update or validate record values before they are saved to the database.
  • After trigger – You can use after triggers to access field values that are set by the system and to affect changes in other records.

Triggers get executed before or after the below operations:

  • Insert
  • Update
  • Delete
  • Merge
  • Upsert
  • Undelete

I will show you how to add a trigger in apex by adding a trigger for the Country object which you have seen in the class above:

Step 1: From the object management settings for country, go to Triggers and click on New.

Step 2: In the trigger editor, add the following trigger definition:

Trigger HelloWorldTrigger on Country__c (before insert) {

Country__c countries = Trigger.new;

HelloWorld.helloWorldMethod(countries);

}

The above code will update your country’s currency before every insert into the database.

Governor Limits

You might know that Salesforce works on multi-tenant architecture, this means that resources are shared across different clients. To make sure no one client monopolize the shared resources, Apex run-time engine strictly enforces governor limits. Enroll for Salesforce architect certification training online and learn more about salesforce and multi-tenant architecture. If your Apex code ever exceeds a limit, the expected governor issues a run-time exception that cannot be handled. So, as a Salesforce developer you have to be very careful while developing your application.

Bulk Operations

As a Salesforce developer, you have to always ensure that your code maintains the governor limits. To make sure Apex adheres to governor limits, you must use the bulk calls design pattern. A bulk operation refers to committing more than one record when you make a DML operation. Before you make a DML operation you have to always make sure that you add the rows into a collection. Below is an image that gives you a complete description of the bulk operation design pattern.

DMLs And Data Operations

You have seen earlier how to retrieve data from the database using SOQL and SOSL queries. Now let’s take a look at the different statements that you can use to insert data into the Salesforce database. For a Salesforce developer, it is a must to know what these statements can do and how to use them.

DML Statement Description
Insert Adds one or more sObjects to your organization’s data
Update Modifies one or more existing sObject records
Upsert Creates new records and updates sObject records
Delete Deletes one or more existing sObject records
Undelete Restores one or more existing sObject records
Merge Merges upto three records of the same sObject type into one record

Visualforce And Apex

You have come a long way in your quest to become a Salesforce developer. I will next discuss about how you can integrate your visualforce page and your apex code. You can connect your visualforce page and your apex code by using controllers and extensions.

  • Custom Controllers – When you want your visualforce page to run entirely in system mode i.e without permissions and field-level security, use a custom controller.
  • Controller Extension – When you want to add new actions or functions that extend the functionality of a standard or custom controller, use a controller extension.

In the code below, I have shown you how to include custom controller in your visualforce page:

<apex:page controller=”myController” tabStyle=”Account”>

<apex:form>

</apex:form>

</apex:page>

In the code below, I have shown you how to include controller extension in your visualforce page:

<apex:page standardController=”Account” extensions=”extensionClass”>

<apex:form>

</apex:form>

</apex:page>

Exception Handling

If you have developed applications before, then you would have definitely come across exceptions. An exception is a special condition that changes the normal flow of program execution. For example, dividing a number by zero or accessing a list value that is out of bounds. If you don’t handle these exceptions, then the execution of process stops and DMLs will be rolled back.

As a Salesforce developer, you need to know how to catch these exceptions and what to do once you catch them. To catch exceptions you can use the try, catch and finally construct. Once you have caught the exception, then you can handle it in ways mentioned below:

Exception How To Handle It
DML Use the addError() method on a record or a field
Visualforce Use ApexPages.message class
Sending an email on exception You can notify the developer by email
Logging in a custom object You can use a future method to catch a custom object

Till now in this Salesforce developer blog you have seen how to develop your user-interface using Visualforce, you have seen how to write custom logic using Apex and different concepts like triggers, bulk operations and exception handling. Last but not the least, we’ll take a look at the Salesforce testing framework.

Testing

As a Salesforce developer, you need to know how to test the code you write. Test driven development is a good way of ensuring long-term success of your software application. You need to test your application so that you can validate that your application works as expected. Especially, if you are developing an application for a customer then testing it before delivering the final product is very important. Apex provides you a testing framework that allows you to write unit tests, run the tests, check test results and have code coverage results.

You can test your application in two ways:

Leave a Reply

Your email address will not be published. Required fields are marked *