77
Track: Developers #CNX14 #CNX14 Intro to Force.com James Ward Platform Evangelist @_JamesWard

#CNX14 - Intro to Force

Embed Size (px)

DESCRIPTION

Force.com can automatically generate user interfaces, but in some cases you might want to build a more custom UI. Join us to learn about Visualforce, the component-based UI framework that lets you build attractive, dynamic, reusable user interfaces. We'll cover code walk-throughs, common use cases, leveraging Apex on the server side, debugging techniques, and how to utilize the component framework to make your code portable and maintainable.

Citation preview

Page 1: #CNX14 - Intro to Force

Track: Developers

#CNX14

#CNX14

Intro to Force.com

James Ward

Platform Evangelist

@_JamesWard

Page 2: #CNX14 - Intro to Force

Track: Developers

#CNX14

Salesforce1 Platform APIs

Salesforce1 App

Force.com HerokuExactTarget

Fuel

Sales Cloud

Service Cloud

Marketing Cloud

AppExchange

Custom Employee Apps

Customer & Partner Apps

Salesforce1 Platform Services

Page 3: #CNX14 - Intro to Force

Track: Developers

#CNX14

IdeaBuild App

Idea

buy & setup

hardware

install complex software

define user access

build & test security

make it mobile &

social

setup reporting & analytics

build app

Traditional Platforms

6-12 Months?

App

App

Page 4: #CNX14 - Intro to Force

Track: Developers

#CNX14

Platform Services

The Fastest Path From Idea To App

Page 5: #CNX14 - Intro to Force

Track: Developers

#CNX14

Salesforce: #1 Enterprise Cloud Platform

Market Share#1Enterprise Platform#1

Magic Quadrant for Application Platform as a Service

January, 2014 Analyst: Yefim V. Natis This graphic was published by Gartner, Inc. as part of a larger research document and should be evaluated in the context of the entire document. The Gartner document is available upon request from Salesforce.com. Gartner does not endorse any vendor, product or service depicted in its research publications, and does not advise technology users to select only those vendors with the highest ratings. Gartner research publications consist of the opinions of Gartner's research organization and should not be construed as statements of fact. Gartner disclaims all warranties, expressed or implied, with respect to this research, including any warranties of merchantability or fitness for a particular purpose.

Page 6: #CNX14 - Intro to Force

Track: Developers

#CNX14

Visualforce PagesVisualforce Components

Apex ControllersApex Triggers

Metadata APIREST APIBulk API

Formula FieldsValidation Rules

Workflows and Approvals

Custom ObjectsCustom FieldsRelationships

Page LayoutsRecord Types

User Interface

Business Logic

Data Model

Declarative Approach Programmatic Approach

Page 7: #CNX14 - Intro to Force

Track: Developers

#CNX14

Free Developer Environment

http://developer.salesforce.com/signup

Page 8: #CNX14 - Intro to Force

Declarative Data Model

Page 9: #CNX14 - Intro to Force

Track: Developers

#CNX14

Salesforce Objects

• Similar to Tables (with more metadata)• Standard objects out-of-the-box

• Account, Contact, Opportunity, …• You can add custom fields to standard objects

• Rating__c, Twitter__c, …• You can create custom objects

• i.e. Speaker__c, Session__c, Hotel__c• Custom objects have standard fields

• Id, Owner, LastModifiedDate, LastModifiedBy, …

Page 10: #CNX14 - Intro to Force

Track: Developers

#CNX14

Rich Data Types

• Auto Number• Formula• Roll-Up Summary• Lookup• Master-Detail • Checkbox• Currency• Date

• Picklist (multi select)• Text• Text Area• Text Area (Long)• Text Area (Rich)• Text (Encrypted)• URL

Date/Time

Email

Geolocation

Number

Percent

Phone

Picklist

Page 11: #CNX14 - Intro to Force

Track: Developers

#CNX14

Modeling One-to-Many Relationships

An expense has one expense report

An expense report has many expenses

Page 12: #CNX14 - Intro to Force

Track: Developers

#CNX14

Modeling Many-to-Many Relationships

A speaker can have many session assignments

A session can have many speaker assignments

Page 13: #CNX14 - Intro to Force

Track: Developers

#CNX14

Id

• All objects are given an Id field• Globally unique Id is assigned at record creation• "Primary key" used to access records

Page 14: #CNX14 - Intro to Force

Track: Developers

#CNX14

Record Name

• Human readable / logical identifier• Text or Auto Number ("Intro to Apex" or SP-00002)• Uniqueness not enforced

