Salesforce-Platform-Developer-II Practice Test Questions

202 Questions


A developer created a Lightning web component mat allows users to Input a text value that is used to search for Accounts by calling an Apex method. The Apex method returns a list of account to apply and is called imperatively from a JavaScript event handler.

Which two changes should the developer make so the Apex method functions correctly?
Choose 2 answers


A. Add @AuraEnabled to line 09.


B. Add @AuraEnabled to line 03.


C. Add @AuraEnabled to lines 11 and 12.


D. Add @AuraEnabled to line 01.





A.
  Add @AuraEnabled to line 09.

B.
  Add @AuraEnabled to line 03.

βœ… Explanation:

πŸ“˜ Scenario Summary:
➝ A Lightning Web Component (LWC) is calling an Apex method imperatively from JavaScript.
➝ The Apex method returns a list of wrapper objects, which include Account and a custom matchProbability field.
➝ For the LWC to call this Apex method and process the returned data, specific annotations are required.

πŸ” Line-by-Line Breakdown:

βœ… A. Add @AuraEnabled to line 09.
➝ The AccountWrapper is returned to LWC.
➝ For LWC to deserialize and use the properties of this wrapper class (account, matchProbability), each property must be annotated with @AuraEnabled.
➝ Required for serialization over the wire between Apex and LWC.

βœ… B. Add @AuraEnabled to line 03.
➝ The method search() must be @AuraEnabled to be accessible by LWC.
➝ Since this method is being called imperatively, @AuraEnabled is sufficient (you only need @AuraEnabled(cacheable=true) for wired methods).
➝ It also needs to be public static, which it already is.

❌ C. Add @AuraEnabled to lines 11 and 12.
➝ This is partially correct in idea, but line 09 is the start of the AccountWrapper class, which is where you’d annotate the properties inside the class, not specifically lines 11 and 12.
➝ Option A already covers this accurately.

❌ D. Add @AuraEnabled to line 01.
➝ @AuraEnabled is not valid on classes.
➝ It is only applied to:
βž₯ Methods you want to expose to Lightning components
βž₯ Properties inside wrapper classes returned to Lightning

πŸ“š References:
Lightning Web Components and Apex
@AuraEnabled Annotation

Instead of waiting to send emails to support personnel directly from the finish method of a batch Apex process, Universal Containers wants to notify an external system in the event that an unhandled exception occurs. What is the appropriate publish/subscribe logic to meet this requirement?


A. Publish the error event using the Eventbus. publish () method.


B. No publishing is necessary. Have the external system subscribe to the BatchapexErrorEvent.


C. Publish the error event using the addError method.


D. Publish the error event with a Flow.





B.
  No publishing is necessary. Have the external system subscribe to the BatchapexErrorEvent.

βœ… Explanation:

Salesforce provides a standard platform event called BatchApexErrorEvent that is automatically published by the platform whenever an unhandled exception occurs in a Batch Apex job (in the start, execute, or finish methods).

This event can be consumed by:
➝ External systems via CometD (Streaming API)
➝ Internal automation via Platform Event-triggered Flows or Apex Triggers

πŸ” Why Option B is Correct
➝ No custom publishing logic is needed.
➝ The platform takes care of publishing the BatchApexErrorEvent.
➝ Your external system simply needs to subscribe to this event using the Streaming API or Change Data Capture/CometD mechanism.
➝ This approach is reliable, scalable, and aligns with Salesforce event-driven architecture.

❌ Why the Other Options Are Incorrect

A. Publish the error event using EventBus.publish()
➝ Incorrect because you don’t need to manually publish the error.
➝ Salesforce handles it automatically via the BatchApexErrorEvent.

C. Publish the error event using addError
➝ addError() is used to prevent DML from proceeding and is meant for user feedback or validation, not for error notification or event publishing.

D. Publish the error event with a Flow
➝ Flows can publish custom platform events, but BatchApexErrorEvent is standard and auto-published.
➝ Using Flows to publish custom error events adds unnecessary complexity and still wouldn't catch unhandled Batch Apex errors as reliably.

πŸ“š References:
🧩 BatchApexErrorEvent – Salesforce Developer Guide
🧩 Event Monitoring for Batch Apex
🧩 Streaming API and CometD

