0% found this document useful (0 votes)
5 views

Salesforce+Developer+Questions

The document is a comprehensive guide containing various Salesforce developer interview questions and topics, including basic OOP concepts, collections, SOQL, Apex triggers, test classes, exception handling, and asynchronous Apex. It also includes practical assignments for developing Apex triggers and understanding their functionalities. Additionally, it covers advanced topics like Queueable Apex, Batch Apex, Scheduled Apex, and secure queries in Salesforce.

Uploaded by

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

Salesforce+Developer+Questions

The document is a comprehensive guide containing various Salesforce developer interview questions and topics, including basic OOP concepts, collections, SOQL, Apex triggers, test classes, exception handling, and asynchronous Apex. It also includes practical assignments for developing Apex triggers and understanding their functionalities. Additionally, it covers advanced topics like Queueable Apex, Batch Apex, Scheduled Apex, and secure queries in Salesforce.

Uploaded by

aaditya31001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Salesforce Developer Questions

Index

● asic OOPs Concept


● ollection in Salesforce
● oops in Salesforce
● etters/Setters in Salesforce
● OQL
● nheritance
● pex Trigger
● est Class
● xception handling
● sync Apex
● ssignments
○ pex Triggers
○ pex Test Classes
○ xception handling

BASIC OOPS CONCEPT

1. What is the OOPs concept in Salesforce?


2. What is Object in Salesforce Apex?
3. Explain Inheritance.
4. Explain Polymorphism
5. What is encapsulation in Salesforce? Provide an example
6. What is abstraction?
7. What is the difference between method overloading and overriding?
8. When shall we use the virtual keyword?
9. What are the different types of Access Modifiers?
10. Difference between public, global & private access modifiers
11. What are DRY Principles?
12. Explain SOLID Principle
13. Explain the below line of Code
a. Car Mercedes = new Car();
14. Can we create the private constructor in the parent class?
15. What is the constructor and what are the different types of constructors?
16. What is the difference between constructors and methods in Salesforce?
17. What is the significance of protected keywords in Salesforce?
18. What are the primitive Data Types in Salesforce?
19. What is instance Bloack in Salesforce?
20. What are static blocks in Salesforce?
21. Difference between instance block & static block in Salesforce.

COLLECTION IN SALESFORCE

1. What are the different types of collections in Salesforce?


2. When to use List in Salesforce?
3. What is the difference between List & SET in Salesforce?
4. What is Map in Salesforce, and how do you use it?
5. What are the different methods of List, Set, and map?
6. Can List contain the duplicate elements?
7. What are the different ways to create a List, Set, or Map in Salesforce?

LOOPS IN SALESFORCE

1. What are the different types of Loop in Salesforce?


2. What is the difference between a for loop and a While Loop?
3. When should you use a while loop and make a while loop?
4. Difference between while and do while loop

GETTERS/SETTERS IN SALESFORCE

1. What are getters and setters in Salesforce?


2. When should we use the getters and setters in Salesforce?
3. What are the different ways to create the setters in Salesforce?
a. Example - public String name { get; set; }

SOQL

1. What is SOQL in Salesforce?


2. What is the Structure of the SOQL Query in Salesforce?
3. List All the Contact Records
4. List All the Contact Records where the Email is not blank
5. List All the Contact Records Where the Email is Blank
6. List All the Contact Records Where the Title contains the developer
7. List All the Contact Records Where the Title Starts with the Manager
8. List All the opportunities where the Amount is more than 29000
9. List All the Opportunity count state-wise
10. List All the Opportunity Amount Stage Wise
11. List All the Open & Closed Opportunities
12. List All the Contact Records with Account Name, Rating, and Industry
13. Query on the Account object and also query all the related Contact, Case, Order, and Opportunities
14. Create 3 Custom Objects
a. An invoice that is related to the Account
b. An Invoice Line Item object that is related to the Invoice
c. A payment that is related to the Account
d. Now Query on Invoice Records to Query the Related Account Information and Also all the related Invoice Line Items
e. Note: - Please create the required records for these objects
15. Query All the Contact Record that does not have any Account Related To
16. Query All the Contact Records that are related to the Account Record
17. Query All the Account Record that does not have any contact
18. Query All the Account Record that does have min one Contact Record
19. Query the Invoice Record, its Related Account, and the related Parent Account Record
20. Query the Invoice Line Item Record, its Related Invoice, Invoice Related Account, and Account Related Parent Account
Record
21. Write a SOQL Query to Show the maximum amount from the opportunity
22. Write a SOQL Query to identify the duplicate records by Name & Email
23. Write a SOQL Query to display the count of Accounts for every Industry
24. Create an Apex Class, and the name is SOQLDemo
a. Create a method and name it searchAccount that accepts a single parameter as the account name or portion of the
account. Make an SOQL Query to get all the accounts whose names match the matching name as parameters and put
a debug log.
b. Create a method polymorphicQueryDemo and make a SOQL query to get the Event Information related to Account,
Contact, Opportunity, and Case. See more for the query
i. When What is Account SELECT Id, Name, Phone, Industry
ii. When What is Opportunity SELECT Id, Name, Amount
iii. When What is Case SELECT Id, CaseNumber, Subject, Status
iv. When What is Contact SELECT Id, Name, Email, Phone
c. create a method soqlForLoopDemo and do the query on Account, Contact, and Opportunity using SOQL For Loop
d. Create a method that accepts the List of Industries and the keyword. Inside the method, the SOQL Query filters the
account based on Industry and the name based on the parameter.
i. Note: - Name should have “Like” the filter on the Account Name field
e. Create a method and have the below query inside that method
i. Query All the Contact Record that does not have any Account Related To
ii. Query All the Contact Records that are related to the Account Record
iii. Query All the Account Record that does not have any contact
iv. Query All the Account Record that does have min one Contact Record
v. Query the Invoice Record, its Related Account, and the related Parent Account Record
vi. Query the Invoice Line Item Record, its Related Invoice, Invoice Related Account, and Account Related Parent
Account Record
vii. Write a SOQL Query to Show the maximum amount from the opportunity
viii. Write a SOQL Query to identify the duplicate records by Name & Email
ix. Write a SOQL Query to display the count of Accounts for every Industry

