0% found this document useful (0 votes)
7 views15 pages

Apex Code Invocation Methods in Salesforce

The document outlines various methods for invoking Apex code in Salesforce, including triggers, asynchronous execution, and web services. It also discusses the use of SOQL and SOSL for querying data, the structure of triggers, and the functionalities of Flow Builder for automating business processes. Additionally, it covers data import tools and the differences between Data Import Wizard and Data Loader for handling records.

Uploaded by

s.dattacemk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

Apex Code Invocation Methods in Salesforce

The document outlines various methods for invoking Apex code in Salesforce, including triggers, asynchronous execution, and web services. It also discusses the use of SOQL and SOSL for querying data, the structure of triggers, and the functionalities of Flow Builder for automating business processes. Additionally, it covers data import tools and the differences between Data Import Wizard and Data Loader for handling records.

Uploaded by

s.dattacemk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

User Name : ussaikatdatta@playful-fox-nr0i26.

com

Architecture:

Methods of Invoking Apex


You can run Apex code with triggers, or asynchronously, or as SOAP or REST web
services.

 Anonymous Blocks
An anonymous block is Apex code that doesn’t get stored in the
metadata, but that can be compiled and executed.
 Triggers
Apex can be invoked by using triggers. Apex triggers enable you to
perform custom actions before or after changes to Salesforce records,
such as insertions, updates, or deletions.
 Asynchronous Apex
Apex offers multiple ways for running your Apex code asynchronously.
Choose the asynchronous Apex feature that best suits your needs.
 Exposing Apex Methods as SOAP Web Services
You can expose your Apex methods as SOAP web services so that
external applications can access your code and your application.
 Exposing Apex Classes as REST Web Services
You can expose your Apex classes and methods so that external
applications can access your code and your application through the
REST architecture.
 Apex Email Service
You can use email services to process the contents, headers, and
attachments of inbound email. For example, you can create an email
service that automatically creates contact records based on contact
information in messages.
 Using the InboundEmail Object
For every email the Apex email service domain receives, Salesforce
creates a separate InboundEmail object that contains the contents and
attachments of that email. You can use Apex classes that implement
the [Link] interface to handle an inbound
email message. Using the handleInboundEmail method in that class,
you can access an InboundEmail object to retrieve the contents,
headers, and attachments of inbound email messages, as well as
perform many functions.
 Visualforce Classes
In addition to giving developers the ability to add business logic to
Salesforce system events such as button clicks and related record
updates, Apex can also be used to provide custom logic for Visualforce
pages through custom Visualforce controllers and controller
extensions.
 JavaScript Remoting
Use JavaScript remoting in Visualforce to call methods in Apex
controllers from JavaScript. Create pages with complex, dynamic
behavior that isn’t possible with the standard Visualforce AJAX
components.
 Apex in AJAX
The AJAX toolkit includes built-in support for invoking Apex through
anonymous blocks or public webservice methods.

Apex switch statement expressions can be one of the following types.

 Integer
 Long
 sObject
 String
 Enum

Trigger Essentials
Before learning more about the execution context, let’s take a step back to
introduce you to the world of database triggers. Similar to triggers in SQL
Server, Apex database triggers execute programming logic before or after
events to records in Salesforce. When defining the trigger, you can specify
more than one of the following events:

 before insert
 before update
 before delete
 after insert
 after update
 after delete
 after undelete

Trigger context variables

Variable Usage
isExecuting Returns true if the current context for the Apex code is a trigger, not a Visualf
service, or an executeanonymous() API call.
isInsert Returns true if this trigger was fired due to an insert operation, from the Sales
Apex, or the API.
isUpdate Returns true if this trigger was fired due to an update operation, from the Sale
Apex, or the API.
isDelete Returns true if this trigger was fired due to a delete operation, from the Sales
Apex, or the API.
isBefore Returns true if this trigger was fired before any record was saved.
isAfter Returns true if this trigger was fired after all records were saved.
isUndelete Returns true if this trigger was fired after a record is recovered from the Recy
can occur after an undelete operation from the Salesforce user interface, Apex
new Returns a list of the new versions of the sObject records.

