470 likes | 484 Views
Join @archladies.com developer certification study group week 4 led by Blanca V. Leon-Carter. Learn Logic & Process Automation part 2, Visualforce controllers, MVC development, control structures, loops, and queries in Salesforce.
E N D
@archladiesarchladies.com PLATFORM DEVELOPER ICertification Study Group Week 4Led by: Blanca V. Leon-Carter bvleoncarter@gmail.com #LBAjourneytoPD1
Guest Presenter • Development Manager & Solution Architect • 2x Salesforce Certified • Former SQL Developer Study Group MemberKelsey Shannon@kelseypshannon
Week 4 Objectives Logic & Process Automation {part 2} • Describe how to programmatically access and utilize the object schema. • Describe how to use and apply Apex control flow statements • Describe how to write and when to use Apex classes and interfaces • Describe how to use basic SOSL, SOQL, and DML statements when working with objects in Apex • Describe the basic patterns used in triggers and classes to process data efficiently • Describe when to use and how to write triggers • Describe the relationship between Apex transactions, the save order of execution, and the potential for recursion and/or cascading • Describe how to implement exception handling in Apex • Describe how to write Visualforce controllers • Describe when and how to use standard Visualforce controllers vs Apex custom controllers and controller extensions • Describe the programmatic techniques to prevent security vulnerabilities in Apex and Visualforce • Describe how Apex impacts the ability to make declarative changes
MVC Development • Model View Controller (MVC) is a software architecture pattern • Model - database schema & objects • Objects, Fields, and Relationships • View - how schema is presented • Visualforce pages, page layouts, tabs • Controller - provide instructions on how the interface should act • Workflows, Apex Classes, Triggers Source: Model View Controller
Defining an Apex Class • Outer Class structure: • Access modifier + Sharing Mode + <class> + Class Name + Interfaces + Extensions Source: Apex Class Definition
Adding a Method • Method structure: • Access modifier + Data Type + Method Name + Parameters Source: Apex Class Definition
Collections • Lists (Arrays) - Ordered collection of elements • Set - Unordered collection of elements that do not contain duplicates • Maps - Collection of key-value pairs • Use when you need identifiers or keys like IDs and Names Source: Salesforce Collections
Control Structure • Three ways to control the flow of code: • IF/ELSE statements • Switch Statements • Loops
Control Structure - IF ELSE • IF - Executes if a boolean statement evaluates to true • IF/ELSE - Executes the ELSE portion if first condition is false, groups with the closest IF • ELSE IF - Navigates through until one statement evaluates to true Source: SF99 IF Statements
Control Structure - Quiz! • If a cat weighs 15 pounds, how much food will they get based on the logic below?
Control Structure - Loops • 3 different procedural loops • For Loops • Do - While Loops • While Loops Source: Salesforce, Loops
For Loops • Different types: • Traditional • List or Set Iteration • SOQL Source: Salesforce, Loops
Do-While Loops vs While Loops • Do-While: • Always executes the first statement at least once • Next continues to check the Boolean statement then executes Do again • While: • Only executes if the “while” condition is true Source: Salesforce, Loops
For Loop - Order of Operations • Execute the init_stmt • Check the exit_condition. If true, continue on. • Execute the code_block • Execute the increment_statement • Return to step 2 Source: Salesforce, Loops
Loops - Quiz! • What is the correct syntax to write a For Loop? Choose 2 answers • for (init_stmt; increment_stmt, exit_condition) { code_block} • for (variable : [soql_query]) {code_block} • for (variable: list_or_set) {code_block} • for (init_stmt, exit_condition) {code_block, increment_stmt}
Loops - Quiz! • What will be the final value of x when the loop finishes? • 5 • 6 • 7 • 8
Queries • SOQL - Used to construct queries that returns one or more fields from one or more objects • SOSL - Uses a text string to search across multiple objects Source: Salesforce SOQL and SOSL Queries
Queries - Quiz! • A developer needs to query all dogs at the shelter who are less than 5 years old and haven’t been adopted yet. Which statement below holds the correct syntax? • SELECT Name, Age__c, Species__c FROM Pets__c WHERE IsAdopted__c = FALSE and Age__c < 5 and Species__c = ‘Dog’ • SELECT Name, Age__c, Species__c FROM Pets__c WHERE IsAdopted__c = “FALSE” and Age__c < 5 and Species__c = “Dog” • SELECT Name, Age, Species FROM Pets WHERE IsAdopted__c = FALSE and Age__c < 5 and Species__c = “Dog”
Data Manipulation Language (DML) • Operations include: • Insert, Upsert, Update, Merge, Delete, Undelete • DML Statements • If an operation fails and exception is thrown and all changes rolled back • Database class methods • Allows for partial inserts if some records fail Source: Salesforce, Data Manipulation Language
Triggers • Allow you to perform custom actions on Salesforce records when certain criteria are “triggered” • Contain references to Apex Classes • Two types of triggers: • BEFORE - insert/update/delete/merge/upsert/undelete • Use to modify fields before the record is saved to the database • AFTER - insert/update/delete/merge/upsert/undelete • Use to access system set values like Id or LastModifiedDate • Records that fire after triggers are read only Source: Salesforce Triggers
Triggers • Trigger syntax: • trigger <TriggerName> on <ObjectName> (trigger_events){code_block}; Source: Salesforce Triggers
Triggers • Context variables allow you to access run-time variables • Includes items such as: • isInsert, isUpdate, isDelete, isBefore, isAfter, new, old, size • Allows you to write triggers more efficiently, examples: • In a before update trigger, access both the previous and current value being updated using ‘new’ and ‘old’ • Combine all object operations into one object and call Apex Classes using isBefore, isAfter (best practice - called a Trigger Handler) Source: Salesforce Triggers
Trigger Handler Example Source: Salesforce Trigger Framework and Apex Trigger Best Practices
Triggers - Quiz! • A developer is required to create a trigger that prevents users from adding a duplicate product onto an Opportunity. What trigger event should they use? • before Merge • after Insert • after Update • before Insert
Triggers - Quiz! • Will the below code succeed or throw an error?
Governor Limits • Why are they necessary? • Multi-tenant environment • Types of limits per transaction (not inclusive): • 100 SOQL Queries issued • 50,000 records retrieved from SOQL Queries • 20 SOSL Queries issued • 150 DML Statements • 10,000 milliseconds CPU time
How to Avoid Hitting Governor Limits • Never place queries inside loops • Never place DML statements inside loops • Only use 1 trigger per object and do not place logic directly inside the trigger • Watch out for code that triggers other processes • Bulkify your code - make sure it can handle more than one record at a time Inside Outside Source: Salesforce The 15 Apex Commandments
Visualforce • A coding language used to alter the user interface of Salesforce
Visualforce Controllers • Controller - A set of instructions on what happens when a user interacts with a page, types: • Standard Controller - same functionality and logic as standard Salesforce pages • Standard List Controller - enables you to create pages that display or act on a set of records • Custom Controller - Apex class that adds implements all of the page’s logic without leveraging the standard controller • Controller extension - Apex Class that extends the functionality of standard or customer controllers Source: Salesforce, What is Visualforce?
Visualforce Controllers • Permissions: • Standard controllers execute in user mode • Custom controllers execute in system mode • Syntax: Source: Salesforce, What is Visualforce?
Visualforce Methods • Methods are used in Visualforce to GET or SET values • Getter Methods - Included in every standard controller and can be defined in custom controllers • Always required to access values from a controller • Setter Methods - used to submit values from the Visualforce page back to the controller • Not always required to pass values if the Visualforce page is bound to an sObject • Syntax: {! expression_name} Source: Salesforce, What is Visualforce?
Visualforce - Quiz! • A developer needs to create a Visualforce page that overrides the standard permissions. What controller do they need to accomplish this? • Standard List Controller • Custom Controller • Controller Extension • Setter Method
Visualforce - Quiz! • What is the correct syntax for referencing a custom controller? • <apex:page standardController=”customControllerName” type=”custom”> • <apex:page controller=”customControllerName”> • <apex:page customController = “customControllerName”> • <apex pageBlock customController = “customControllerName”>
Dynamic Apex • Describe how to programmatically access and utilize the object schema.
Dynamic Apex • Allows you to access behind the scenes details (metadata) of Objects and Fields like: • Object/Field API and label names • Field description details • Security settings for the user who kicks off the code • Valid record types and picklist values • Max number of digits allowed in a field Source: Cap Tech Consulting, How to PASS Salesforce Platform Developer Exam
Dynamic Apex • WHY? • Makes code more flexible • Ex: Can use the same piece of code on multiple objects • Not all developers have access to the target environment • Ex: Apps downloaded from the AppExchange must work for any environment • Allows you to access information not easy to query: • Ex: The permissions a user has on an object, return all fields on an object Source: Cap Tech Consulting, How to PASS Salesforce Platform Developer Exam
Dynamic Apex • Schema Namespace - Contains a set of pre-defined classes and methods in Salesforce that allow you to access metadata • A namespace is identified using dot notation, example: Schema.Reports • Some examples: • DescribeFieldResult Class - Contains methods for describing sObject fields • DescribeSObjectResult Class - Contains methods for describing sObjects • DescribeTabResult Class - Contains tab metadata information in a standard or custom app Image Source: FocusOnForce.com - PD1 Study Guide
Dynamic Apex • Example of commonly used methods in the Schema Namespace • From the DescribeFieldResult Class: • getDefaultValue(), getLabel(), getPicklistValues() • From the DescribeSObjectResult Class • isAccessible(), isCreatable(), isDeletable() • *These three methods describe the current user’s permissions on the current object Image Source: FocusOnForce.com - PD1 Study Guide
Dynamic Apex - Homework! • Be familiar with the syntax of the different ways to describe metadata. Open up the below links and read the definitions • Schema Namespace - Get familiar with names of classes. • Get familiar with the names of the methods in these classes: • DescribeSObjectResult Class • DescribeFieldResult Class
Dynamic Apex - Question Examples How can a developer check that a user can create records on the Visualforce page they are accessing? • Call the isCreatable() method of Schema.DescribeSObjectResult to verify • Call the isInsertable() method of Schema.DescribeFieldResult to verify • Call the isCreateable() method of Schema.DescribeSObject to verify • Call the isAccessible() method of Schema.DescribeSObjectResult to verify
Dynamic Apex - Question Examples What three methods below can be accessed through the DescribeFieldResult class? • getName() • getLabel() • isCustom() • isStandard()
Order of Operations • System validation rules • Before triggers • System validation rules + custom validation rules • Duplicate rules • Record saved (not committed) • After triggers • Assignment rules • Auto-response rules • Workflow rules • Before triggers/validation rules/after triggers executed from workflow updates • Processes • Escalation rules • Rollup summary fields and cross-object formula fields • Parent & grandparent records saved • Criteria-based sharing rules are evaluated • DML operations • Post-commit logic
Topics not Covered: • Potential for recursion and cascading • Field updates that Re-Evaluate Workflow Rules • Avoid Recursive Triggers • Context Variable Considerations • Describe how to implement exception handling in Apex • Introduction to Exception Handling • Built in Exceptions and Common Methods • Create Custom Exceptions • Describe the programmatic techniques to prevent security vulnerabilities in Apex and Visualforce • Security Tips for Apex and Visualforce Development • Data Access Control • Trailhead: App Logic Vulnerability Prevention • Trailhead: Injection Vulnerability Prevention • Describe how Apex impacts the ability to make declarative changes • Notes on Changing Custom Field Types • Visual Development - When to Click Instead of Write Code Source: Focus on Force PD1 Study Guide
Platform Developer I Certification Study Group Got questions?Unmute your line to speak or type into the chat box.
Platform Developer I Certification Study Group *Schedule: June 28 – September 6, 2018 *Schedule was updated on 7/18/2018