KEMBAR78
Coding Apps in the Cloud with Force.com - Part I | PPTX
#forcewebinar
Coding Apps in the Cloud with Force.com - Part I
January 28, 2016
#forcewebinar#forcewebinar
Speakers
Shashank Srivatsavaya
Sr. Developer Advocate Engineer
@shashforce
Sonam Raju
Sr. Developer Advocate Engineer
@sonamraju14
#forcewebinar
#forcewebinar
Go Social!
@salesforcedevs / #forcewebinar
Salesforce Developers
Salesforce Developers
Salesforce Developers
This webinar is being recorded!
The video will be posted to
YouTube & the webinar recap
page (same URL as
registration).
#forcewebinar
Agenda
• Data Model
• Application
• Apex(Java vs Apex)
• Apex Classes
• SOQL and DML
• Triggers
#forcewebinar
Data Model
#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, …
#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
#forcewebinar
Modeling One-to-Many Relationships
An event can have many
attendees
An attendee can be
associated to one Event
#forcewebinar
Modeling Many-to-Many Relationships
A speaker can
have many
session
assignments
A session can
have many
speaker
assignments
#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
#forcewebinar
Record Name
• Human readable / logical identifier
• Text or Auto Number ("Sonam Raju" or RollNo-00002)
• Uniqueness not enforced
#forcewebinar
When you create an Object, you get…
• A CRUD user interface
• Instant Mobile App access (Salesforce1)
• A REST API
• Rich Metadata
• Indexed search
#forcewebinar
Application
#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
#forcewebinar
Page Layouts
Defines how you see fields, related records..on the
Salesforce UI
#forcewebinar
Apex
#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
#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
#forcewebinar
Apex Class
public 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)));
}
}
#forcewebinar
Development Tools
• Developer Console
• Force.com IDE (Eclipse Plugin)
• Force CLI(command-line interface)
• Mavensmate(opensource IDE)
#forcewebinar
Developer Console
• Browser Based IDE
• Create Classes, Triggers,
Pages
• Execute Apex Anonymously
• Execute SOQL Queries
• Run Unit Tests
• Review Debug Logs
#forcewebinar
SOQL & DML
#forcewebinar
What's SOQL?
• Salesforce Object Query language
• Similar to SQL
• Streamlined syntax to traverse object relationships
• Built into Apex
#forcewebinar
SELECT Id, Name, Phone
FROM Contact
#forcewebinar
SELECT Id, Name, Phone
FROM Contact
WHERE Phone <> null
#forcewebinar
SELECT Id, Name, Phone
FROM Contact
WHERE Phone <> null
AND Name LIKE '%rose%'
#forcewebinar
SELECT Id, Name, Phone
FROM Contact
WHERE Phone <> null
AND Name LIKE '%rose%'
ORDER BY Name
#forcewebinar
SELECT Id, Name, Phone
FROM Contact
WHERE Phone <> null
AND Name LIKE '%rose%'
ORDER BY Name
LIMIT 50
#forcewebinar
Details to Master
SELECT Id, Name, Phone,
Account.Name
FROM Contact
WHERE Phone <> null
AND Name LIKE '%rose%'
ORDER BY Name
LIMIT 50
#forcewebinar
Master to Details
SELECT Name,
(SELECT FirstName, LastName, Phone
FROM Contacts)
FROM Account
#forcewebinar
Executing SOQL in the Developer Console
#forcewebinar
Inlining SOQL in Apex
Integer i = [SELECT Count()
FROM Session__c];
#forcewebinar
Inlining SOQL in Apex
String level = 'Advanced';
List<Session__c> sessions =
[SELECT Name, Level__c FROM
Session__c
WHERE Level__c = :level];
#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];
#forcewebinar
Inlining SOQL in Apex
for (Speaker__c s : [SELECT
Email__c FROM Speaker__c])
{
System.debug(s.email__c);
}
#forcewebinar
What's DML?
• Data Manipulation Language
• Language used to create, update, delete records
#forcewebinar
insert
Session__c session = new Session__c();
session.name = 'Apex 101';
session.level__c = 'Beginner';
insert session;
#forcewebinar
insert
Session__c session = new Session__c(
name = 'Apex 201',
level__c = 'Intermediate'
);
insert session;
#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;
#forcewebinar
delete
String name = 'Testing 501';
Session__c session =
[SELECT Name FROM Session__c
WHERE Name=:name];
delete session;
#forcewebinar
Triggers
#forcewebinar
What's a Trigger?
• Apex code executed on database events
• Before or after:
• Insert
• Update
• Delete
• Undelete
#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
#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)
#forcewebinar
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;
}
#forcewebinar
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
}
}
}
#forcewebinar
Workflow vs Trigger
Workflow 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
developer.salesforce.com/trailhead
#forcewebinar#forcewebinar
Got Questions?
Post’em to
http://developer.salesforce.com/forums/
#forcewebinar#forcewebinar
Q&A
Your feedback is crucial to the success
of our webinar programs. Thank you!
http://bit.ly/feedbackforce
#forcewebinar#forcewebinar
Thank You
Try Trailhead: http://developer.salesforce.com/trailhead
Join the conversation: @salesforcedevs

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