INHERITANCE

1. What is Inheritance in Salesforce, and what is the importance of inheritance in Salesforce?


2. What are the required steps to implement the Inheritance in Salesforce?
3. What is the use of virtual keywords in Salesforce?
4. What is the use of the super keyword in Salesforce?

APEX TRIGGER

● What is Apex Trigger in Salesforce?


● What is the Size of the Apex Trigger?
● When should you use Before vs After Trigger in Salesforce?
● What are the different context variables in Salesforce Apex Trigger?
● What context variables are available in case of deleting context?
● What does it mean by Record is read-only? How to get rid of the Record is read-only Error?
● Have you encountered the error called Maximum Depth reached? When does this error come into the picture, and how do
we resolve this?
● Write a trigger to count the cases under the account record and store it in the custom field at Account. This trigger should
take care of all the scenarios.
● Write a trigger to prevent the duplicate Lead record using the Email and company of the Lead.
● How do you make a callout from the apex trigger?
● How do we prevent the recursive trigger in Salesforce?
● How many triggers should we create in Salesforce Object?
● How do you write a logicless apex trigger?
● What is the difference between the Trigger.new And Trigger.newMap?
● can we call a batch apex from the Trigger?
● How many times do triggers execute on a merge event?
● When do Maximum Trigger Depth Exceed errors come in Salesforce, and how to resolve this error?
● What is the maximum time triggers execute, and then we get Maximum Trigger Depth Exceeded Error?
● What trigger context variables are available in case of a deleted event?
● What is a recursive apex trigger?
● How to prevent recursive Apex Trigger?

TEST CLASS

● What is Apex Test Class?


● What are the important annotations in Test Class
● What are the best practices of the Test class?
● What is the use of Test.startTest() & Test.stopTest()
● When to use runAs in test class
● What is the Assert statement in test class?
● What is the use of @teseSetup in Test Class?
● What minimum coverage is required for an Apex Class and trigger to deploy to production org?
● What are the benefits of the Test Class?
● What is a test drive framework?

EXCEPTION HANDLING

● What is an exception in Salesforce?


● How do we handle the exceptions in Salesforce?
● Name some of the exceptions.
● How do you create a custom exception in Salesforce?
● Can we have a nested try-catch?
● What is the use of the final block?
● What is the parent Exception Class?
● Can we have multiple catch blocks for a single try block?
● How do you throw the custom exception?
● What is nullPointerException & how do you resolve the error?
● What are the common methods in exception classes?
● Resources
○ https://www.apexhours.com/different-types-of-exceptions-in-salesforce/
○ https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_definition.htm
○ https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_builtin.htm
○ https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_limits.htm
○ https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_exception_methods.htm

ASYNC APEX

● What is Asynchronous Apex?


● What are the different types of Apex?
● What is the future method?
● What is the structure of the future method?
● Why can we not pass the non-primitive variables in future methods?
● Why future methods can not return any value?
● Can we call a future method from the future method?
● Why can we not call a future method from the future method?
● What is a MIXED_DML operation error?
● What are the Setup object & NoN-Setup objects?
● How to resolve the MIXED_DML operation error?
● What are the limitations of future methods?
● Can we chain future methods?
● Can we track future methods?
● What is a flex Queue?
● What is the maximum no of records that can be there in Flex Queue?
● Can we make a callout using the future method? If yes, how?

QUEUEABLE APEX

● What is Queueable Apex?


● How do you create an Apex Class as Queueable Apex?
● Which interface do we need to use for creating a class as Queueable Apex?
● How are future methods and Queueable Apex different?
● Can we chain the Queueable Apex?
● Can we track the Queueable Apex?
● How to Execute the Queueable Apex?
● When to use Queueable Apex?
● What is a flex Queue?
● What is the maximum no of records that can be there in Flex Queue?
● How many times can we use System.enqueueJob in a single transaction?

BATCH APEX

● What is Batch Apex?


● Which interface do we use to create a Batch Apex?
● What are the methods available in batch apex?
● Can we create a batch apex without having a finish method?
● What is the default batch size?
● Can we use 0 as batch size?
● Can we chain the batch job?
● Can we call another batch from the start and execute the method?
● How many max records can we process with batch apex?
● Can we make the callouts from the batch Apex?
● What is the minimum batch size?
● What is the maximum batch size?
● How do we provide the input for Batch Apex?
● What is the stateful interface?
● Can we make the callout from the batch apex? If yes, how?
● What is the maximum number of async methods that can be executed?
● How many max records can be returned in Batch Class by Database.getQueryLocator method
● How many max records can be returned in a normal Apex Class by Database.getQueryLocator method
● What is a Stateful Interface?
● What is the use of a Database.Stateful Interface