This sObject list is only available in insert , update , and undelete triggers, and t
modified in before triggers.

newMap A map of IDs to the new versions of the sObject records.

This map is only available in before update , after insert , after update , and after

old Returns a list of the old versions of the sObject records.

This sObject list is only available in update and delete triggers.


Variable Usage
oldMap A map of IDs to the old versions of the sObject records.

This map is only available in update and delete triggers.

operationTy Returns an enum of type [Link] corresponding to the curre


pe
Possible values of the [Link] enum
are: BEFORE_INSERT , BEFORE_UPDATE , BEFORE_DELETE , AFTER_INSERT , AFTER_UPDATE , AFTER_D
and AFTER_UNDELETE . If you vary your programming logic based on different trigg
using the switch statement with different permutations of unique trigger exec

size The total number of records in a trigger invocation, both old and new.

Trigger exceptions

Triggers and Callouts


Use @future(callout=true) methods

Asynchronous Apex

Future Methods
 You can’t track execution because no Apex job ID is returned.
 Parameters must be primitive data types, arrays of primitive data
types, or collections of primitive data types. Future methods can’t take
objects as arguments.
 You can’t chain future methods and have one call another.

Batch or Scheduled Apex


 troubleshooting can be troublesome.
 Jobs are queued and subject to server availability, which can
sometimes take longer than anticipated.
 Have we talked about limits yet?

Queueable Apex
 Non-primitive types - Classes can accept parameter variables
of non-primitive data types, such as sObjects or custom Apex types.
 Monitoring - When you submit your job, a jobId is returned that you
can use to identify the job and monitor its progress.
 Chaining jobs - You can chain one job to another job by starting a
second job from a running job. Chaining jobs is useful for sequential
processing.

List of container

 Salesforce Classic
 Visualforce
 Salesforce App
 Lightning Experience
 Lightning App Builder (LAB)
 Lightning console apps
 Communities
 Lightning Components for Visualforce (LC4VF)
 Lightning Out
 Lightning for Outlook and Lightning for Gmail
 Stand-alone [Link]

Your Aura component code can access only the services of the container
it’s running inside of, even if that container is inside of another container.
Lightning Data Service (LDS) is only available with Aura components. It’s the closest
thing Aura components has to a standard controller. It does require writing some
code. Not as much as a plain controller, but some. But LDS is far more powerful, and
includes built-in caching, update notification, and other crazy cool features. Once
you make the switch, you won’t want to go back. See the Resources section to learn
more about LDS.

SOQL is used only to perform queries with the SELECT statement. SOQL has no
equivalent INSERT, UPDATE, and DELETE statements

The big thing to be aware of when working with aggregates is that for most
functions your result is returned as an AggregateResult type

* matches zero or more characters at the middle or end of the search


term
? matches only one character at the middle or end of the search term
The big thing to know about the nickname search is that it applies only to English-
language searches on the Account, Contact, Lead, and User objects.

Salesforce provides only a synonym search to look for nicknames.

Searches that exceed 20,000 characters can tax the system.

FIND {"grand*"} IN ALL FIELDS RETURNING Account(Name),


Contact(LastName, FirstName, Email)

Indexed Field Description


Id Unique 18-character field that is system generated. This is the primary k
Name Text-based field.
OwnerId Reference to the owner of the object.
CreatedDate Date and time when the record was created.
SystemModStam Read-only field that contains the last date that the record was updated. T
p indexed where the similar LastModifiedDate is not, so consider using this
queries.
RecordType Id of the RecordType. RecordTypes are used to offer different UI results t
Master-Detail Foreign key field used to indicate a master-detail relationship
Fields
Lookup Fields Foreign key field used to indicate a lookup relationship
Unique Fields Custom fields can be marked as unique when they are created, and this
make them indexed.
External ID Like unique fields, these custom fields can be marked as an External Id a
Fields used for integration purposes.

You can create custom object from Excel

