(151~278)【Platform Developer I】Salesforce 認定 Platform デベロッパー 資格練習問題集と解析

◐151.A developer wrote the following two classes:

public with sharing class StatusFetcher {
    private Boolean active = true;

    private Boolean isActive() {
        return active;
    }
}

public with sharing class Calculator {
    public void doCalculations() {
        StatusFetcher sFetcher = new StatusFetcher();
        if (sFetcher.isActive()) {
            // do calculations here
        }
    }
}

The StatusFetcher class successfully compiled and saved. However, the Calculator class has a compile-time error.

How should the developer fix this code?

Options:
A. Make the doCalculations method in the Calculator class private.
B. Change the class declaration for the Calculator class to public with inherited sharing.
C. Change the class declaration for the StatusFetcher class to public with inherited sharing.
D. Make the isActive method in the StatusFetcher class public.

Correct Answer: D

Explanation:

D. Make the isActive method in the StatusFetcher class public:

  • The isActive method in the StatusFetcher class is currently private, which means it is only accessible within the StatusFetcher class itself. To allow the Calculator class to access this method, it needs to be changed to public.

Why the others are incorrect:

A. Make the doCalculations method in the Calculator class private: Changing the visibility of the doCalculations method does not resolve the issue with accessing the isActive method from the StatusFetcher class.

B. Change the class declaration for the Calculator class to public with inherited sharing: This does not address the visibility issue of the isActive method in the StatusFetcher class.

C. Change the class declaration for the StatusFetcher class to public with inherited sharing: While this would make the class accessible from other classes, it does not resolve the issue of the isActive method's visibility.


◐152.Given the code below in an Apex class:

List<Account> aList = [SELECT Id, Active_c FROM Account];

for (Account a : aList) {
    if (!a.Active_c) {
        a.Name = 'INACTIVE';
    }
}

update aList;

What should a developer do to correct the code so that there is no chance of hitting a governor limit?

Options:
A. https://developer.salesforce.com/docs/platform/lwc/guide/reference-configuration-tags.html
B. Change the DML to use Database.update(aList, false);
C. Add a LIMIT clause to the SOQL statement.
D. Add a WHERE clause to the SOQL statement.

Correct Answer: D

Explanation:

D. Add a WHERE clause to the SOQL statement:

  • Adding a WHERE clause to the SOQL query ensures that only the relevant records are retrieved and processed, reducing the risk of hitting governor limits related to the number of records. In this case, you would filter the records to only include those that have Active_c as false.

Why the others are incorrect:

A. https://developer.salesforce.com/docs/platform/lwc/guide/reference-configuration-tags.html: This option is not relevant to the Apex code or addressing governor limits.

B. Change the DML to use Database.update(aList, false);: This option allows partial success on DML operations but does not address the issue of retrieving and processing too many records, which is more directly managed by adjusting the SOQL query.

C. Add a LIMIT clause to the SOQL statement: While adding a LIMIT clause can be useful to prevent retrieving too many records, it does not solve the problem of processing potentially large datasets. Filtering with a WHERE clause is more effective in ensuring you only process relevant records.


◐153.A lead developer creates an Apex interface called "Laptop". Consider the following code snippet:

public class SilverLaptop {
    //code implementation
}

How can a developer use the Laptop interface within the SilverLaptop class?

Options:
A. @Extends(class="Laptop") public class SilverLaptop
B. public class SilverLaptop extends Laptop
C. @Interface(class="Laptop") public class SilverLaptop
D. public class SilverLaptop implements Laptop

Correct Answer: D

Explanation:

D. public class SilverLaptop implements Laptop:

  • In Apex, to use an interface within a class, the class must implement the interface. The implements keyword is used to denote that SilverLaptop will adhere to the contract defined by the Laptop interface.

Why the others are incorrect:

A. @Extends(class="Laptop") public class SilverLaptop: There is no @Extends annotation for interfaces in Apex. The correct keyword to use is implements.

B. public class SilverLaptop extends Laptop: The extends keyword is used to derive a class from another class, not for implementing interfaces.

C. @Interface(class="Laptop") public class SilverLaptop: There is no @Interface annotation in Apex. The correct approach is to use implements for interfaces.