Question - Can we call batch from batch Apex?


Answer - Yes. From Finish Method

Question - Can we call queueable apex from the batch apex?

Question - Can we call batch from the Queueable?

Question - Can we call the future method from the batch apex?

Question - Can we call the batch apex from the future method?

Question - What is the maximum number of async methods that can be executed in 24 hours? - 2,50,000 OR total no of Salesforce
Licence * 200, whichever is greater in 24 hours

Question - Can we make the callout from the batch apex?


Answer - We need to implement an interface Database for this.AllowsCallouts
Error - System.LimitException: Too many callouts: 1

Question - Can we make the callout from the future method?


Answer - @future(callouts = true)
Error - Callouts are not allowed from the future method

Question - Can we make the callout from the queueable apex?


Answer - We need to implement an interface Database for this.AllowsCallouts -
Error - System.LimitException: Too many callouts: 1

SCHEDULED APEX

● What is Scheduled Apex?


● What interface do we use to enable any class to be a Scheduled Apex Class?
● Can we make a callout from the Scheduled Class?
● What are the different ways to Schedule the Apex Class?
● Can we schedule Batch, Queueanle & Normal Apex using Schedule Apex Class?

SECURE QUERIES IN SALESFORCE

● What is the use of WITH SECURITY_ENFORCED in Salesforce?


● What is the difference between WITH USER_MODE and WITH SECURITY_ENFORCED?
● Which one performs better among WITH USER_MODE and WITH SECURITY_ENFORCED?
● What is the exception that occurred by WITH USER_MODE and WITH SECURITY_ENFORCED keywords?
● What is the use of AccessLevel Class?
● what is the use of insert as user & insert as a system?
Assignments
APEX TRIGGERS

→ Apex Trigger 1
Develop an Apex Trigger so that every time any account is inserted, the value of the Industry field is set to Education. Also, check
if the Description is blank, then set the value for the description field to “Account Description is blank.”

1. Industry = Education
2. Description = Account Description is blank if Description is null

→ Apex Trigger 2

Develop an Apex Trigger so that every time when any account is created or updated then Set the Value of the Billing Address is to
Shipping Address.

Shipping Address

● ShippingStreet
● ShippingCity
● ShippingState
● ShippingPostalCode
● ShippingCountry

Billing Address

● BillingStreet
● BillingCity
● BillingState
● BillingPostalCode
● BillingCountry

→ Apex Trigger 3

Develop an Apex Trigger so that every time any account is created or updated, then Set the Value of the Billing Address is to
Shipping Address.

You only need to update the Shipping Address information with the Billing Address if the Shipping Address (Compound Field) is a
blank value.

→ Apex Trigger 4

When the Account is Created, create a Task Record under that Account and assign the Task to the Account Owner. Use the
information below
● Subject - Created from Apex Trigger
● Comments - Created from Apex Trigger
● Due Date - Todays Date + 7
● Status - Not Started
● Priority - High
● Related To (What) - Account Id
● Assigned To (OwnerId) - Account Owner Id

→ Apex Trigger 5

Prerequisite

● Create a Custom field on Opportunity “Discount” and the data type of this field should be percent.
● Create a Custom field on Opportunity “Discounted Price” with Currency Data Type

Requirement

Develop an Apex Trigger on Opportunity so that when any opportunity is created and if the Discount & Amount field is not blank
then calculate the discount and store it in the Discount Price field.

To Calculate the discount use the below formula and store it in a variable
Decimal discount = ( Discount * Amount ) / 100

To Calculate the Discounted Price use the below calculation and store it in a variable
Decimal discountedAmount = Amount - discount;

→ Apex Trigger Scenario 6

Develop an Apex Trigger that will update the Contact Phone Value to Contact Fax every time a Contact is updated.

→ Apex Trigger Scenario 7

Develop an Apex Trigger so that when an opportunity is created and the account is blank, create a task under Opportunity. Hint: -
AccountId == null

○ Subject - Opportunity is created without Account


○ Description - Opportunity is created without Account, Please assign the Correct account to Opportunity.
○ Due Date - Todays Date + 7
○ Status - Not Started
○ Priority - High
○ Related To (What) - Opportunity Id
○ Assigned To (OwnerId) - Opportunity Owner Id

→ Apex Trigger Scenario 8

Create an Apex Trigger on Case Object and print all the Trigger Context Variable
→ Apex Trigger Scenario 9

Convert the Apex Trigger that you created in the previous assignment to Handler & Dispatcher Apex Classes

→ Apex Trigger Scenario 10

Develop an Apex Trigger on the Contact record so that when any Contact is created under any Account Record then, please

● Populate the Contact Mailing Address with Account Shipping Address


● Populate the Contact Other Address with the Account Billing Address

Account Address Fields

Shipping Address

● ShippingStreet
● ShippingCity
● ShippingState
● ShippingPostalCode
● ShippingCountry

Billing Address

● BillingStreet
● BillingCity
● BillingState
● BillingPostalCode
● BillingCountry

Contact Address Fields

Mailing Address

● MailingStreet
● MailingCity
● MailingState
● MailingPostalCode
● MailingCountry

Other Address