There are two main types of object relationships: lookup and master-detail.

Lookup Relationships

Master-Detail Relationships

External Lookup Relationship


You can’t export your schema from Schema Builder (for example, to use the schema in
another org).

Data Loader uses the SOAP API to process records.

 Data Import Wizard—this tool, accessible through the Setup menu,


lets you import data in common standard objects, such as contacts,
leads, accounts, as well as data in custom objects. It can import up to
50,000 records at a time. It provides a simple interface to specify the
configuration parameters, data sources, and the field mappings that
map the field names in your import file with the field names in
Salesforce.
 Data Loader—this is a client application that can import up to five
million records at a time, of any data type, either from files or a
database connection. It can be operated either through the user
interface or the command line. In the latter case, you need to specify
data sources, field mappings, and other parameters via configuration
files. This makes it possible to automate the import process, using API
calls.

Use the Data Import Wizard When:


 You need to load less than 50,000 records.
 The objects you need to import are supported by the wizard.
 You don’t need the import process to be automated.

Use Data Loader When:


 You need to load 50,000 to five million records. If you need to load more than
5 million records, we recommend you work with a Salesforce partner or visit
the AppExchange for a suitable partner product.
 You need to load into an object that is not supported by the Data Import
Wizard.
 You want to schedule regular data loads, such as nightly imports
Use Case Salesforce Flow Functionality
Create a guided tutorial Flow Builder includes several out-of-the-box screen components,
or wizard with screens. buttons, and file-uploads. If you need more than what’s offered, a
components to your screens.
Set up automated tasks Declaratively configure logic and actions for your business proces
and processes. needed, you can build custom Apex code to fill any functional gap
Connect to external Communicate changes between your Salesforce org and your ext
systems. platform events.

Flow Builder lets you respond to and send platform event messag
also retrieve data from third-party systems with External Service
Add automation to your Make sure your behind-the-scenes processes start when the right
pages and apps. whether that’s when records change or when users click a particu

Once you build guided visual experiences, add them to Lightning


Builder pages, the utility bar in your Lightning apps, and more.
Reuse what you build. In Flow Builder, break down process logic or actions into a flow th
Subflow element. You can reuse or reference this flow in other bu
In Process Builder, call an autolaunched flow from a process to au
business processes.

