Introduction
Apex is a strongly typed, object-oriented programming language used to execute code in the Salesforce instance. You may define classes in Apex with the Force.com platform. The syntax of an Apex class is very much similar to the java class and also gives built-in support for database operations. Here is the syntax below how to create a simple Apex class in the Salesforce.
Public class MyFirstApexClass
{ // body of the Apex class. Here we can define variables and methods }
In Salesforce, you should use the keyword “class” followed by the access specifier. To define an Apex class in Salesforce, three things are mandatory. These are access specifiers (public, private, global), class keyword, and the name of the class. In this blog, we will learn all about String Class Salesforce.
How to create an Apex class in Salesforce?
To create an Apex class in the Salesforce, go to the Setup -> Build -> Develop -> Apex Class
and click on the “NEW” button to create a class there.
One of the most basic examples for creating an Apex class is given below.
Read: The Ideal Salesforce Admin Tutorial For Beginners With Career Path & Certifications
The objective of this simple program is to create a new account by using Apex methods. You can write more similar examples ahead once you will understand the concept thoroughly.
Enroll for an online Salesforce training and Ace the race of becoming a much demanded Salesforce professional
Salesforce Training For Administrators & Developers
- No cost for a Demo Class
- Industry Expert as your Trainer
- Available as per your schedule
- Customer Support Available
Create an Apex class from Force.com IDE
These are the simple steps for creating an Apex class from Force.com IDE.
- In the first step, open the Force.com Eclipse IDE.
- Create a new project now by clicking on File -> New -> Apex class.
- Give a proper name to the class then select the “OK” option.
- Once these three steps are followed successfully, a new class is created.
Access modifiers in Salesforce for an Apex Class
Read: Ultimate Guide to Salesforce Relationships: Types, Best Practices and Latest Trends
- Public: If a class is declared as public then it implies that class is accessible to everyone in your organization and your defined namespace. Usually, most of the Apex classes are defined with the public keyword.
- Private: If a class is declared as private then it can be used locally and you cannot access it outside. By default, a class is set private.
- Global: If a class is declared as Global then it is accessible by all Apex codes irrespective of your organization. It is not recommended using the “Global” keyword until required.
Sharing Modes in Salesforce For An Apex Class
- With Sharing: This is a special feature of Apex class in the Salesforce. When a class is defined using “with sharing” keyword then it has the following implication - every time the class is executed, it will obey the class permissions and users’ access settings. This is also called the user mode.
- Without Sharing: When a class is defined using “without sharing” keyword then the class is made to run in the system mode.
- Virtual: With the virtual keyword, a class can be extended or overridden. To override a class, always add a virtual keyword within a class.
- Abstract: If a class is declared as “abstract” it only contains the signature of the method, not the actual implementation of the class.
Now learn Salesforce at with our Self-learning training module
Learn Salesforce in the Easiest Way
- Learn from the videos
- Learn anytime anywhere
- Pocket-friendly mode of learning
- Complimentary eBook available
Why choose Apex as a Language?
Let us discuss a few features of Apex as a programming language –
- Easy Syntax: Apex is easy to use a programming language with a simpler syntax like Java. For example – variable declaration, loop syntax, or conditional statements, etc.
- Integrated: Apex has built-in support for various database operations like INSERT, UPDATE, DELETE, etc. It also supports DML exceptional handling too and inline query handling.
- Strongly typed: Apex is a strongly typed object-oriented programming language that uses the direct reference to schema objects like sObject. The invalid reference quickly fails if you use the wrong data type or it is deleted.
- Strongly integrated with data: Apex is data-focused and designed to execute multiple queries and DML statements together. It issues multiple transaction statements on Database.
- Multitenant environment: Apex runtime engine is designed to run the code in a multitenant environment and preventing it from monopolizing shared resources. If there is some code that violates limits then Apex will display easy to understand the error message.
- Automatic Upgrades: Apex is upgraded automatically with Salesforce releases. You don’t have to upgrade it manually.
- Easy to test: Apex provides built-in support for unit testing and execution of test cases that make the final code more efficient.
Why choose Apex classes for application development?
Apex should be used when you are not able to implement complex business functionality with pre-built features. Here are the cases when Apex classes should be used by developers for application development.
- To create web services that need to be integrated with other systems.
- To create web services for email setup or email blast.
- To perform complex validation over multiple objects at the same time and also the custom validation implementation.
- To create complex business processes that are not supported by the existing workflow.
- To create custom transactional logic using database methods to update the records.
- To perform some logic when the record is modified by the related object’s record.
Apex string class in Salesforce
A string Apex class in the Salesforce contains methods for string primitive data types. Here is the list:
Read: Top 63 Salesforce Lightning Interview Questions and Answers
- abbreviate (maxWidth): It returns an abbreviated version of the string of the specified length. If the current string is longer than the specified length then it will return the original string in that case.
- abbreviate (maxWidth, Offset): It returns an abbreviated version of the string, starting at the specified character offset and of the specified length.
- Capitalize (): It returns the current string with the first letter changed to the title case.
- Center (Size): It returns the current String padded with spaces on the left and the right to appear it in the center.
- Code point count (): It returns the total number of Unicode code points within the specified text range.
- CompareTo (): It will compare the two strings on the basis of the Unicode values of each character in the String.
- Contains (): The method returns true if a particular string contains the substring that you want to find out otherwise method returns fail.
- Contains Whitespace (): The method returns true if the current string contains any whitespaces otherwise method returns fail.
- Delete whitespace (): It will delete the whitespaces if the current string contains any.
- Difference (): The method returns the differences between two strings that are compared together
- Equal (): The method returns true if the current string and the specified string are equal other method returns fail. This method is generally used for case-sensitive comparisons.
- Format (string, arguments): This method is used to format or substitute a string. The substitution and formatting are considered the same in many cases. You have to extra cautious here when formatting a particular string.
These are a few popular String class methods Salesforce. However, there are many more that can be used based on the requirement. Moving ahead, let us understand the Apex syntax in little detail below.
A Deep look at the Apex Syntax for String class
Apex code contains many things that are common and familiar with other programming languages. It includes the variable declaration, SOQL Query, Loop Statement, Flow control statement, DML statements etc. let us discuss each of them one by one.
- Variable Declaration: As we know, Apex is a strongly typed programming language where every variable must be declared with a suitable data type in Apex. If the data type is not defined for the variable then Apex will show an error message when the code is executed.
- Loop Statement: The loop statement is generally used for iterating over a piece of code for a specified number of times. The iteration limit is equal to the value defined in the code within the loop statement. Loop statements are common in almost all programming languages.
- SOQL Query: It is used to fetch the desired data from the Salesforce database. There are multiple SOQL queries in the Salesforce that are used by developers on the basis of requirement.
- Flow Control statement: The if statement is used by almost all programming languages for flow control. Based on the condition, it is decided whether the code should go for execution or to stop the execution for the particular piece of code. Keep in mind that flow control statements should be used wisely when required.
- DML Statement: These statements are used to perform the insert, upsert, update, or delete operations on records within a database.
Take a free demo session of our Salesforce training. Reserve your spot now!
Salesforce Training For Administrators & Developers
- Personalized Free Consultation
- Access to Our Learning Management System
- Access to Our Course Curriculum
- Be a Part of Our Free Demo Class
Conclusion
With this blog, you must have understood the concept of Apex class, how to create an Apex class and when developers should use Apex classes. Also, we have discussed the Apex String class in the Salesforce and its different methods that are used frequently by developers. To know more about the Apex classes in Salesforce and different cloud concepts, you should join the Salesforce certification program at JanBask Training and get hands-on expertize on Salesforce tools too.
Read: App Builder Salesforce Certification - Complete Exam Details & Passing Tips
FaceBook
Twitter
LinkedIn
Pinterest
Email
Salesforce Course
Upcoming Batches
Trending Courses
Cyber Security
- Introduction to cybersecurity
- Cryptography and Secure Communication
- Cloud Computing Architectural Framework
- Security Architectures and Models
Upcoming Class
12 days 04 Jan 2025
QA
- Introduction and Software Testing
- Software Test Life Cycle
- Automation Testing and API Testing
- Selenium framework development using Testing
Upcoming Class
5 days 28 Dec 2024
Salesforce
- Salesforce Configuration Introduction
- Security & Automation Process
- Sales & Service Cloud
- Apex Programming, SOQL & SOSL
Upcoming Class
7 days 30 Dec 2024
Business Analyst
- BA & Stakeholders Overview
- BPMN, Requirement Elicitation
- BA Tools & Design Documents
- Enterprise Analysis, Agile & Scrum
Upcoming Class
4 days 27 Dec 2024
MS SQL Server
- Introduction & Database Query
- Programming, Indexes & System Functions
- SSIS Package Development Procedures
- SSRS Report Design
Upcoming Class
4 days 27 Dec 2024
Data Science
- Data Science Introduction
- Hadoop and Spark Overview
- Python & Intro to R Programming
- Machine Learning
Upcoming Class
11 days 03 Jan 2025
DevOps
- Intro to DevOps
- GIT and Maven
- Jenkins & Ansible
- Docker and Cloud Computing
Upcoming Class
3 days 26 Dec 2024
Hadoop
- Architecture, HDFS & MapReduce
- Unix Shell & Apache Pig Installation
- HIVE Installation & User-Defined Functions
- SQOOP & Hbase Installation
Upcoming Class
5 days 28 Dec 2024
Python
- Features of Python
- Python Editors and IDEs
- Data types and Variables
- Python File Operation
Upcoming Class
4 days 27 Dec 2024
Artificial Intelligence
- Components of AI
- Categories of Machine Learning
- Recurrent Neural Networks
- Recurrent Neural Networks
Upcoming Class
12 days 04 Jan 2025
Machine Learning
- Introduction to Machine Learning & Python
- Machine Learning: Supervised Learning
- Machine Learning: Unsupervised Learning
Upcoming Class
4 days 27 Dec 2024
Tableau
- Introduction to Tableau Desktop
- Data Transformation Methods
- Configuring tableau server
- Integration with R & Hadoop
Upcoming Class
5 days 28 Dec 2024
Salesforce Course
Upcoming Batches
Receive Latest Materials and Offers on Salesforce Course