● OtherStreet
● OtherCity
● OtherState
● OtherPostalCode
● OtherCountry
→ Apex Trigger Scenario 11

Prerequisite

● Create the following custom text field on the Opportunity


○ Account Name
○ Account Industry
○ Account Phone
○ Account Rating

Requirement

Develop an Apex Trigger on the Opportunity record so that when any Contact is created under any Account Record then update the
following on the same Opportunity Record

● The value of the related Account Name should be populated on the Account Name field of the Opportunity object
● The value of the related Account Industry should be populated on the Account Industry field of the Opportunity object
● The value of the related Account Phone should be populated on the Account Phone field of the Opportunity object
● The value of the related Account Rating should be populated on the Account Rating field of the Opportunity object

→ Apex Trigger Scenario 12

Prerequisite

● Create a custom field on Contact & Opportunity with the label “Active” and this picklist field would be the type of
picklist. Below are the values for the picklist fields
○ Yes
○ No

Requirement

Develop an Apex Trigger so that when the account status field is changed at Account Level it should update the same value in all
the related Contacts & Opportunities custom fields “Active” that you have created as part of the prerequisite.

For Example - There is an Account PantherSchools.com that has 5 Contacts & 10 Opportunities. Now, if the Active field on
pantherschools.com is changed it should change the same value on the custom field of Contact & Opportunities.

→ Apex Trigger Scenario 13

→ Create the Apex trigger on the Opportunity & Contact so that when either any Contact or Opportunity is getting created check the
related account Active field. If the Value for the related Account Active field is No then throw the error “You can not create an
Opportunity OR Contact under the account that is Not Active” and do not let the user save the record.
→ Apex Trigger Scenario 14

→ Create the Apex Trigger on the Opportunity & Contact so that when either of these objects is getting inserted then update the
Active field from the related Account.

● For Example, if the Contact is created under the account pantherschools.com and the value of the Active field is “Yes” then
the contact Active Field should be populated with “Yes”.

→ Apex Trigger Scenario 15

Whenever an Opportunity is getting created inside Salesforce and the Amount field is blank then update the Amount field with 1000.

Note: - You need to use After Event here in the scenario.

→ Apex Trigger Scenario 15.1

Create an Apex Trigger so that when any Account Owner is changed then change the Owner of the related Contacts and
Opportunities with the same value as the Account Owner.

→ Apex Trigger (Prevent Duplicate Records) 16

● The business got to know that there are multiple accounts with the same name and rating. Now, as a developer, you need to
make sure that no new duplicates are being created with the same name and rating.

→ Apex Trigger 17

Develop a Solution on Opportunity so that if the StageName of the Opportunity is already set to “Closed Won” and now if any user
is trying to Change the Amount or Account of the Opportunity the user should get the Error.

→ Apex Trigger 18

Prerequisite

● Create a Custom Object and Name it “Location”. Relate this location object with Account using Lookup relationship.
● Create a field on Account “Number of Locations” of type Number

Develop a solution that will create the location records when the Account is created and the “Number of Locations” field has some
value in it. The no of locations related to the account should be the same as the value in the “Number of Locations” field.

For Example - if the value of the “Number of Locations” field is 4 then there should be 4 location records created under that
account.

→ Apex Trigger 19

Develop an Apex Trigger to prevent the duplicate Contact record using Name & Email. So if there are duplicate records with the
same Name & Email new Record should not get created.
→ Apex Trigger 20

Develop an Apex Trigger to prevent Duplicate Leads if there is already an existing Lead record with the same Email & Company.

→ Apex Trigger 21

Prerequisite
Create a Custom Field on the Account Object “Number of Contacts” with a Number Data Type that does not have any decimal point
in it.

Requirement (Roll-up Summary Trigger)


Develop a Solution that will count the related contact related to the Account and store the information in the “Number of Contacts”
field.

Note:- The contact can be created/deleted/undeleted and updated from the Account Record. So please keep that in mind

→ Apex Trigger 22

Prerequisite
Create the Custom Fields on the Account Object “Sum of Closed Opportunity Amount” & “Sum of Open Opportunity Amount”
with a Currency Data Type with 2 Decimal Points.

Requirement (Rollup Summary Trigger)

Develop an Apex trigger to SUM the Opportunity Amount of the Closed Opportunity and Open Opportunities. The closed
opportunities are the ones with the Status equal to “Closed Won” OR “Closed Lost”. And store the result in the “Sum of Closed
Opportunity Amount” & “Sum of Open Opportunity Amount” fields in the related Account for the opportunity.

Note:- The Opportunity can be created/deleted/undeleted and updated from the Account Record. So please keep that in mind

Scenarios for Update DML

The account has been changed


The amount has been changed

→ Apex Trigger 23

Develop an Apex Trigger to send the email to the Account owner when the Account is Created within Salesforce and Account does
not have the phone and industry field populated.

Also, create a task under the account with the following information

● Subject - The phone and Industry of the Account are Blank


● Description - The phone and Industry of the Account are Blank
● Priority - High
● Status - Not Started
● What Id - Related Account Id
● OwnerId - Related Account Owner
The Email should contain the following details

Dear <Account Owner>

A new account <account name> has been created in Salesforce. The account is missing some important information Phone &
Industry.

Please try to collect this information and update the account ASAP.

Thanks & Regards,


<Company Name>

→ Apex Trigger 24

