Process Automation and Logic Flashcards
When the number of records in a recordset is unknown, which control statement should a developer use to implement a set of code that executes for every record in the recordset, without performing a .size() or .length() method call?
A. do { … } while (condition)
B. while (condition) {…}
C. for (init_stmt; exit_condition; increment_stmt) { … }
D. for (variable : list_or_set) { … }
D. for (variable : list_or_set) { … }
List or set iteration for loops do not need the size of the collection to run.
What is the value of x after the code segment executes?
String x = 'A'; Integer i = 10; if ( i < 15 ){ i = 15; x = 'B'; } elseif ( i < 20 ){ x = 'C'; } else { x = 'D'; }
A. ‘A’
B. ‘B’
C. ‘C’
D. ‘D’
B. ‘B’
These if statements are evaluating the value of i. Since i is equal to 10 then i is less than 15 and i will be reassigned the value 15 and x will be equal to ‘B’. Once the values are assigned the if statement is no longer run.
Which three are accurate statements about variable scope? (Select three answers.)
A. A variable can be defined at any point in a block.
B. Sub-blocks cannot reuse a parent block’s variable name.
C. Sub-blocks can reuse a parent block’s variable name if its value is null.
D. Parallel blocks can use the same variable name.
E. A static variable can restrict the scope to the current block if its value is null.
A. A variable can be defined at any point in a block.
B. Sub-blocks cannot reuse a parent block’s variable name.
D. Parallel blocks can use the same variable name.
A variable is valid from the point where it is declared inside of the code.
Variables can be defined anywhere within a block. However Sub-blocks can’t redefine a variable name that has already been used within a parent block.
The same name can be used inside parallel blocks because the scope of the variable is limited only to the particular block that it was defined in.
Which two users can edit a record after it has been locked for approval?
An administrator and a user who is assigned as the current approver
What happens to changes in the result when a Visualforce page calls an Apex controller, which calls another Apex class, and then hits a governor limit?
Any changes up to the error are rolled back.
A developer in a Salesforce org with 100 accounts executes the following code using the Developer Console:
Account myAccount = new Account(Name='MyAccount'); insert myAccount; for (Integer x = 0; x < 150; x++){ Account newAccount = new Account(Name='MyAccount' + x); try{ insert newAccount; } catch (Exception ex){ System.Debug(ex); x = 'D'; } } insert new Account(Name='MyAccount');
How many accounts are in the org after this code is run?
A. 100
B. 101
C. 102
D. 252
A. 100
Salesforce has a governor limit of 150 DML statements per transaction. The Salesforce org originally starts with 100 accounts. The first DML statement inserts the first account leaving 149 DML statements available. Inside the for loop it tries to make 150 DML statements. But the limit is reached when x = 149. When this happens the transaction aborts and all data changes are rolled back. The only thing that is left is the original 100 accounts. Since the error happened the last insert statement is never reached. Best practice is to avoid DML statements inside for loops.
Which two statements should a developer avoid using inside procedural loops? (Select two answers.)
A. List contacts = [select id, salutation, firstname, lastname, email from Contact where accountId = :a.Id];
B. update contactList;
C. if (o.accountid == a.id)
D. System.debug(‘Amount of CPU time (in ms) used so far: ‘ + Limits.getCpuTime());
A. List contacts = [select id, salutation, firstname, lastname, email from Contact where accountId = :a.Id];
B. update contactList;
Avoid using SOQL in loops since there is a governor limit that enforces a maximum number of SOQL queries. When these operations are placed inside a loop, database operations are invoked once per iteration of the loop making it very easy to reach these governor limits. Best practice is to query once to obtain all results, and then iterate over the results.
Avoid using DML in loops since there is a governor limit that enforces a maximum number of DML statements. When these operations are placed inside a loop, database operations are invoked once per iteration of the loop making it very easy to reach these governor limits.
Which data structure is returned to a developer when performing a SOSL search?
A list of lists of sObjects
A developer runs the following anonymous code block:
List acc = [SELECT Id FROM Account LIMIT 10];
Delete acc;
Database.emptyRecycleBin(acc); system.debug(Limits.getDMLStatements()+’,’ +Limits.getLimitDMLStatements());
What is the result?
2,150
A developer has the following query:
Contact c = [SELECT id, firstname, lastname, email FROM Contact WHERE lastname = ‘Smith’];
What does the query return if there is no contact with the last name Smith?
An error that no rows are found.
A developer writes a Salesforce Object Query Language (SOQL) query to find child records for a specific parent. How many levels can be returned in a single query?
1
What is an accurate constructor for a custom controller named MyController?
A.
public MyController(List objects){
accounts = (List)objects;
}
B. public MyController(){ account = new Account(); }
C.
public MyController(SObject obj){
account = (Account) obj;
}
D.
public MyController(List objects){
accounts = (List)objects;
}
B. public MyController(){ account = new Account(); }
To create a constructor for a custom controller, the constructor cannot have a parameter.
A developer uses a before insert trigger on the Lead object to fetch the Territory__c object, where the Territory__c.PostalCode__c matches the Lead.PostalCode. The code fails when the developer uses the Apex Data Loader to insert 10,000 lead records. The developer has the following code block:
01 for(Lead l : Trigger.new){ 02 if(l.PostalCode != null){ 03 List terrList = [SELECT Id FROM Territory\_\_c Where PostalCode\_\_c = :l.PostalCode]; 04 if(terrList.size() > 0){ 05 l.Territory\_\_c = terrList[0].Id; 06 } 07 } 08 }
Which line of code causes the code block to fail?
A. 01: Trigger.new is not valid in a before insert trigger.
B. 02: A NullPointer exception is thrown if PostalCode is null.
C. 03: A SOQL query is located inside of the for loop code.
D. 05: The lead in a before insert trigger cannot be updated.
C. 03: A SOQL query is located inside of the for loop code.
This will fail on the record number 201 because the limit of SOQL queries is 200. Best practice is to avoid SOQL queries inside for loops.
A developer needs to automatically populate the ReportsTo field in a contact record based on the values of the related Account and Department fields in the contact record. Which two trigger types should the developer create? (Select two answers.)
A. before update
B. before insert
C. after insert
D. after update
A. before update
B. before insert
The developer should use a before update/insert trigger. That way there is no need for an extra DML statement. It is good practice to use before triggers to validate updated/inserted records before they are saved.
A hierarchy custom setting stores a specific URL for each profile in Salesforce. Which statement can a developer use to retrieve the correct URL for the current user’s profile and display this on a Visualforce Page?
A. {!$Setup.Url_Settings__c.URL__c}
B. {!$Setup.Url_Settings__c.Instance[Profile.Id].URL__c}
C. {!$Setup.Url_Settings__c[Profile.Id].URL__c}
D. {!$Setup.Url_Settings__c[$Profile.Id].URL__c}
A. {!$Setup.Url_Settings__c.URL__c}
A specific profile should get a specific record type. So, simply getting a record type Id will not work. We need to know what each profile should get. Hierarchy custom settings support unique values per profile.