Which Salesforce feature allows a developer to see when a user last logged in to Salesforce if real-time notification is not required?


A. Calendar Events


B. Asynchronous Data Capture Events


C. Event Monitoring Log


D. Developer Log





C.
  Event Monitoring Log

βœ… Explanation:

Event Monitoring is a Salesforce feature that allows developers and admins to track and analyze user activity, including login history, via log files. These logs include valuable information such as:

βž₯ Login Time
βž₯ User ID
βž₯ IP Address
βž₯ Login Status
βž₯ Login Type (UI, API, etc.)

This data is not real-time (which aligns with the question requirement) and is available through:

➟ Event Log Files (ELFs), accessible via:
βž₯ REST API
βž₯ Event Log File Browser App (in AppExchange)
βž₯ Shield Event Monitoring (for more advanced visibility, if licensed)

❌ Why Other Options Are Incorrect

A. Calendar Events
➟ Related to user-created appointments and tasks, not login or security tracking.
➟ Does not provide login data or system activity logs.

B. Asynchronous Data Capture Events
➟ This option doesn't exist in Salesforce as a specific named feature.
➟ Might be confused with Change Data Capture, which tracks data changes, not user logins.

D. Developer Log (Debug Log)
➟ Shows logs for code execution (Apex, triggers, etc.) for specific users.
➟ Only logs activity when explicitly enabled.
➟ Does not passively track logins or provide consistent historical login data.

πŸ“š Reference Documentation:
1. Salesforce Event Monitoring Overview
2. Event Log File Browser (AppExchange Tool)
3. Login History in Salesforce

A company's support process dictates that any time a case is closed with a status of "Could net fix," an Engineering Review custom object record should be created and populated with information from the case, the contact, and any of the products associated with the case. What Is the correct way to automate this using an Apex trigger?


A. An after update trigger on Case that creates the Engineering Review record and inserts it


B. An after upset trigger on Case that creates the Engineering Review record and inserts It .


C. A before update trigger on Case that creates the Engineering Review record and Inserts It


D. A before upset trigger on Case that creates the Engineering Review record and Inserts it





A.
  An after update trigger on Case that creates the Engineering Review record and inserts it

βœ… Explanation:

This automation needs to occur after a Case is updated, specifically when its Status = 'Could not fix' and the Case is closed. Based on these conditions, let’s break down why an after update trigger is the right choice.

πŸ” Why after update is correct:
➑️ You’re creating a new related record (Engineering_Review__c) based on the values of the Case, Contact, and associated Products.
➑️ Accessing related objects (like Case.Contact or Case.Products) is more reliable in an after trigger, because the parent Case record has already been committed to the database.
➑️ You cannot safely perform DML operations (like insert) on unrelated records inside a before trigger without risk of partial commits or recursion issues.

❌ Why Other Options Are Incorrect:

B. An after upsert trigger
➜ There's no such thing as a trigger type named after upsert.
➜ Triggers respond to insert, update, delete, etc., not upsert directly.
➜ Upsert is a DML operation, but you'd still write before insert, after update, etc.

C. A before update trigger
➜ You should not perform DML operations (like creating/inserting new records) in a before trigger for unrelated objects.
➜ before triggers are intended for modifying the same records being saved β€” not for creating new ones.

D. A before upsert trigger
➜ Again, no such trigger exists.
➜ And performing inserts in a before context is not recommended for this use case.

βœ… Use Case Best Practice Summary:
➜ Use an after update trigger when creating related records after the main record (here, Case) has been successfully updated.
Check the status condition inside the trigger:
➜ Only create Engineering_Review__c when Status == 'Could not fix' and it's a transition from some other status (to avoid duplicates).
➜ Always bulkify the logic to handle multiple Cases.

πŸ“š Reference:
➜ Salesforce Apex Triggers – Developer Guide
➜ Trigger Context and Best Practices

Which three Visualforce components can be used to initiate Ajax behavior to perform partial page updates?
(Choose 3 answers)


A. Option A


B. Option B


C. Option C


D. Option D


E. Option E





B.
  Option B

D.
  Option D

E.
  Option E

βœ… Explanation:

βœ… B.
➞ Can initiate an Ajax request by setting action and reRender attributes.
➞ When used with reRender, it triggers a partial page update for specified components.

