930 likes | 934 Vues
Development Tools. Developer Console Force.com IDE (Eclipse Plugin) Mavens Mate (Sublime Plugin) Force CLI. Developer Console. Browser Based IDE Create Classes, Triggers, Pages Execute Apex Anonymously Execute SOQL Queries Run Unit Tests Review Debug Logs. Lab 3: Creating an Apex Class.
E N D
Development Tools • Developer Console • Force.com IDE (Eclipse Plugin) • Mavens Mate (Sublime Plugin) • Force CLI
Developer Console • Browser Based IDE • Create Classes, Triggers, Pages • Execute Apex Anonymously • Execute SOQL Queries • Run Unit Tests • Review Debug Logs
Lab 3: Creating an Apex Class • Create the EmailManager class • Send emails from the developer console
What’s SOQL? • Salesforce Object Query language • Similar to SQL • Streamlined syntax to traverse object relationships • Built into Apex
SELECT Id, Name, Phone FROM Contact
SELECT Id, Name, Phone FROM Contact WHERE Phone <> null
SELECT Id, Name, Phone FROM Contact WHERE Phone <> null AND Name LIKE '%rose%'
SELECT Id, Name, Phone FROM Contact WHERE Phone <> null AND Name LIKE '%rose%' ORDER BY Name
SELECT Id, Name, Phone FROM Contact WHERE Phone <> null AND Name LIKE '%rose%' ORDER BY Name LIMIT 50
Details to Master SELECT Id, Name, Phone, Account.Name FROM Contact WHERE Phone <> null AND Name LIKE '%rose%' ORDER BY Name LIMIT 50
Details to Master SELECT Name, (SELECT FirstName, LastName, Phone FROM Contacts) FROM Account
Inlining SOQL in Apex Integer i = [SELECT Count() FROM Session__c];
Inlining SOQL in Apex String level = 'Advanced'; List<Session__c> sessions = [SELECT Name, Level__c FROM Session__c WHERE Level__c = :level];
Inlining SOQL in Apex List<String> levels = new List<String>(); levels.add('Intermediate'); levels.add('Advanced'); List<Session__c> sessions = [SELECT Name, Level__c FROM Session__c WHERE Level__c IN :levels];
Inlining SOQL in Apex for (Speaker__c s : [SELECT Email__c FROM Speaker__c]) { System.debug(s.email__c); }
What’s DML? • Data Manipulation Language • Language used to create, update, delete records
insert Session__c session = new Session__c(); session.name = 'Apex 101'; session.level__c = 'Beginner'; insert session;
insert Session__c session = new Session__c( name = 'Apex 201', level__c = 'Intermediate' ); insert session;
update String oldName = 'Apex 101'; String newName = 'Apex for Beginners'; Session__c session = [SELECT Id, Name FROM Session__c WHERE Name=:oldName]; session.name = newName; update session;
delete String name = 'Testing 501'; Session__c session = [SELECT Name FROM Session__c WHERE Name=:name]; delete session;
Lab 4: Accessing Data using SOQL and DML • Execute SOQL statements in the Query Editor • Execute DML statements in the Anonymous Window
What’s a Trigger? • Apex code executed on database events • Before or after: • Insert • Update • Delete • Undelete
Before or After? • Before • Update or validate values before they are saved to the database • Example: Prevent double-booking of a speaker • After • Access values set by the database (Id, lastUpdated, …) • Example: Send speaker confirmation email
Bulk Mode • Triggers work on lists of records, not single records • This is to support bulk operations • Data Import, Bulk API, etc. • Context variables provide access to old and new values: • Trigger.old and Trigger.new (List) • Trigger.oldMap and Trigger.newMap (Map)
Example 1 trigger WelcomeKit on Account (after insert) { List<Case> cases = new List<Case>(); for (Account account : Trigger.new) { Case case = new Case(); case.Subject = 'Mail Welcome Kit'; case.Account.Id = account.Id; cases.add(case); } insert cases; }
Example 2 trigger on Account (before update) { for (Account acc: Trigger.New) { // Compare new value with old value if (acc.Rating != Trigger.oldMap.get(acc.Id).Rating) { // Your Logic } } }
Lab 5: Writing Triggers • Write the SendConfirmationEmail trigger • Write the RejectDoubleBooking trigger
What's a Visualforce Page? • HTML page with tags executed at the server-side to generate dynamic content • Similar to JSP and ASP • Can leverage JavaScript and CSS libraries • The View in MVC architecture
Model-View-Controller • Separation of concerns • No data access code in view • No view code in controller • Benefits • Minimize impact of changes • More reusable components View UI code Controller View-Model interactions Model Data + Rules
Model-View-Controller in Salesforce • View • Standard Pages • Visualforce Pages • External apps • Controller • Standard Controllers • Controller Extensions • Custom Controllers • Model • Objects • Triggers (Apex) • Classes (Apex)
Component Library • Presentation tags • <apex:pageBlock title="My Account Contacts"> • Fine grained data tags • <apex:outputField value="{!contact.firstname}"> • <apex:inputField value="{!contact.firstname}"> • Coarse grained data tags • <apex:detail> • <apex:pageBlockTable> • Action tags • <apex:commandButton action="{!save}" >
Expression Language • Anything inside of {! }is evaluated as an expression • Same expression language as Formulas • $ provides access to global variables (User, RemoteAction, Resource, …) • {! $User.FirstName } {! $User.LastName }
Example 1 <apex:page> <h1>Hello, {!$User.FirstName}</h1> </apex:page>
Standard Controller • A standard controller is available for all objects • You don't have to write it! • Provides standard CRUD operations • Create, Update, Delete, Field Access, etc. • Can be extended with more capabilities (next module) • Uses id query string parameter in URL to access object
Example 2 Standard controller object <apex:pagestandardController="Contact"> <apex:form> <apex:inputField value="{!contact.firstname}"/> <apex:inputField value="{!contact.lastname}"/> <apex:commandButton action="{!save}" value="Save"/> </apex:form> </apex:page> Function in standard controller
Where can I use Visualforce? Email Templates Embedded in Page Layouts Generate PDFs Custom Tabs Page Overrides Mobile Interfaces
Lab 6: Writing Visualforce Pages • Write the SpeakerForm Visualforce page • Set it as default for creating and editing speakers
Module 7:Writing Controller Extensions and Custom Controllers
What's a Controller Extension? • Custom class written in Apex • Works on the same object as the standard controller • Can override standard controller behavior • Can add new capabilities
Defining a Controller Extension Provides basic CRUD <apex:page standardController="Speaker__c" extensions="SpeakerCtrlExt"> Overrides standard actions and/or provide additional capabilities
Anatomy of a Controller Extension public class SpeakerCtrlExt { private final Speaker__c speaker; private ApexPages.StandardControllerstdController; public SpeakerCtrlExt (ApexPages.StandardController ctrl) { this.stdController = ctrl; this.speaker = (Speaker__c)ctrl.getRecord(); } // method overrides // custom methods }
What's a Custom Controller? • Custom class written in Apex • Doesn't work on a specific object • Provides custom data • Provides custom behaviors
Defining a Custom Controller <apex:page controller="FlickrController">
Custom Controller Example public with sharing class FlickrController { public FlickrListgetPictures() { HttpRequestreq = new HttpRequest(); req.setMethod('GET'); req.setEndpoint('http://api.flickr.com/services/feeds/'); HTTP http = new HTTP(); HTTPResponse res = http.send(req); return (FlickrList) JSON.deserialize(res.getBody(), FlickrList.class); } }
Lab 7: Writing a Controller Extension • Write a Controller Extension that supports Picture Upload