Today's Offer - Salesforce Certification Training - Enroll at Flat 10% Off.

- Salesforce Blogs -

What is Apex String Class in the Salesforce?

Apex is a strongly typed, object-oriented programming language used to execute code in the Salesforce instance. You may define classes in Apex with 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.

Read: What is Standard Controller in Salesforce?

How to create an Apex class in the 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. What is Apex String Class in the Salesforce? One of the most basic examples for creating an Apex class is given below. What is Apex String Class in the Salesforce? 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.

Create an Apex class from Force.com IDE

These are the simple steps for creating an Apex class from Force.com IDE.

  1. In the first step, open the Force.com Eclipse IDE.
  2. Create a new project now by clicking on File -> New -> Apex class.
  3. Give a proper name to the class then select the “OK” option.
  4. Once these three steps are followed successfully, a new class is created.

Access modifiers in Salesforce for an Apex Class

Access modifiers in Salesforce for an Apex Class

  • 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 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

Sharing Modes in Salesforce For An Apex Class

Read: What is SFDC? What does SFDC stand for?
  • 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 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.

Why choose Apex as a Language?

Let us discuss a few features of Apex as a programming language –

  • Easy Syntax: Apex is an easy to use programming language with 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 of methods for the String class.

  • abbreviate (maxWidth) : It returns 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 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 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 few popular methods for string Apex class in the 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. free salesforce demo

Read: Top 20 Salesforce Alternatives: Is There Good CRM Competitor to Salesforce?

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 on each of them one by one. A Deep look at the Apex Syntax for String class

  • 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 condition, it is decided that 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.

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: How Can I Start my Career in the Salesforce Industry?

    Janbask Training

    JanBask Training is a leading Global Online Training Provider through Live Sessions. The Live classes provide a blended approach of hands on experience along with theoretical knowledge which is driven by certified professionals.


Trending Courses

AWS

  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

1 day 14 Nov 2019

DevOps

  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing

Upcoming Class

2 days 15 Nov 2019

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning

Upcoming Class

2 days 15 Nov 2019

Hadoop

  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation

Upcoming Class

3 days 16 Nov 2019

Salesforce

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

1 day 14 Nov 2019

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing

Upcoming Class

20 days 03 Dec 2019

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum

Upcoming Class

2 days 15 Nov 2019

SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design

Upcoming Class

6 days 19 Nov 2019

Comments

Search Posts

Reset

Receive Latest Materials and Offers on Salesforce Course

Interviews