◐154.Management asked for opportunities to be automatically created for accounts with annual revenue greater than $1,000,000. A developer created the following trigger on the Account object to satisfy this requirement:

For(Account a : Trigger.new) {
    if(a.AnnualRevenue > 1000000) {
        List<Opportunity> oppList = [SELECT Id FROM Opportunity WHERE accountId = :a.Id];
        if(oppList.size() ==0) {
            Opportunity oppty = new Opportunity(Name = a.Name, StageName = 'Prospecting', CloseDate = system.today().addDays(30));
            insert oppty;
        }
    }
}

Users are able to update the account records via the UI and can see an opportunity created for high annual revenue accounts. However, when the administrator tries to upload a list of 179 accounts using Data Loader, it fails with System.Exception errors.

Which two actions should the developer take to fix the code segment shown above? (Choose 2 answers)

Options:
A. Move the DML that saves opportunities outside the for loop.
B. Use Database.query to query the opportunities.
C. Check if all the required fields for Opportunity are being added on creation.
D. Query for existing opportunities outside the for loop.

Correct Answers: A and D

Explanation:

A. Move the DML that saves opportunities outside the for loop:

  • To avoid hitting governor limits and improve performance, DML operations should be performed outside of loops. Moving the insert statement outside the for loop will prevent multiple DML operations from being executed within the loop.

D. Query for existing opportunities outside the for loop:

  • Querying for existing opportunities inside the loop can lead to hitting the SOQL governor limit. To avoid this, query all relevant opportunities once before the loop starts and use a map to check for existing records.

Why the others are incorrect:

B. Use Database.query to query the opportunities:

  • Database.query is not necessary here; the main issue is not with the query method but with the location of the query and DML operations.

C. Check if all the required fields for Opportunity are being added on creation:

  • This is a good practice, but in this specific context, the issue with governor limits and DML operations within the loop is more pressing. Ensuring all required fields are populated is important but does not address the main problem with the code execution.


◐155.To determine the maximum number of search terms that can be provided without exceeding governor limits, let’s analyze the code snippet and Salesforce governor limits:
Code Analysis:

@AuraEnabled
public static List<List<sObject>> searchTerms(List<String> termList){
    List<List<sObject>> result = new List<List<sObject>>();
    for(string term : termList) {
        result.addAll([FIND :term IN ALL FIELDS RETURNING Account(Name), Contact(FirstName, LastName)]);
    }
    return result;
}

Key Points:

  1. SOQL Query Limits: Salesforce has limits on the number of SOQL queries that can be executed in a single transaction. However, this code uses SOSL, not SOQL, for the search operations.

  2. SOSL Query Limits: The key limit to consider here is the number of SOSL queries that can be performed in a single transaction. Salesforce allows up to 20 SOSL queries per transaction.

  3. SOSL Query Complexity: Each FIND clause in SOSL is executed as a separate query. If you use more than 20 search terms, you would exceed the SOSL governor limit.

Answer:

A. 20

Explanation:

  • The searchTerms method uses a loop to execute one SOSL query for each term in termList. Salesforce limits the number of SOSL queries that can be performed in a single transaction to 20. Therefore, the maximum number of search terms you can provide while adhering to this limit is 20.

Why the others are incorrect:

B. 150

  • Exceeds the SOSL governor limit of 20 queries per transaction.

C. 200

  • Far exceeds the SOSL governor limit of 20 queries per transaction.

D. 2000

  • Much higher than the SOSL governor limit of 20 queries per transaction.

So, the correct answer is A. 20.


◐156.A developer needs to implement a custom SOAP Web Service that is used by an external Web Application. The developer chooses to include helper methods that are not used by the Web Application in the implementation of the Web Service Class.

Which code segment shows the correct declaration of the class and methods?

Options:

A.
webservice class WebServiceClass {
    private Boolean helperMethod() { /* implementation ... */ } 
    webservice static string updateRecords() { /* implementation ... */}
}
B.
global class WebServiceClass {
    private Boolean helperMethod() { /* implementation ... */ }
    global string updateRecords() { /* implementation ... */ }
}
C.
global class WebServiceClass {
    private Boolean helperMethod() { /* implementation ... */ }
    webservice static string updateRecords() { /* implementation ... */ }
}
D.
webservice class WebServiceClass {
    private Boolean helperMethod() { /* implementation ... */ }
    global static string updateRecords() { /* implementation ... */}
}