βœ… D.
➞ Manages the status (like a spinner or loading message) during an Ajax call.
➞ Works with components like and .
➞ Doesn't initiate the Ajax request directly but supports and enhances the Ajax behavior.

βœ… E.
➞ Adds Ajax behavior to non-Ajax components (like , , etc.).
➞ Fires Ajax events like onchange, onclick, etc., and can reRender specific parts of the page.

❌ A. (misspelled in the option)
➞ This would normally be a correct answer, but it appears to be a misspelled or invalid option ().
➞ If it were , then yes, it supports Ajax via reRender.

❌ C.
➞ Required as a container for input components and command buttons but does not initiate Ajax behavior.
➞ It simply enables interaction between components but does not trigger partial page updates by itself.

πŸ“š References:
➩ Visualforce Developer Guide – Ajax Components
➩ Understanding
➩ Visualforce

A developer needs to add code to a Lightning web component's configuration file so the component only renders for a desktop size form factor when on a record page. What should the developer add to the component's record page target configuration to meet this requirement?


A. Option A


B. Option B


C. Option C


D. Option D





B.
  Option B

βœ… Explanation:

The correct choice is Option B.

➀ Salesforce Lightning Web Components allow you to control on which device form factors a component is exposed by using the element in your component’s metadata. By specifying only the β€œLarge” form factor, you ensure that the component appears on record pages when viewed in a desktop (or large-screen) context, but is hidden when the same page is accessed from a phone or tablet. This mechanism is declarative and built into the Lightning framework, so it doesn’t require any additional Apex or JavaScript logic to enforce the restriction.

➀ either of the other options meets this requirement: the tag (Option A) is solely for design-time property exposure in the App Builder; the tag (Option C) relates to component internals and layout arrangement, not availability; and with a β€œformFactor” name (Option D) isn’t a recognized metadata instruction for controlling form factors.

➀ By leveraging the supported form factors feature, you maintain clean separation of concernsβ€”your component’s rendering logic remains focused on business requirements, while the metadata governs where it can be placed.

Reference:
– Salesforce Lightning Web Components Metadata Configuration, Supported Form Factors

Consider the following code snippet:

How should the component communicate to the


A. Create and fire a component event.


B. Create and fire an application event.


C. Create and fire a standard DOM event.


D. Create and dispatch a custom event,





D.
  Create and dispatch a custom event,

βœ… Explanation:

The code snippet shows a Lightning Web Component (LWC) using a template for: each> directive to iterate over a orders.data collection, rendering an component for each order item. The orderId is bound to order.Id. The question implies that the component needs to communicate some information or action (e.g., a selection or change) to another part of the application, likely a parent component or another handler.

In LWC, communication between components can occur in several ways:
➜ Component Events: These are custom events fired from a child component to its parent component. This is the most common and recommended approach for parent-child communication in LWC.
➜ Application Events: These are global events that can be handled by any component, but they are less common and generally discouraged in favor of component events or the pub/sub model.
➜ Standard DOM Events: These are native browser events (e.g., click, change) and are not typically used for custom component communication in LWC.
➜ Custom Events: In LWC, custom events are created and dispatched using the CustomEvent API, which is the mechanism for firing component events.

Given that the component is likely a child component within the component, the most appropriate way to communicate (e.g., to notify the parent of an action like order selection) is by firing a custom event from the child to the parent. This aligns with LWC’s event-driven architecture, where child components use dispatchEvent with a CustomEvent to send data upward.

Correct Answer:
D. Create and dispatch a custom event.

Steps to Implement:
In the component, create a custom event using this.dispatchEvent(new CustomEvent('eventname', { detail: data }));, where data could include orderId or other relevant information. In the parent component, handle the event using the on syntax in the template (e.g., ), and define the handleNotify method to process the event data.

Reference:
Communicate with Events
Lightning Web Components Basics

An org has a requirement that addresses on Contacts and Accounts should be normalized to a company standard by Apex code any time that they are saved. What is the optimal way to implement this?


A. Apex triggers on Contact and Account that call a helper class to normalize the address


B. Apex trigger on Account that calls the Contact trigger to normalize the address


C. Apex triggers on Contact and Account that normalize the address


D. Apex trigger on Contact that calls the Account trigger to normalize the address





