Coding Apps in the Cloud with Force.com - Part I

52
#forcewebi nar Coding Apps in the Cloud with Force.com - Part I January 28, 2016

Transcript of Coding Apps in the Cloud with Force.com - Part I

Page 1: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Coding Apps in the Cloud with Force.com - Part IJanuary 28, 2016

Page 2: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar#forcewebinar

Speakers

Shashank SrivatsavayaSr. Developer Advocate Engineer@shashforce

Sonam RajuSr. Developer Advocate Engineer@sonamraju14

Page 3: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Page 4: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Go Social!@salesforcedevs / #forcewebinar

Salesforce Developers

Salesforce Developers

Salesforce Developers

This webinar is being recorded!The video will be posted toYouTube & the webinar recappage (same URL as registration).

Page 5: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Agenda

• Data Model

• Application

• Apex(Java vs Apex)

• Apex Classes

• SOQL and DML

• Triggers

Page 6: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Data Model

Page 7: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

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

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

• i.e. Student__c, Speaker__c, Hotel__c• Custom objects have standard fields(Audit Fields)

• CreatedDate, LastModifiedDate, LastModifiedBy, …

Page 8: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

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

e.g. Percentage__c, Site__c

Page 9: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Modeling One-to-Many Relationships

An event can have many attendees

An attendee can be associated to one Event

Page 10: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Modeling Many-to-Many RelationshipsA speaker can

have many session

assignments

A session can have many

speaker assignments

Page 11: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Id• All objects are given an Id field• Globally unique Id is assigned at record creation, cannot be

edited• "Primary key" used to access records

Page 12: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Record Name• Human readable / logical identifier• Text or Auto Number ("Sonam Raju" or RollNo-00002)• Uniqueness not enforced

Page 13: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

When you create an Object, you get…

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

Page 14: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Application

Page 15: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

What's an Application?

• Group of tabs for 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 16: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Page LayoutsDefines how you see fields, related records..on the Salesforce UI

Page 17: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Apex

Page 18: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

What is Apex?

• Salesforce platform language which is similar to Java• Object-oriented : classes encompass variables and methods• Strongly typed • Classes and Interfaces : for reusability• Cloud based compiling, debugging and unit testing

Page 19: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

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 20: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Apex Classpublic class MortgageCalculator {

public Double amount { get; set; } //variable syntax: modifier datatype variablename public Double rate { get; set; }public Integer years { get; set; }

public Double calculateMonthlyPayment() { //method syntax: modifier returndatatype methodname Integer months = years * 12; Double monthlyRate = rate / (12 * 100); return amount * (monthlyRate/ (1 - Math.pow(1 + monthlyRate, -months)));

}}

Page 21: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Development Tools• Developer Console• Force.com IDE (Eclipse Plugin)• Force CLI(command-line interface)• Mavensmate(opensource IDE)

Page 22: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Developer Console• Browser Based IDE• Create Classes, Triggers,

Pages• Execute Apex Anonymously• Execute SOQL Queries• Run Unit Tests• Review Debug Logs

Page 23: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

SOQL & DML

Page 24: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

What's SOQL?• Salesforce Object Query language• Similar to SQL• Streamlined syntax to traverse object relationships• Built into Apex

Page 25: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

SELECT Id, Name, Phone FROM Contact

Page 26: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

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

Page 27: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

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

Page 28: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

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

Page 29: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

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

Page 30: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

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

Page 31: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Master to Details

SELECT Name, (SELECT FirstName, LastName, Phone FROM Contacts) FROM Account

Page 32: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Executing SOQL in the Developer Console

Page 33: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Inlining SOQL in Apex

Integer i = [SELECT Count() FROM Session__c];

Page 34: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Inlining SOQL in Apex

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

Page 35: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

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 36: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Inlining SOQL in Apex

for (Speaker__c s : [SELECT Email__c FROM Speaker__c]) { System.debug(s.email__c);}

Page 37: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

What's DML?

• Data Manipulation Language• Language used to create, update, delete records

Page 38: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

insert

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

Page 39: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

insert

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

Page 40: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

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;

Page 41: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

delete

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

Page 42: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Triggers

Page 43: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

What's a Trigger?• Apex code executed on database events • Before or after:

• Insert• Update• Delete• Undelete

Page 44: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

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

Page 45: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

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)

Page 46: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Example 1trigger 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;}

Page 47: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Example 2trigger 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 } }}

Page 48: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar

Workflow vs TriggerWorkflow Trigger

Created with Clicks Code

What can it do • Update field• Send email• Create task

• Send outbound message• Launch flow (flow trigger)

~ Anything (e.g. create/delete records, REST callout, etc.)

Cross-object field updates Limited (detail -> master) Any

Page 49: Coding Apps in the Cloud with Force.com - Part I

developer.salesforce.com/trailhead

Page 50: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar#forcewebinar

Got Questions?

Post’em tohttp://developer.salesforce.com/forums/

Page 51: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar#forcewebinar

Q&A

Your feedback is crucial to the successof our webinar programs. Thank you!

http://bit.ly/feedbackforce

Page 52: Coding Apps in the Cloud with Force.com - Part I

#forcewebinar#forcewebinar

Thank YouTry Trailhead: http://developer.salesforce.com/trailhead

Join the conversation: @salesforcedevs