Correct Answer: C

Explanation:

  • In Salesforce, the class must be marked as global when exposed to external systems for SOAP Web Services.

  • The webservice keyword must be applied to the method that will be exposed, which also needs to be static.

  • Helper methods should remain private to ensure they are not accessible externally.

Why the others are incorrect:

  • A: The class is incorrectly marked as webservice. It should be global to expose it externally.

  • B: The method updateRecords is marked as global, but it should use the webservice keyword to be exposed as a SOAP Web Service.

  • D: The class is declared as webservice instead of global, and global static is incorrect for the updateRecords method; it should use the webservice keyword.


◐157.Consider the following code snippet:

public static List<Lead> obtainAllFields(Set<Id> leadIds) {
    List<Lead> result = new List<Lead>();
    for(Id leadId : leadIds) {
        result.add([SELECT FIELDS(ALL) FROM Lead WHERE Id = :leadId]);
    }
    return result;
}

Given the multi-tenant architecture of the Salesforce platform, what is a best practice a developer should implement and ensure successful execution of the method?

Options:
A. Avoid executing queries without a limit clause.
B. Avoid returning an empty List of records.
C. Avoid using variables as query filters.
D. Avoid performing queries inside for loops.

Correct Answer: D

Explanation:
Performing queries inside a loop can lead to SOQL query limit exceptions in Salesforce due to the governor limits. In a multi-tenant environment, it’s important to bulkify operations by performing the SOQL query outside the loop. The correct approach is to run a single query that retrieves all records at once, and then process them within the loop if necessary.

Why the others are incorrect:

  • A: Using a limit clause is good practice when fetching large sets of data but is not the most relevant concern in this scenario.

  • B: Returning an empty list is not harmful and is an acceptable practice in many situations.

  • C: Using variables as query filters is a common practice and is not a bad practice.


◐158
Example 1:

AggregateResult[] groupedResults = [SELECT CampaignId, AVG(Amount) FROM Opportunity GROUP BY CampaignId];

for (AggregateResult ar : groupedResults) {
    System.debug('Campaign ID: ' + ar.get('CampaignId'));
    System.debug('Average amount: ' + ar.get('expr0'));
}

Example 2:

AggregateResult[] groupedResults = [SELECT CampaignId, AVG(Amount) theAverage FROM Opportunity GROUP BY CampaignId];

for (AggregateResult ar : groupedResults) {
    System.debug('Campaign ID: ' + ar.get('CampaignId'));
    System.debug('Average amount: ' + ar.get('theAverage'));
}

Example 3:

AggregateResult[] groupedResults = [SELECT CampaignId, AVG(Amount) FROM Opportunity GROUP BY CampaignId];

for (AggregateResult ar : groupedResults) {
    System.debug('Campaign ID: ' + ar.get('CampaignId'));
    System.debug('Average amount: ' + ar.get.AVG());
}

Example 4:

AggregateResult[] groupedResults = [SELECT CampaignId, AVG(Amount) theAverage FROM Opportunity GROUP BY CampaignId];

for (AggregateResult ar : groupedResults) {
    System.debug('Campaign ID: ' + ar.get('CampaignId'));
    System.debug('Average amount: ' + ar.theAverage);
}

Question:

Which two examples above use the System.debug statements to correctly display the results from the SOQL aggregate queries?

Options:

A. Example 1
B. Example 2
C. Example 3
D. Example 4


Correct Answers:

A, B

Explanation:

  • Example 1: This uses ar.get('CampaignId') to retrieve the campaign ID and ar.get('expr0') to retrieve the average amount. The alias expr0 is automatically assigned to the first aggregate function in the query, which works correctly.

  • Example 2: In this case, the query explicitly uses the alias theAverage for the AVG(Amount) function, and ar.get('theAverage') retrieves the correct value from the result.