Page 15: #CNX14 - Intro to Force

Track: Developers

#CNX14

When you create an Object, you get…

• A CRUD user interface• Instant Mobile App access (Salesforce1)• A REST API• Rich Metadata• Indexed search

Page 16: #CNX14 - Intro to Force

Declarative Apps

Page 17: #CNX14 - Intro to Force

Track: Developers

#CNX14

What's an Application?

• Group of tabs that provide easy access to related features• Salesforce comes with standards apps

• Sales, Call Center, Marketing, …• You can create your own apps• Tabs can be:

• Object pages, Visualforce pages, Canvas app

Page 18: #CNX14 - Intro to Force

Track: Developers

#CNX14

Page LayoutsLet you customize all aspects of the layout, related lists, …

Page 19: #CNX14 - Intro to Force

Programming with Apex

Page 20: #CNX14 - Intro to Force

Track: Developers

#CNX14

What is Apex?

• Salesforce platform language• Cloud based compiling, debugging and unit testing• Object-oriented• Strongly typed• Classes and Interfaces• Similar to Java

Page 21: #CNX14 - Intro to Force

Track: Developers

#CNX14

Apex and Java

Same• Primitive data types • Flow control (if, for, while, …)• Exception handling• Collections: Lists, Sets, …

Different• Case insensitive• Single quote strings: 'Joe'• Id data type• Built-in support for data access

Page 22: #CNX14 - Intro to Force

Track: Developers

#CNX14

Apex Class

public class MortgageCalculator { public Double amount { get; set; } public Double rate { get; set; } public Integer years { get; set; } public Double calculateMonthlyPayment() { Integer months = years * 12; Double monthlyRate = rate / (12 * 100); return amount * (monthlyRate/ (1 - Math.pow(1 + monthlyRate, -months))); }}

Page 23: #CNX14 - Intro to Force

Track: Developers

#CNX14

Development Tools

• Developer Console• Force.com IDE (Eclipse Plugin)• Mavens Mate (Sublime Plugin)• Force CLI

Page 24: #CNX14 - Intro to Force

Track: Developers

#CNX14

Developer Console

• Browser Based IDE• Create Classes, Triggers, Pages• Execute Apex Anonymously• Execute SOQL Queries• Run Unit Tests• Review Debug Logs

Page 25: #CNX14 - Intro to Force

Data Access with SOQL and DML

Page 26: #CNX14 - Intro to Force

Track: Developers

#CNX14

SOQL

• Salesforce Object Query language• Similar to SQL• Streamlined syntax to traverse object relationships• Built into Apex

Page 27: #CNX14 - Intro to Force

Track: Developers

#CNX14

SELECT Id, Name, Phone FROM Contact

Page 28: #CNX14 - Intro to Force

Track: Developers

#CNX14

SELECT Id, Name, Phone FROM Contact WHERE Phone <> null

Page 29: #CNX14 - Intro to Force

Track: Developers

#CNX14

SELECT Id, Name, Phone FROM Contact WHERE Phone <> null AND Name LIKE '%rose%'

Page 30: #CNX14 - Intro to Force

Track: Developers

#CNX14

SELECT Id, Name, Phone FROM Contact WHERE Phone <> null AND Name LIKE '%rose%' ORDER BY Name

Page 31: #CNX14 - Intro to Force

Track: Developers

#CNX14

SELECT Id, Name, Phone, Account.Name FROM Contact WHERE Phone <> null AND Name LIKE '%rose%' ORDER BY Name

Page 32: #CNX14 - Intro to Force

Track: Developers

#CNX14

SELECT Id, Name, Phone, Account.Name FROM Contact WHERE Phone <> null AND Name LIKE '%rose%' ORDER BY Name LIMIT 50

Page 33: #CNX14 - Intro to Force

Track: Developers

#CNX14

Executing SOQL in the Developer Console

Page 34: #CNX14 - Intro to Force

Track: Developers

#CNX14

Inlining SOQL in Apex

Integer i = [select count() from Session__c];

Page 35: #CNX14 - Intro to Force

Track: Developers

#CNX14

Inlining SOQL in Apex

String level = 'Advanced';List<Session__c> sessions = [SELECT Name, Level__c FROM Session__c WHERE Level__c = :level];

Page 36: #CNX14 - Intro to Force

Track: Developers

#CNX14

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];

Page 37: #CNX14 - Intro to Force

Track: Developers

#CNX14

Inlining SOQL in Apex

for (Speaker__c s : [select email__c from Speaker__c]) { System.debug(s.email__c);}