A.
  Apex triggers on Contact and Account that call a helper class to normalize the address

βœ… Explanation:

To meet the requirement of normalizing addresses on both Contact and Account whenever they're saved, Option A is the most modular, maintainable, and scalable approach.

βœ… Why A is correct:
➝ Having triggers on both objects ensures that address normalization logic is run independently whenever either object is inserted or updated.
➝ By delegating the logic to a shared helper class, the code becomes reusable, DRY (Don’t Repeat Yourself), and easier to test.
➝ This structure keeps the trigger lightweight and avoids logic duplication.

πŸ” This follows the Trigger β†’ Handler β†’ Helper design pattern, which is a Salesforce best practice.

❌ Why B is incorrect:
➝ An Apex trigger on Account that calls the Contact trigger is not supported or reliable. Triggers don’t call each other explicitly.
➝ Cross-object triggering in this way would require an update to the Contact, which could lead to recursive triggers and governor limit issues.

❌ Why C is incorrect:
➝ Although it works, placing normalization logic directly inside the triggers makes the code hard to maintain, difficult to test, and not reusable.
➝ This approach violates the Single Responsibility Principle and does not align with enterprise coding standards.

❌ Why D is incorrect:
➝ Similar to B, calling one trigger from another object’s trigger is not a valid or reliable pattern.
➝ Account and Contact are separate sObjects, and their triggers don’t naturally invoke one another.
➝ It would require updates that could lead to performance problems, complex debugging, or infinite loops.

πŸ“š Reference:
➝ Apex Trigger Best Practices – Salesforce Developer Guide

🧠 Key Takeaway:
Use triggers on both objects and delegate logic to a shared Apex helper class to ensure clean, maintainable, and scalable code.

Given a list of Opportunity records named opportunityList, which code snippet is best for querying all Contacts of the Opportunity's Account?


A. Option A


B. Option B


C. Option C


D. Option D





A.
  Option A

βœ… Explanation:

The goal is to retrieve all Contacts associated with the Accounts linked to the Opportunities in opportunityList. This involves:
Collecting the unique Account IDs from the Opportunities.
Querying the Accounts using those IDs.
Using a subquery to fetch the related Contacts for each Account.

Let’s analyze each option:

Option A:
Collects Account IDs into a Set from opportunityList.
Queries Accounts where Id IN :accountIds and includes a subquery for Contacts.
Adds all Contacts from the queried Accounts to contactList.
Issue: This works but is inefficient because it queries all Accounts and their Contacts, even if some Accounts might not be relevant if opportunityList is incomplete or contains duplicates.

Option B:
Queries a single Account where Id = o.AccountId (for each Opportunity) and includes a subquery for Contacts.
Adds all Contacts to contactList.
Issue: This is highly inefficient as it performs a separate SOQL query for each Opportunity in opportunityList, potentially hitting governor limits (e.g., 100 SOQL queries per transaction).

Option C:
Directly queries Contacts where AccountId IN :opportunityList.AccountId.
Adds each Contact to contactList.
Issue: This assumes opportunityList.AccountId is a valid collection, but it’s notβ€”AccountId is a field on each Opportunity, not a list. This code will fail to compile due to invalid syntax.
Option D:
Collects Contact IDs into a Set from Contacts where Id IN :contactIds.
However, the initial step collects contactIds from opportunityList (which should be AccountId), and then queries Contacts based on those IDs.
Issue: This approach is flawed because it tries to collect Contact IDs from Opportunities (which don’t directly relate to Contacts) and then queries Contacts based on those IDs, which doesn’t align with the requirement.

Correct Answer:
Option A is the best approach among the given options. It:
Efficiently collects unique Account IDs from opportunityList.
Uses a single SOQL query with a subquery to fetch Accounts and their related Contacts.
Avoids governor limit issues by leveraging a bulkified query.

Why Option A is Best:
It uses a Set to deduplicate Account IDs, preventing redundant queries.
The subquery SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Id IN :accountIds retrieves all Contacts for the relevant Accounts in one query.
This aligns with Salesforce best practices for bulk processing and minimizes SOQL usage.

Potential Improvement:
In a real-world scenario, you might want to ensure opportunityList is not empty and handle cases where AccountId might be null. However, based on the options provided, Option A is the most correct and efficient.

