Salesforce+Developer+Questions
Salesforce+Developer+Questions
Index
COLLECTION IN SALESFORCE
LOOPS IN SALESFORCE
GETTERS/SETTERS IN SALESFORCE
SOQL
INHERITANCE
APEX TRIGGER
TEST CLASS
EXCEPTION HANDLING
ASYNC APEX
QUEUEABLE APEX
BATCH APEX
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
SCHEDULED APEX
→ 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;
Develop an Apex Trigger that will update the Contact Phone Value to Contact Fax every time a Contact is updated.
Develop an Apex Trigger so that when an opportunity is created and the account is blank, create a task under Opportunity. Hint: -
AccountId == null
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
Develop an Apex Trigger on the Contact record so that when any Contact is created under any Account Record then, please
Shipping Address
● ShippingStreet
● ShippingCity
● ShippingState
● ShippingPostalCode
● ShippingCountry
Billing Address
● BillingStreet
● BillingCity
● BillingState
● BillingPostalCode
● BillingCountry
Mailing Address
● MailingStreet
● MailingCity
● MailingState
● MailingPostalCode
● MailingCountry
Other Address
● OtherStreet
● OtherCity
● OtherState
● OtherPostalCode
● OtherCountry
→ Apex Trigger Scenario 11
Prerequisite
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
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.
→ 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”.
Whenever an Opportunity is getting created inside Salesforce and the Amount field is blank then update the Amount field with 1000.
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.
● 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.
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.
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
→ 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
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.
→ 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
Requirement
→ 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.
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: -
● 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
● 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.
→ 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
Suboptimal Solution
→ Apex Trigger 32
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.
● Product2
● PriceBookEntry
● OpportunityLineItem
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
A new contact <Contact> has been assigned to you. Here are the details about the contact
Write a trigger on contact to prevent duplicate records based on Contact Email & Contact Phone.
A B
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.
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.
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;
Prerequisites
Requirement
Once any student record is created then the record should automatically be shared with the users who belong to the profile
“Standard User”
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
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
Create the Opportunity Line Item under that opportunity with the following information
Associate the selected customer on the Asset record as an Opportunity to Contact the Role
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.
Requirement
Once an opportunity is created/deleted/inserted then update the “Opportunity MIN Date” AND “Opportunity MAX Date”
Prerequisites
Requirement
If we update an Opportunity with this multi-select value Account should also update with the same picklist values.
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.
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.
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.
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
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
Requirement
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
EXCEPTION HANDLING
Requirement #1
Create an Account using the below code and handle the DML Exception
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
update accountList;
Requirement #3
Requirement #4
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.
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.