Page 38: #CNX14 - Intro to Force

Track: Developers

#CNX14

insertSession__c session = new Session__c();session.name = 'Apex 101';session.level__c = 'Beginner';insert session;

Page 39: #CNX14 - Intro to Force

Track: Developers

#CNX14

insertSession__c session = new Session__c( name = 'Apex 201', level__c = 'Intermediate');insert session;

Page 40: #CNX14 - Intro to Force

Track: Developers

#CNX14

updateString 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;

Page 41: #CNX14 - Intro to Force

Track: Developers

#CNX14

deleteString name = 'Testing 501';Session__c session = SELECT Name FROM Session__c WHERE Name=:name];delete session;

Page 42: #CNX14 - Intro to Force

Apex Triggers

Page 43: #CNX14 - Intro to Force

Track: Developers

#CNX14

Trigger

• Apex code executed on database events • Before or after:

• Insert• Update• Delete• Undelete

Page 44: #CNX14 - Intro to Force

Track: Developers

#CNX14

Before or After?

• Before• Update or validate values before they are saved to the database• Example: Prevent double-booking of a speaker

• After• Need access to values set by the database (Id, lastUpdated, …)• Example: Send speaker confirmation email

Page 45: #CNX14 - Intro to Force

Track: Developers

#CNX14

Bulk Mode

• Trigger API designed to support bulk operations• Data Import, Bulk API, etc.

• Triggers work on bulk of records, not single records• Context variables provide access to data:

• Trigger.old and Trigger.new (List) • Trigger.oldMap and Trigger.newMap (Map)

Page 46: #CNX14 - Intro to Force

Track: Developers

#CNX14

ExampleTrigger on Account (after insert) { for (Account account : Trigger.new) { Case case = new Case(); case.Subject = 'Mail Welcome Kit'; case.Account.Id = account.Id; insert case; }}

Page 47: #CNX14 - Intro to Force

Visualforce Pages

Page 48: #CNX14 - Intro to Force

Track: Developers

#CNX14

Model-View-Controller

ModelData + Rules

ControllerView-Modelinteractions

ViewUI code

Separation of concerns– No data access code in view

– No view code in controller

Benefits– Minimize impact of changes

– More reusable components

Page 49: #CNX14 - Intro to Force

Track: Developers

#CNX14

Model-View-Controller in Salesforce

View• Metadata• Standard Pages• Visualforce Pages• External apps

Controller• Standard

Controllers• Controller

Extensions• Custom

Controllers(all Apex)

Model• Metadata• Objects• Triggers (Apex)• Classes (Apex)

Page 50: #CNX14 - Intro to Force

Track: Developers

#CNX14

What's a Visualforce Page?

• The View in MVC architecture• HTML page with tags executed at the server-side to generate dynamic content• Similar to JSP and ASP• Can leverage JavaScript and CSS libraries

Page 51: #CNX14 - Intro to Force

Track: Developers

#CNX14

Expression Language

• Anything inside of {! } is evaluated as an expression• Same expression language as Formulas• Same global variables are available. For example:

• {!$User.FirstName} {!$User.LastName}

Page 52: #CNX14 - Intro to Force

Track: Developers

#CNX14

Example 1<apex:page> <h1>Hello, {!$User.FirstName}</h1></apex:page>

Page 53: #CNX14 - Intro to Force

Track: Developers

#CNX14

Example 2<apex:page standardController="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

Standard controller

object

Page 54: #CNX14 - Intro to Force

Track: Developers

#CNX14

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• Uses id in URL to access object

Page 55: #CNX14 - Intro to Force

Track: Developers

#CNX14

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}" >

Page 56: #CNX14 - Intro to Force

Track: Developers

#CNX14

Email Templates

Embedded in Page Layouts

Generate PDFs

Custom Tabs

Mobile Interfaces

Page Overrides

Where can I use Visualforce?

Page 57: #CNX14 - Intro to Force

Apex Controller Extensions

Page 58: #CNX14 - Intro to Force

Track: Developers

#CNX14

Custom Controllers

• Custom class written in Apex• Can override standard behavior• Can add new capabilities

Page 59: #CNX14 - Intro to Force

Track: Developers

#CNX14

Defining a Controller Extension

<apex:page standardController="Speaker__c" extensions="SpeakerCtrlExt">

Provides basic CRUD

Overrides standard actions and/or provide additional capabilities

Page 60: #CNX14 - Intro to Force

Track: Developers

#CNX14

Anatomy of a Controller Extension