Prerequisite
Create a field on Account with the Label “Sales Rep” and this field should be the Lookup to the User object.

Requirement
Create an Apex Trigger in Salesforce that will update the Salesforce Rep field with the Account Owner who is owing the Account
record.
Also, if the Account owner gets changed then the Sales Rep should get updated with the latest account owner.

→ Apex Trigger 25

Prerequisites

1. Create another user in your Salesforce Org and assign the Standard User profile.
2. Create a custom Object and name it “Account Request” and make sure that the OWD for this object is private. The object
can only be created by Salesforce Admin.
3. Below are the fields that should be created for the object

○ Account - This field is a lookup to the Account object and must be selected while creating the record
○ Access Level - This is a required picklist field with Read & Edit as picklist values
○ User - This is a lookup field to the User object and the user must be selected while creating the record.
○ Reason for Account Access - A Text Area field to store why a user needs access to a certain account.

Requirement

Create an Apex Trigger so that when a new record is created for “Account Request” then the selected account should be shared with
the selected user and provide the Access Level selected in the Account Request object record.

When the Account Request object record is deleted then the account share should also get deleted so that the user does not have
further access to the Account object.

Hint - You need to create/delete the AccountShare object. Please refer to the given link for reference -
https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_accountshare.htm

→ Apex Trigger 26
Prerequisites

1. Create a Permission Set called “System Admin Permissions”


2. In this permission, set enable the following permissions
a. Modify All Data
b. View All Data
c. Manage Users
d. Customize Application
3. Create a Custom object called “Admin Request Access” with the following fields
a. User - Lookup to the User object
b. Reason for System Admin - Text area field
c. Duration (In Hours) - A picklist field value that contains the below options
i. 1
ii. 2
iii. 4
iv. 8
v. 12
vi. 24
vii. 28
d. Status - A Picklist field with the following values
i. Submitter
ii. Approval In Progress
iii. Approved
iv. Rejected
e. Approval Comment - Text area field
f. Rejection Comment - Text area field
g. Manager - Lookup field to the User Object.
h. Approver 1 - Lookup field to the User Object.
i. Approver 2 - Lookup field to the User Object.

Requirement

1. Develop an apex trigger to perform the following


a. If the Manage field is populated on the User Record for whom the request is getting created the record of the “Admin
Request Access” object, then populates the Manager of the user record in the Manager field of the “Admin Request
Access” object.
b. Now check if the Manager field on the “Admin Request Access” object record is not populated then either field
Approver 1 or Approver 2 should be populated before saving the record. [User Can not save the record without
providing value for either Approver 1 or Approver 2]
2. Create an Apex Trigger so that when the Status of the “Admin Request Access” object record is changed to Approved then
assign “System Admin Permissions” permission set to the user who selected in the “User” field for the “Admin Request
Access” record.

Data Model for Permission Set Assignment - [Link for Reference]

→ Apex Trigger 27

Prerequisites

Create a custom object within your Salesforce Org and name it “Public File Links” with the following fields

● Parent Object Name - Text (255) field that will store the object Label under which the file is uploaded/created. For
Example, if the file is uploaded under account record then this field value will be Account.
● Parent Record Id - Text (18) field that will store the record Id of the parent object under which the file is uploaded.
● Link to File - Url field that will have the link to public file
● File Title - The title of the file.

File Structure in Salesforce

Requirement

Create a Trigger so that when a file is created related to the Account object then create the record for the “Public File Links” object
where the value for the “Link to File” field will contain the URL that is public which means if a user has access to the link he/she
should be able to view the content of the File.

Hints: -

● ContentVersion Record is getting created when a file is uploaded to Salesforce


● The ContentDistribution object contains information about the public links and other information. You need to insert the
record to get the public link. Example below

● ContentDistribution newDist = new ContentDistribution();


newDist.ContentVersionId = contentVersionId;
newDist.Name = title;
newDist.PreferencesNotifyOnVisit = false;
newDist.PreferencesAllowViewInBrowser = true;
newDist.PreferencesAllowOriginalDownload=true;

● The contentDownloadUrl field contains the public URL for a file in the ContentDistribution object

→ Apex Trigger 28

Prerequisite
Create a Lookup field on the Lead object to Account object and label it as “Matching Account”

Requirement
Create an Apex Trigger to populate the Matching Account field where there is already an existing Account with the name same as the
value provided in the Company field of Lead Object.
For example, There is an account already existing in Salesforce with the name “United Oil & Gas” Now if a user creates a new Lead
and uses United Oil & Gas value as the company name then United Oil & Gas Account should be auto-populated into the Matching
Account field of Lead Object.

→ Apex Trigger 29

Prerequisite

Add the following values to the Lead Object Status field

● Assigned
● Working
● Qualified
● Unqualified

Requirement
Develop an Apex Trigger when the status of the Lead is selected to Qualified then Convert the Lead in Salesforce.

Note:- The trigger should be bulkified so that it can handle records in bulk.

Resource for the above requirement - https://developer.salesforce.com/docs/atlas.en-


us.apexref.meta/apexref/apex_dml_convertLead.htm

→ Apex Trigger 30

Prerequisite
Create a custom object in Salesforce with the below information

The label of the Object should be “Deleted Opportunity” and will have the below custom fields

● Opportunity Name
● Closed Date
● Stage Name
● Opportunity Id
● Opportunity Amount
● Account