Reference:
Salesforce SOQL and SOSL
Apex Basics & Database

What should a developer use to query all Account fields for the Acme account in their sandbox?


A. SELECT FIELDS FAOM Account WHERE Name = β€˜Acme’ LIMIT 1


B. SELECT FIELDS (ALL) FROM Account WHERE Name = β€˜Acme’ LIMIT 1


C. SELECT ALL FROM Account WHERE Name = "Acme’ LIMIT 1


D. SELECT * FROM Recount WHERE Names = β€˜Aeme’ LIMIT 1





B.
  SELECT FIELDS (ALL) FROM Account WHERE Name = β€˜Acme’ LIMIT 1

βœ… Explanation:

In Salesforce, SOQL (Salesforce Object Query Language) is used to query data. The goal is to retrieve all fields for an Account record where the Name is 'Acme' and limit the result to one record (assuming 'Acme' is unique). Let’s analyze the options:

Option A:
Issue: SELECT FIELDS is not valid SOQL syntax. The correct syntax to retrieve all fields is SELECT FIELDS(ALL) or SELECT FIELDS(STANDARD)/FIELDS(CUSTOM), but the keyword must be followed by a specifier in parentheses.
Result: This query will fail to execute.

Option B:
Correctness: This uses the valid SOQL syntax FIELDS(ALL), which retrieves all fields (standard, custom, and system fields) for the Account object.
Consideration: FIELDS(ALL) includes all fields, even those that might not be accessible or relevant, but it is a valid approach for querying all fields.
Result: This is a correct and efficient way to query all Account fields for the 'Acme' account, limited to one record.