public class SpeakerCtrlExt {

private final Speaker__c speaker; private ApexPages.StandardController stdController;

public SpeakerCtrlExt (ApexPages.StandardController ctrl) { this.stdController = ctrl; this.speaker = (Speaker__c)ctrl.getRecord(); } // method overrides // custom methods}

Page 61: #CNX14 - Intro to Force

JavaScript in Visualforce

Page 62: #CNX14 - Intro to Force

Track: Developers

#CNX14

Why JavaScript?

• Build Engaging User Experiences• Leverage JavaScript Libraries• Build Custom Applications

Page 63: #CNX14 - Intro to Force

Track: Developers

#CNX14

JavaScript in Visualforce Pages

JavaScript RemotingRemote Objects

(REST)

Visualforce Page

Page 64: #CNX14 - Intro to Force

Track: Developers

#CNX14

Examples

Page 65: #CNX14 - Intro to Force

Track: Developers

#CNX14

Using JavaScript and CSS Libraries

• Hosted elsewhere

<script src="https://maps.googleapis.com/maps/api/js"></script>• Hosted in Salesforce

• Upload individual file or Zip file as Static Resource• Reference asset using special tags

Page 66: #CNX14 - Intro to Force

Track: Developers

#CNX14

Referencing a Static Resource// Single file<apex:includeScript value="{!$Resource.jquery}"/><apex:image url="{!$Resource.myLogo}"/>

// ZIP file<apex:includeScript value="{!URLFOR($Resource.MyApp, 'js/app.js')}"/><apex:image url="{!URLFOR($Resource.MyApp, 'img/logo.jpg')}/><link href="{!URLFOR($Resource.bootstrap, 'bootstrap/css/bootstrap.css')}" rel="stylesheet"/>

Page 67: #CNX14 - Intro to Force

Track: Developers

#CNX14

JavaScript Remoting - Server-Sideglobal with sharing class HotelRemoter {

@RemoteAction global static List<Hotel__c> findAll() { return [SELECT Id,

Name, Location__Latitude__s, Location__Longitude__s

FROM Hotel__c]; }

}

Page 68: #CNX14 - Intro to Force

Track: Developers

#CNX14

"global with sharing"?

• global• Available from outside of the application

• with sharing• Run code with current user permissions. (Apex code runs in system context

by default -- with access to all objects and fields)

Page 69: #CNX14 - Intro to Force

Track: Developers

#CNX14

JavaScript Remoting - Visualforce Page<script>Visualforce.remoting.Manager.invokeAction( '{!$RemoteAction.HotelRemoter.findAll}', function (result, event) { if (event.status) { for (var i = 0; i < result.length; i++) {

var lat = result[i].Location__Latitude__s; var lng = result[i].Location__Longitude__s; addMarker(lat, lng); } } else { alert(event.message); } });</script>

Page 70: #CNX14 - Intro to Force

REST APIs

Page 71: #CNX14 - Intro to Force

Track: Developers

#CNX14

When?

• Get Salesforce data from outside Salesforce• Integrate Salesforce in existing apps• Build consumer apps• Device integration (Internet of Things)

Page 72: #CNX14 - Intro to Force

Track: Developers

#CNX14

Mobile SDK Example

REST APIs

OAuth

Page 73: #CNX14 - Intro to Force

Track: Developers

#CNX14

Libraries

• ForceTK• Salesforce REST API Toolkit

• Nforce• node.js a REST API wrapper

• ngForce• Visualforce Remoting integration in AngularJS

• JSForce

Page 74: #CNX14 - Intro to Force

Track: Developers

#CNX14

Track: Developers

#CNX14

Take the after-session survey!

Take the Survey in the Connections 2014 Mobile App

Join the Conversation!

#CNX14

$50Starbucks

Gift Card

Page 75: #CNX14 - Intro to Force

Track: Developers

#CNX14

Page 76: #CNX14 - Intro to Force

Track: Developers

#CNX14

Track: Developers

#CNX14

Questions?

Page 77: #CNX14 - Intro to Force

Track: Developers

#CNX14

CUSTOMER JOURNEY SHOWCASE

MARKETING THOUGHT LEADERS

EMAIL MARKETING PRODUCT STRATEGY& ROADMAP

PERSONAL TRANSFORMATION

& GROWTH

SOCIAL MARKETING MOBILE & WEB MARKETING

DEVELOPERS HANDS-ON TRAINING

INDUSTRY TRENDSETTERS

CREATIVITY & INNOVATION

SALESFORCE FOR MARKETERS

ROUNDTABLES