You are currently viewing Data Structures in Salesforce Explained

Data Structures in Salesforce Explained

Salesforce, the world’s #1 CRM provider, provides a wide variety of tools to help organizations deal with large volumes of data sets. The Salesforce data model is designed to understand the relationship between different objects.

A data model is defined as the architectural structured way of storing and customizing data in an application. The Salesforce platform provides a standard model that allows the user to customize the data models for custom functionality. 

Apex in Salesforce is an object-oriented programming language that enables the user to develop custom business logic on the platform by writing Apex triggers, custom VisualForce controllers and extensions, anonymous Apex codes, etc. It is compiled, stored, and run entirely on the Force.com platform.

Every variable in Apex must be declared with the specific data type such as sObject, primitive, or enum. Data types are defined as the data storage format that a variable can store to perform a specific operation. 

Apex in Salesforce supports the following data types:

  • Primitive (Integer, Double, Long, Date, Datetime, String, ID, or Boolean)
  • Collections (Lists, Sets, and Maps)
  • sObject
  • Enums
  • Classes, Objects, and Interfaces

Primitive Data Types

Primitive data types include Integer, Double, Long, Date, Datetime, String, ID, Boolean, etc.

All primitive data types are passed by value, not by reference.

All Apex variables, including class member variables, are initialized to null. Before using the data variables, the users must ensure that they initialize variables to appropriate values.

Example:

Integer num = 10;

Boolean check = true;

String name = ‘Test’;

sObject Data Types

A sObject data type can be generic or non-generic. sObject declared with generic type can store any standard or custom object of Salesforce. While the non-generic sObject variable is declared for specific objects.

Generic sObject data types are useful when the users are utilizing SOSL queries that return results from multiple objects.

Example:

Sobject s = new Account();

Account a = new Account();

CustomObject__c c = new CustomObject__c();

Collections

List – A list is an ordered collection of elements that are identified by their indexes. 

  • A list contains up to four levels of nested collections. It contains elements of primitive types, sObjects, user-defined objects, Apex objects, or even other collections. 
  • A list can contain duplicate elements.

Set – A set is an unordered collection of elements that do not contain any duplicates elements. Set elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.  A set is used to store sObject record Ids.

Map – A map is a collection of key-value pairs. Keys and values can be any data type such as primitive types, collections, sObjects, user-defined types, and built-in Apex types. Keys are unique and map elements must be accessed by the key as the order of map elements is not constant.

The map is used to store Salesforce records with their IDs making it easier to iterate over the records.

Enums 

Enum type defines a fixed set of named constants. An enum is a data type that contains a fixed set of constants.

Variable in Salesforce

Variable is defined as a memory location in the Salesforce platform. A variable is a container that stores a specific piece of data collected from the customer or output from Salesforce. Context Variable is implicit variable, managed, and created to provide developers with the ability to access run time code.  It helps the users get information about the Apex Trigger.

Conclusion

Apex in Salesforce utilizes data types, variables, and related language constructs such as enums, constants, expressions, operators, and assignment statements. In Apex, all variables and expressions should initialize a data type, such as sObject, primitive, or enum. Apex uses the same primitive data types as the SOAP API.

Want to learn concepts of data structure and algorithm to improve the performance of your organization’s data? Choose the best Salesforce consultant in the UK to hire certified Salesforce developers.

Akshay

Akshay Dhiman

Chief Technical Officer (CTO)

“Akshay Dhiman, the CTO of Cloud Analogy, has been a standout and successful Salesforce Platform Developer for years. He has a rich experience in Salesforce Integration, JavaScript, APEX, VisualForce, Force.com Sites, Batch Processing, Lightning, PHP, C++, Java, NodeJs, ReactJs, Angular 8, GraphQL, React Native, Web Technology, and jQuery. Known for his problem-solving and debugging skills, Akshay is an out-of-the-box thinker and his capability to understand the business context and translate it into a working model is par excellence. Akshay would not only translate his thoughts into reality but would also bring in his own perspective that is always a tremendous value add. Akshay has the knack of taking challenges head on, equipped with In-depth industry knowledge, Resourcefulness and uncanny nag to build relationship with anyone in shortest time possible. Not only does he possesses fantastic technical depth and awareness but Akshay also complements them with a profound understanding of business functionalities, tools, and methodologies. He has the rare combination of skills and talent that one looks for in Salesforce – attention to detail and the drive for innovation.”

Leave a Reply