Option C:
Issue: SELECT ALL is not valid SOQL syntax. The correct syntax for selecting all fields is FIELDS(ALL) or explicitly listing fields. Additionally, the quotation marks are mismatched ("Acme’ should be 'Acme' or "Acme" consistently).
Result: This query will fail due to invalid syntax.

Option D:
Issue:
SELECT * is not valid SOQL syntax; Salesforce does not support the asterisk (*) wildcard like SQL.
Recount seems to be a typo and should be Account.
Names should be Name (case-sensitive field name mismatch).
Aeme appears to be a typo for Acme.
Result: This query will fail due to multiple syntax errors.

Correct Answer:
Option B: SELECT FIELDS (ALL) FROM Account WHERE Name = β€˜Acme’ LIMIT 1

Why Option B is Best:
It uses the correct SOQL syntax FIELDS(ALL) to retrieve all fields (standard, custom, and system) for the Account object.
The WHERE Name = β€˜Acme’ clause filters for the 'Acme' account.
The LIMIT 1 ensures only one record is returned, which is appropriate if 'Acme' is expected to be unique.
This approach is efficient and aligns with Salesforce’s query capabilities for retrieving all fields dynamically.

Additional Notes:
In a production environment, ensure the developer has access to all fields (e.g., via field-level security).
For better performance and maintainability, explicitly listing required fields is often recommended over FIELDS(ALL) unless all fields are truly needed, but for this specific requirement (querying all fields), Option B is appropriate.

Reference:
Salesforce SOQL and SOSL Reference: FIELDS() Syntax
Trailhead Module: Data Modeling

A company has many different unit test methods that create Account records as part of their data setup. A new required field was added to the Account and now all of the unit tests fail. What is the optimal way for a developer to fix the issue?


A. Add the required field to the data setup for all of the unit tests.


B. Add a before insert trigger on Account to set the value of the required field,


C. Change the required field to be a validation rule that excludes the System Administrator profile.


D. Create a TestDataFactory class that serves as the single place to create Accounts for unit tests and set the required field there.





D.
   Create a TestDataFactory class that serves as the single place to create Accounts for unit tests and set the required field there.

βœ… Explanation:

When a required field is added to the Account object, all unit tests that create Account records must now include that field to avoid errors. Rather than modifying every test individually, the best long-term and scalable solution is to centralize the test data creation logic.

βœ… Why D is correct:
➝ A TestDataFactory class is a Salesforce testing best practice.
➝ It creates a single source of truth for all test data setup.
➝ When changes occur (like a new required field), you only need to update one place, and all tests will work again.
➝ It promotes code reuse, cleaner tests, and reduced maintenance effort.

πŸ“Œ A typical TestDataFactory would include methods like createTestAccount(), which builds and returns a complete Account with all required fields set.

❌ Why A is incorrect:
➝ Manually updating each unit test with the new required field is time-consuming, error-prone, and hard to maintain.
➝ This duplicates logic and violates the DRY principle (Don't Repeat Yourself).

❌ Why B is incorrect:
➝ Using a trigger to auto-populate required fields just to satisfy test methods is a bad practice.
➝ Triggers should reflect business logic, not act as a workaround for test data setup.
➝ It can lead to unintended behavior in production and mask real issues.

❌ Why C is incorrect:
➝ Modifying a required field to be a validation rule with profile exceptions weakens your data integrity.
➝ It's a security and logic loopholeβ€”especially using profile-specific logic in validation rules.
➝ This is a hacky workaround, not a sustainable or scalable solution.

πŸ“š Reference:
Salesforce Apex Developer Guide – Test Data Factory Pattern
Testing Best Practices – Salesforce Developer Guide

🧠 Key Takeaway:
Always centralize test data logic using a TestDataFactory class to ensure test resilience, improve maintainability, and follow Salesforce test design best practices.

A company wants to run different logic based on an Opportunity's record type. Which code segment handles this request and follows best practices?


A. Option A


B. Option B


C. Option C


D. Option D





C.
  Option C

βœ… Explanation:

The requirement is to execute different logic based on the Opportunity's record type (e.g., 'New' or 'Renewal') within a trigger. Best practices in Salesforce Apex include:
➝ Minimizing SOQL queries inside loops to avoid governor limits.
➝ Using efficient data retrieval methods.
➝ Ensuring maintainable and scalable code.

Option A:
➝ Queries RecordType for 'New' and 'Renewal' separately with LIMIT 1.
➝ Uses these IDs in an if-else block within a trigger loop to apply logic.
➝ Issues:
βž₯ Performs two SOQL queries outside the loop, which is acceptable but could be optimized by querying all relevant record types once.
βž₯ Relies on hardcoded DeveloperNames ('New', 'Renewal'), which is fine but less flexible if names change.

➝ Pros: Avoids SOQL inside the loop, follows bulkification principles.

Option B:
➝ Queries all RecordType records for the Opportunity object into a List without filtering by DeveloperName.
➝ Issues:
βž₯ Does not assign or use the recTypes list, making the query ineffective for the logic.
βž₯ Lacks a mechanism to map record types to IDs or names for use in the trigger.
➝ Pros: Queries all record types in one go, which is efficient if used properly.
➝ Cons: Incomplete implementation; the logic cannot work without further processing of recTypes.

Option C:
➝ Uses Schema to get record type info by DeveloperName for 'New' and 'Renewal' without a SOQL query.
➝ Applies logic based on RecordTypeId in the trigger loop.
➝ Pros:
βž₯ Avoids SOQL entirely, which is highly efficient and avoids governor limits.
βž₯ Uses Schema methods, a best practice for retrieving metadata.

➝ Cons: Assumes the DeveloperNames exist; if they don’t, it will cause a runtime error unless handled.
Option D:
➝ Checks the RecordType.Name directly in the trigger loop.
➝ Issues:
βž₯ Accessing RecordType.Name requires the record type to be queried or related, but no query is performed.
βž₯ This will result in a NullPointerException because RecordType is not populated without a SOQL or relationship query.
➝ Cons: Violates bulkification (SOQL would be needed inside the loop if fixed) and is prone to errors.

Why Option C is Best:
It leverages Schema.DescribeSObjectResult and getRecordTypeInfosByDeveloperName() to retrieve record type IDs without SOQL, which is the most efficient approach.
Avoids governor limits by not using database queries inside the trigger loop.
Follows Salesforce best practices by using metadata API calls for configuration data.
The code is maintainable and scalable, as it doesn’t rely on hardcoded queries that might need adjustment if record types change.

Implementation Note:
The code assumes the DeveloperNames 'New' and 'Renewal' exist. In a production environment, you should add error handling (e.g., check if the map contains the key) to handle cases where record types are missing.

Reference:
Salesforce Apex Developer Guide: Schema Methods
Trailhead Module: Apex Triggers


Page 7 out of 17 Pages
Previous