Why the others are incorrect:

  • C: ar.get.AVG() is invalid syntax. The correct way to access fields from an AggregateResult is using ar.get().

  • D: ar.theAverage is also incorrect syntax. Aggregate results must be accessed using the get() method, not through dot notation like ar.theAverage.


◐159.Given the following code snippet, which is part of a custom controller for a Visualforce page:

public void updateContact(Contact thisContact) {

    thisContact.Is_Active__c = false;

    try {
        update thisContact;
    } catch(Exception e) {
        String errorMessage = 'An error occurred while updating the Contact. ' + e.getMessage();
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, errorMessage));
    }
}

In which two ways can the try/catch be enclosed to enforce object and field-level permissions and prevent the DML statement from being executed if the current logged-in user does not have the appropriate level of access?

Options:

A. Use if (thisContact.OwnerId == UserInfo.getUserId())
B. Use if (Schema.sObjectType.Contact.isUpdatable())
C. Use if (Schema.sObjectType.Contact.isAccessible())
D. Use if (Schema.sObjectType.Contact.fields.Is_Active__c.isUpdateable())


Correct Answers:

B, D


Explanation:

  • B. Schema.sObjectType.Contact.isUpdatable(): This checks whether the current user has permission to update the Contact object as a whole. It ensures that object-level update permissions are respected before performing the DML operation.

  • D. Schema.sObjectType.Contact.fields.Is_Active__c.isUpdateable(): This checks whether the current user has permission to update the specific Is_Active__c field on the Contact object, enforcing field-level security.


Why the others are incorrect:

  • A. if (thisContact.OwnerId == UserInfo.getUserId()): This checks ownership of the record but does not enforce object or field-level permissions, so it would not guarantee that the user has permission to update the record or its fields.

  • C. if (Schema.sObjectType.Contact.isAccessible()): This checks whether the Contact object is accessible for the current user, which refers to read access, not update permissions.


◐160.Which two examples correctly display the results from the SOQL aggregate queries in the System.debug statements?

Code Examples:

Example 1:

AggregateResult[] groupedResults = [SELECT CampaignId, AVG(Amount) FROM Opportunity GROUP BY CampaignId];

for (AggregateResult ar : groupedResults) {
    System.debug('Campaign ID' + ar.get('CampaignId'));
    System.debug('Average amount' + ar.get('expr0'));
}

Example 2:

AggregateResult[] groupedResults = [SELECT CampaignId, AVG(Amount) theAverage FROM Opportunity GROUP BY CampaignId];

for (AggregateResult ar : groupedResults) {
    System.debug('Campaign ID' + ar.get('CampaignId'));
    System.debug('Average amount' + ar.get('theAverage'));
}

Example 3:

AggregateResult[] groupedResults = [SELECT CampaignId, AVG(Amount) FROM Opportunity GROUP BY CampaignId];

for (AggregateResult ar : groupedResults) {
    System.debug('Campaign ID' + ar.get('CampaignId'));
    System.debug('Average amount' + ar.get.AVG());
}

Example 4:

AggregateResult[] groupedResults = [SELECT CampaignId, AVG(Amount) theAverage FROM Opportunity GROUP BY CampaignId];

for (AggregateResult ar : groupedResults) {
    System.debug('Campaign ID' + ar.get('CampaignId'));
    System.debug('Average amount' + ar.theAverage);
}

Options:

A. Example 1
B. Example 2
C. Example 3
D. Example 4


Correct Answers:

A. Example 1
B. Example 2


Explanation:

  • Example 1: Correct. When using an aggregate function without an alias in SOQL, Salesforce assigns the default alias expr0 for the first aggregate result. Hence, using ar.get('expr0') correctly retrieves the average amount.

  • Example 2: Correct. The AVG(Amount) is given an alias theAverage in the SOQL query, so ar.get('theAverage') correctly retrieves the average amount.


Why the others are incorrect:

  • Example 3: Incorrect. The method ar.get.AVG() does not exist. Instead, the result should be accessed using ar.get('expr0') (default alias) when no alias is provided.

  • Example 4: Incorrect. ar.theAverage is not valid. You must use ar.get('theAverage') to retrieve the value in Salesforce AggregateResult objects.


ここから先は

153,503字

¥ 500

この記事が気に入ったらサポートをしてみませんか?