Type of Business
Description Av
Process
Guided visual Business processes that need input from users, whether they’re Flo
experience employees or customers.
Behind-the-scenes Business processes that get all the necessary data from your Flo
automation Salesforce org or a connected system. In other words, user input (Re
isn’t needed.
Pro
exi

Ape
Approval automation Business processes that determine how a record, like a time-off App
request, gets approved by the right stakeholders.
Flow Builder
Use Flow Builder to:

 Automate a guided visual experience.


 Start a behind-the-scenes business process:
o When a user clicks something, like a button
o When a record is created
o When a record is updated
o When a record is deleted
o When a platform event occurs
o At a specified time and frequency

For example, when an opportunity is won, your company wants a renewal


opportunity to be created automatically.

Flow Builder : Like breadcrumb, also can work as utility

Process builder : like partner update BADI

Approval : Like timesheet approval

Workflow:

FLOW BUILDER WORKFLOW APPROVALS


PROCESS BUILDER
Complexity Multiple if/then statements Complex A single if/then statement

Visual designer

 User clicks button or link


 User accesses Lightning
page, Experience Cloud site
 Record is changed
page, Visualforce page, or
 Invoked by another process
custom tab
Starts when  Platform event message is Record is changed
 User accesses item in a
received
utility bar
 Process starts
 Apex is called

Supports time-based actions


Supports user interaction
Supported Actions
Call Apex code
FLOW BUILDER WORKFLOW APPROVALS
PROCESS BUILDER
Create records Tasks only
Invoke processes
Delete records

Launch a flow
(Pilot)1
Post to Chatter

Send email
(Email alerts only) (Email alerts only)
Send custom notification
Send outbound messages
without code
Submit for approval

Update fields Any related record Any record The record or its parent

You can create generic sObject like account, contact e.t.c.

Unlike DML statements, Database methods have an optional allOrNone parameter


that allows you to specify whether the operation should partially succeed. When this
parameter is set to false , if errors occur on a partial set of records, the successful
records will be committed and errors will be returned for the failed records. Also, no
exceptions are thrown with the partial success option

The delete operation supports cascading deletions. If you delete a parent object,
you delete its children automatically, as long as each child record can be deleted.

You don’t need to specify the Id field in the query as it is always returned in Apex
queries, whether it is specified in the query or not. For example: SELECT Id,Phone
FROM Account and SELECT Phone FROM Account are equivalent statements. The only
time you may want to specify the Id field if it is the only field you’re retrieving
because you have to list at least one field: SELECT Id FROM Account . You may want to
specify the Id field also when running a query in the Query Editor as the ID field
won’t be displayed unless specified.
you can retrieve all accounts whose names start with SFDC by using this
condition: WHERE Name LIKE 'SFDC%' . The % wildcard character matches any or no
character. The _ character in contrast can be used to match just one character.

100 SOQL queries for synchronous Apex or 200 for asynchronous Apex.

Triggers execute on batches of 200 records at a time. So if 400 records cause a


trigger to fire, the trigger fires twice, once for each 200 records. For this reason, you
don’t get the benefit of SOQL for loop record batching in triggers, because triggers
batch up records as well. The SOQL for loop is called twice in this example, but a
standalone SOQL query would also be called twice. However, the SOQL for loop still
looks more elegant than iterating over a collection variable!

The Apex runtime allows up to 150 DML calls in one transaction.

Search:
SOQL
SOSL
suggested records API
Salesforce Federated Search

 Query (REST) and query() (SOAP)—Executes a SOQL query against


the specified object and returns data that matches the specified
criteria.

 Search (REST) and search() (SOAP)—Executes a SOSL text string


search against your org’s data.

I want to... SOSL SO


Limit the data that is searched IN SearchGroup WH

Specify the data to be returned in the response Returning FieldSpec SE

Sort results ORDER BYLIMITOFFSET OR

Filter by data category WITH DATA CATEGORY WI


 Search Suggested Records—Returns a list of suggested records whose
names match the user’s search string. The suggestions resource
provides a shortcut for users to navigate directly to likely relevant
records, before performing a full search.

 Search Suggested Article Title Matches—Returns a list of Salesforce


Knowledge articles whose titles match the user’s search query string.
Provides a shortcut to navigate directly to likely relevant articles
before the user performs a search.

 SObject Suggested Articles for Case—Returns a list of suggested


Salesforce Knowledge articles for a case.

Creating synonym groups is easy.

1. From Setup, enter Synonyms in the Quick Find box, then


select Synonyms.
2. Under Custom Synonym Groups, click New to start a synonym group
or Edit next to an existing group.
3. Add two to six synonyms per group. A synonym can be any word or
phrase. No special characters.

Your organization can create a maximum of 2,000 promoted terms.

Understand Event-Driven Software Architecture


 Change events alike
The Salesforce platform provides allocations for how many events you can define
in your org, and how many events you can publish in an hour. For events
configured with the Publish After Commit behavior, each method execution is
counted as one DML statement against the Apex DML statement limit. You can
check limit usage using the Apex [Link]() method. For events
configured with the Publish Immediately behavior, each method execution is
counted against a separate event publishing limit of
150 [Link]() calls. You can check limit usage using the
Apex [Link]() method. For more information see the
Resources section.

The batch size in a platform event trigger is 2,000 event messages, which is
larger than the Salesforce object trigger batch size of 200. The batch size
corresponds to the size of the [Link] list. You can modify the batch size of a
platform event trigger. For more information, see Configure the User and Batch
Size for Your Platform Event Trigger in the Platform Events Developer Guide

To use the empApi methods in your Lightning web component, import the
methods from the lightning/empApi module as follows.
import { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled }

from 'lightning/empApi';

You might also like