Requirements

● When the opportunity is deleted then the Deleted Opportunity record should get deleted and store the data of that
opportunity in Salesforce under Deleted Opportunity Object.
● If the Opportunity is undeleted from the Recycle Bin then deleted the associated Deleted Opportunity record. Note:- You
can use Record Id to get the associated Deleted Opportunity record.
● https://help.salesforce.com/s/articleView?id=000384697&type=1

→ Apex Trigger 31
Requirement
Develop a solution so that whenever a file is getting inserted under Task, Event or Case Object the same file should be linked to the
related Account Record.

Note: - The file should only get linked to the account record. If the task and event records are associated with Account (which means
the what id is of type Account) then only the file should get copied.

Hints

● Explore Content Document Link


● You can use the getSObjectType method to check the type of What Id
● Explore the clone method of the List class
● https://developer.salesforce.com/docs/atlas.en-
us.apexref.meta/apexref/apex_methods_system_list.htm#apex_System_List_clone
● https://developer.salesforce.com/docs/atlas.en-
us.object_reference.meta/object_reference/sforce_api_objects_contentdocumentlink.htm
● https://developer.salesforce.com/docs/atlas.en-
us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_getSObjectType

Suboptimal Solution

trigger ContentVersionTrigger on ContentVersion (after insert) {


/*
* Develop a solution so that whenever a file is getting inserted under Task,
* Event or Case Object the same file should be linked to the related Account Reco
*/
Set<Id> contentDocumentIdsSet = new Set<Id>();
/* Step1 - Get the content document Id */
for(ContentVersion version: Trigger.New){
contentDocumentIdsSet.add(version.ContentDocumentId);
}
System.debug(' contentDocumentIdsSet '+ contentDocumentIdsSet);
/* Step2 - Get the Content document link */
List<ContentDocumentLink> contentDocumentLinkList = [SELECT Id, LinkedEntityId, Co
FROM
ContentDocumentLink
WHERE
ContentDocumentId IN: contentDocumentIdsSet
];

// Check if the Version/File/Document is created under Task, Event or Case


List<ContentDocumentLink> contentDocumentLinkToInsertList = new List<ContentDocume
Set<Id> taskIdsSet = new Set<Id>();
Set<Id> caseIdsSet = new Set<Id>();
Set<Id> eventIdsSet = new Set<Id>();
for(ContentDocumentLink link: contentDocumentLinkList){ // 2
// Check the sObjectType
String sObjectName = link.LinkedEntityId.getSObjectType().getDescribe().getNam
if(sObjectName == 'Task'){
taskIdsSet.add(link.LinkedEntityId);
}else if(sObjectName == 'Case'){
caseIdsSet.add(link.LinkedEntityId);
}else if(sObjectName == 'Event'){
eventIdsSet.add(link.LinkedEntityId);
}
}
if(taskIdsSet.isEmpty() == false){
// SOQL Query
List<Task> taskList = [Select Id, WhatId FROM Task WHERE Id IN: taskIdsSet AND
for(ContentDocumentLink link: contentDocumentLinkList){
String sObjectName = link.LinkedEntityId.getSObjectType().getDescribe().ge
if(sObjectName == 'Task'){
for(Task t: taskList){
if(link.LinkedEntityId == t.Id){
// Link Clone
ContentDocumentLink clonedLinkRecord = link.clone(false, false
clonedLinkRecord.LinkedEntityId = t.WhatId;
contentDocumentLinkToInsertList.add(clonedLinkRecord);
}
}
}
}
}
if(caseIdsSet.isEmpty() == false){
// SOQL Query
List<Case> caseList = [Select Id, AccountId FROM Case WHERE Id IN: caseIdsSet]
for(ContentDocumentLink link: contentDocumentLinkList){
String sObjectName = link.LinkedEntityId.getSObjectType().getDescribe().ge
if(sObjectName == 'Case'){
for(Case c: caseList){
if(link.LinkedEntityId == c.Id){
// Link Clone
ContentDocumentLink clonedLinkRecord = link.clone(false, false
clonedLinkRecord.LinkedEntityId = c.AccountId;
contentDocumentLinkToInsertList.add(clonedLinkRecord);
}
}
}
}
}
if(eventIdsSet.isEmpty() == false){
// SOQL Query
List<Event> eventList = [Select Id, WhatId FROM Event WHERE Id IN: eventIdsSet
for(ContentDocumentLink link: contentDocumentLinkList){
String sObjectName = link.LinkedEntityId.getSObjectType().getDescribe().ge
if(sObjectName == 'Event'){
for(Event evt: eventList){
if(link.LinkedEntityId == evt.Id){
// Link Clone
ContentDocumentLink clonedLinkRecord = link.clone(false, false
clonedLinkRecord.LinkedEntityId = evt.WhatId;
contentDocumentLinkToInsertList.add(clonedLinkRecord);
}
}
}
}
}
insert contentDocumentLinkToInsertList;
}

→ Apex Trigger 32

Line Items Data Model Salesforce

Prerequisites

● Create a custom field on Opportunity with a checkbox datatype and label it as “Create Line Items”.
● Create a custom field on Opportunity with text data type and label it as “Product Name”.
● Edit the opportunity page layout and add the “Price Books” field in the layout.

Requirement
Develop an apex trigger so that when the “Create Line Items” check box is checked and the PriceBook and “Product Name” field
is populated on the Opportunity record then create the Product with the same name as the value provided in the “Product Name”
field. Add the product to the Standard Price Book and then Add the same product as Opportunity Line Item.

Note: - You need to create multiple objects

● Product2
● PriceBookEntry
● OpportunityLineItem

→ Apex Trigger Scenario 33

Write a trigger on Contact, when a contact is created and the email is populated on the contact record the email should be sent to the
contact owner and contact with the below content

Email Subject - A new contact <Contact Nane> has been assigned!

Dear <Contact Owner>,

A new contact <Contact> has been assigned to you. Here are the details about the contact

Name :- <Contact Name>


Email: <Contact Email>
Phone: <Contact phone>
Account: <Account name>
Link to access Contact - <Link to Contact>

Thanks & Regards,


<Account Owner Name>
‘→ Apex Trigger Scenario 34

Write a trigger on contact to prevent duplicate records based on Contact Email & Contact Phone.

→ Apex Trigger Scenario 35


Write a trigger, only the system admin user can delete the task.

→ Apex Trigger Scenario 36


Write a trigger on Account, when an account is inserted, automatically account billing address should populate into the account
shipping address.

→ Apex Trigger Scenario 37


Write a trigger on Account when Account Ownership is changed to “Private” If that is the case then check all opportunities related
to that account.
Check if the Opportunity Created date is less than 30 days from today and the stage is not equal to close won then set the
Opportunity Stage equal to “Closed Lost”.

→ Apex Trigger Scenario 38


When the Opportunity Product is created under any Opportunity then Create an Asset Record related to the Account that is
associated with the Opportunity.
https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_asset.htm

Use the Below Mapping for the Same

A B

1 Asset Object Field Opportunity Product Field

2 Name Related Product → Name


3 Product Related Product → Id
4 Account Related Opportunity → Account Id
5 Quantity Quantity
6 Price Total Price
7 Status Registered
8 Purchase Date Related Opportunity → Created Date
9 Usage Date Related Opportunity → Closed Date
10 Install Date Created Date
11 Serial Number Related Product --> Product Code

→ Apex Trigger Scenario 39

Prerequisite -

● Create a field on Account and label it as Primary Contact the data type is a lookup to Contact Object

Requirement

Develop a solution so that once a contact is created and the contact has the related Account then populate the Primary Contact field
on Account with the newly created contact.

→ Apex Trigger Scenario 40

Create a field on the Opportunity Line item Object and label it as Serial No with Text DataType and populate increment values once
an Opportunity Line Item is added under the Opportunity.

Let's say we add 3 products then the sequence would be 1,2,3. Now if we delete 2 and again add one more product the value in the
Serial No must be 4 irrespective of the deleted sequence number.

→ Apex Trigger Scenario 41

Prerequisites

● Create Total Quantity and Available Quantity Fields on the Product2 object.
○ The data type of both the fields should be Number with 16,2 specification.

Requirement

Once Opportunity Line Line Item is added to Opportunity with the product, Then Product Total Quantities must be deducted from
the Product Object.

For example, If we have Total Quantity 100 on the Product object and we have added 50 in the line items, then the product must be
updated 100-50 = 50;

→ Apex Trigger Scenario 42

Prerequisites

● Create a custom object “Student” with the following fields


○ FistName
○ LastName
○ Email
○ College
● Set the OWD of the object to private
● Create another user with the Standard User profile

Requirement

Once any student record is created then the record should automatically be shared with the users who belong to the profile
“Standard User”

→ Apex Trigger Scenario 43

Prerequisites
● Create a Validation Rule on the Asset object so that an Asset can not be created without selecting the following fields
○ Account
○ Contact
○ Product

Data Model For PriceBook Entry

Requirement

Write a trigger on Asset Once an asset is inserted related to any account Record then an Opportunity should be created under that
account with the following information

● Name - “Account Name ” + “Opportunity”


● Close Date - Todays Date + 30 Days
● Stage - Prospecting
● Account - Asset-related Account
● Price Book - Standard price book

Create the Opportunity Line Item under that opportunity with the following information

● Opportunity id - Recently Inserted Opportunity Id


● Quantity - Quantity on Asset Record
● Unit Price - Price on Asset Record
● PriceBookEntryId - Get the price book entry Id for the product against the Standard Price Book

Associate the selected customer on the Asset record as an Opportunity to Contact the Role

→ Apex Trigger Scenario 44

Prerequisites

● Add two picklist values in the Product Family field "Electronic" and "Books"
● Create a custom field on Opportunity Label “Product Type” and add "Electronic" and "Books" as values.

Requirement

Once the Opportunity Line Item is added related to an Opportunity check if the Product Type selected on the Opportunity is not the
same as the Product Type selected on the then do not let the Opportunity Line Item insert.

→ Apex Trigger Scenario 45


Prerequisites

● Create a couple of fields on Account with the following details


○ Opportunity MIN Date
○ Opportunity MAX Date

Requirement

Once an opportunity is created/deleted/inserted then update the “Opportunity MIN Date” AND “Opportunity MAX Date”

→ Apex Trigger Scenario 46

Prerequisites

● Create a Multi-Select picklist field on Account with A, B, C, D, E, F


● Create a Multi-Select picklist field on Opportunity with A, B, C, D, E, F

Requirement
If we update an Opportunity with this multi-select value Account should also update with the same picklist values.

→ Apex Trigger Scenario 47

Develop an Apex Trigger on Opportunity if the Opportunity is deleted then check if the Opportunity if it has Opportunity Line Item
then do not let the Opportunity get deleted.

→ Apex Trigger Scenario 48

Prerequisites
Create a field on Account - Asset Minimum Expiration Date

Requirement

When any asset is created/deleted/updated under any Account record then update the Min Usage End Date to the related
account Asset Minimum Expiration Date field.

→ Apex Trigger Scenario 49

Prerequisites

● Create a custom field on Contact and label it Total Opportunity Amount with currency datatype

When any Opportunity contact role is inserted then calculate the amount of opportunity and update the total amount into the Total
Opportunity Amount field.

→ Apex Trigger Scenario 50


Requirement

Write an Apex Trigger so that when a Contact is inserted and that Contact does not have any associated Account with it then do the
following

1. Check if the Account is already there where the name is the same as Contact FirstName + Contact LastName. If the Account
is already there then associate that account with the Contact.
2. If the Account does not exist then first create the Account with the following mapping and associate the Account with the
newly created Contact.
a. Account Name = Contact First Name + Contact Last
b. Account Rating = ‘Hot’
c. Account Phone = Contact Phone
d. Account Description = Contact Description
e. Account Shipping Address = Contact Mailing Address
f. Account Billing Address = Contact Other Address
g. Account Industry = ‘Education’
h. Account Fax = Contact Fax

→ Apex Trigger Scenario 51

Requirement

Create an Apex trigger when the contact is either created or updated and if any of the following fields is blank then throw the error.
“First Name/ Last Name & Email can not be blank.”

● First Name
● Last name
● Email

→ Apex Trigger Scenario 52

Requirement

APEX TEST CLASSES

Develop the test classes for all the Apex Trigger that you have developed as part of the Apex Trigger assignment and the test class
must need to have the following

● Use @teseSetup annotation to create the setup data


● use Utility class wherever it is possible
● Try to use runAs wherever it is possible
● Class must need to have Test.startTest & Test.stopTest()
● You must need to put assert statements in every test method
● Develop the test cases for positive and negative scenarios

EXCEPTION HANDLING

Requirement #1

Create an Account using the below code and handle the DML Exception

Account accRecord = new Account(


Rating = 'Hot',
Phone = '9087656787',
Fax = '9087656787'
);
insert accRecord;

Also, fix the issue!

Requirement #2

Below is the code that updates the account name with Exception. Execute the below code from the developer console and do the
exception handling and also fix the issue

List<Account> accountList = [Select Id FROM Account LIMIT 10];

for(Account acc : accountList){


acc.Name = acc.Name + ' Exception ';
}

update accountList;

Requirement #3

→ Create a Custom Exception Class with the name “OpportunityException”.


→ Create an Apex trigger on Opportunity and throw the Custom Exception if the Amount. is less than 50K
→ Do the Exception Handling in the Apex Class

Requirement #4

→ Create a Custom Exception Class with the name “OpportunityException”.


→ Create an Apex trigger on Opportunity and throw the Custom Exception if the Amount. is less than 50K
→ Do the Exception Handling in the Apex Class
→ When any error occurs store the error into the custom object System Event.

ASYNC APEX

Requirement #1

Create an Apex Trigger so that when a Contact is created and Email is not null then create the user for the Contact with the same
name and email.

While creating the user use the “Chatter Free User” Profile and The role must be assigned.

Requirement #2

Create a Batch Apex that will Query on Contact, that Contact does not have any associated Account with it then do the following

1. Check if the Account is already there where the name is the same as Contact FirstName + Contact LastName. If the Account
is already there then associate that account with the Contact.
2. If the Account does not exist then first create the Account with the following mapping and associate the Account with the
newly created Contact.
a. Account Name = Contact First Name + Contact Last
b. Account Rating = ‘Hot’
c. Account Phone = Contact Phone
d. Account Description = Contact Description
e. Account Shipping Address = Contact Mailing Address
f. Account Billing Address = Contact Other Address
g. Account Industry = ‘Education’
h. Account Fax = Contact Fax

Requirement #3

Create a Batch Apex that will query on the Contact record that has an account associated with them and copy the Account Shipping
Address to the Related Contacts Shipping Address.

Requirement #4

Create a batch Apex that will query all the Account Record that does not have any associated Contact. Create a Contact with the
same name as Account under that account and send the email to the admin user saying that batch processing has been completed.

Requirement #5

Create a batch apex to update the contact record phone and billing address with the phone of the related account and shipping
address respectively.

Also, send the email to admin users with the success record count and failure record account.

Requirement #6
Create a batch apex that will delete all the cases which are in New Status in the last 3 months.

Requirement #7

Create a New Custom Object called Archived Opportunity which will have the below fields.

Now, Develop a batch apex that will query all the opportunities whose close date is less than 3 months. And Insert the data into
Archive Opportunity Custom Object then delete that opportunity. Also, send an email to the system admin with the no of record
failure and success.

Fields to be created on Custom Object

Note:- Please use the correct data type as per your understanding

● Name
● Account
● Closed Date
● Amount
● Description
● Stage Name
● Opportunity Owner

Requirement #8

Create an apex class that will act as a scheduler class for the above batch apex and schedules it to run every week from the Standard
apex schedule wizard.

You might also like