Force.com IDE (Apex and Visualforce) Internet Passcode Z6ZT Caleb Sidel, Strategic Growth, Inc.
Feb 23, 2016
Force.com IDE(Apex and Visualforce)
Internet Passcode Z6ZT
Caleb Sidel, Strategic Growth, Inc.
Agenda Install Force.com IDE
How to run a SOQL query from the IDE How to modify Objects, Workflow, etc (dangerous, but fun!) How to create an Apex Trigger (just the shell) How to create a Visualforce Page (just the shell) How to run testMethods
What is the Developer Console? Running testMethods Running queries Running anonymous script statements (dangerous, but
fun!) Apex Trigger – Create a Case from an Opportunity Visualforce – De-dupe before Saving an Account Community Resources Technical Resources
2
Install Force.com IDE IDE Install Guide
https://developer.salesforce.com/page/Force.com_IDE_Installation
Eclipse 4.3 Kepler http://
www.eclipse.org/downloads/packages/eclipse-ide-java-developers/keplerr
Java https://www.java.com/en/download/
To Check your Java Version on a PC Run CMD At the command prompt type ‘java –version’
To Check your Java Version on a Mac Open TERM At the prompt type ‘java –version’
3
Project Browser SOQL query using the .schema Objects, Workflow, etc. New Apex Trigger New Visualforce Page New Apex Class
Helper Controller Extension
Run a single Test Class/All Test Classes
4
New Project
5
SOQL & Schema Browsing
6
Metadata
7
Metadata Ever want to turn off every Workflow for an
Object? Open the object’s XML file, copy the contents into a
text editor, then set <active>false</active> for everything in Eclipse, then revert back when done.
Ever want to know where a field is used? Maybe you have data being changed and you can’t
figure out why – maybe it is referenced in a field update or a trigger!
8
New Apex/Visualforce
9
Developer Console Queries Running Tests (All vs One) Anonymous Script Statements
(aka living on the edge)
10
Developer Console - Query
11
Developer Console – Run Tests
12
Developer Console – Anonymous
13
Code Examples Apex Trigger – Create a Case from an
Opportunity Visualforce – De-dupe before saving a Lead
14
Trigger Create a Case from an Opportunity
Have you ever wanted to create a project once an Opportunity is Closed/Won? Or a survey when a Case is Closed? Or create an Order when an Opportunity is Closed/Won?
15
Trigger (GOOD)List<Case> casesToInsert = new List<Case>();for(Opportunity o : Trigger.new){ //Filter out the Id or Keyword etc if(o.IsWon) { Case c = new Case(); c.Subject = o.Name; c.Description = o.Description; c.AccountId = o.AccountId; c.Oppoprtunity__c = o.Id; //You can set origin, type, status, etc
//NEVER do a QUERY or a DML (insert, update, delete, upsert) INSIDE A LOOP!!! casesToInsert.add(c); }}
//Done with the loop, what about the insert?if(casesToInsert.size() > 0){ //DO NOT do a try/catch and ignore the error //insert below will cause the entire trigger to fail if there is even 1 case with an error. //maybe this is good? insert casesToInsert;
//If you’d rather you can use Database.insert() and handle errors one at a time. //Sometimes this is good. Just don’t “hide” the errors in System.debug() statements. //throw them or email them or something!}
16
Trigger (BAD)List<Case> casesToInsert = new List<Case>();for(Opportunity o : Trigger.new){ //Filter out the Id or Keyword etc if(o.IsWon) { Case c = new Case(); c.Subject = o.Name; c.Description = o.Description; c.AccountId = o.AccountId; c.Oppoprtunity__c = o.Id; //You can set origin, type, status, etc
//NEVER do a QUERY or a DML (insert, update, delete, upsert) INSIDE A LOOP!!! insert c; //BAD! BAD!!!! }}
//Done with the loop, what about the insert?if(casesToInsert.size() > 0){ //BAD try { insert casesToInsert; } catch (Exception exp) { //This exception only goes to a log when you monitor it. No one will ever know of the error :( System.debug(exp); } }
17
Visualforce Page There are a ton of de-dupe before saving a Lead
apps out there, so don’t actually code this in a real org! But it makes for a simple example
There are lots of ways of going about this. This is how I like to do it: First set yourself up with Development Mode (see next
slide) Then, in your Salesforce org/browser enter the URL
https://naX.salesforce.com/apex/DeDupePage The wizard will walk you through creating the page!
18
Development Mode
19
Visualforce Page – The Page (pre Extension)
20
<apex:page standardController="Lead“ > <apex:form > <apex:pageBlock mode="edit" > <apex:pageMessages /> <apex:pageBlockButtons > <apex:commandButton value="Save" action="{!Save}" /> <apex:commandButton value="Cancel" action="{!Cancel}" /> </apex:pageBlockButtons> <apex:sectionHeader title="Lead"/> <apex:pageBlockSection title="Details" columns="1" > <apex:inputField value="{!Lead.Firstname}" /> <apex:inputField value="{!Lead.Lastname}" required="true" /> <apex:inputField value="{!Lead.Company}" required="true" /> <apex:inputField value="{!Lead.Email}" required="true" /> </apex:pageBlockSection> </apex:pageBlock> </apex:form></apex:page>
Visualforce Page If you’ve created your Visualforce Page via the
Salesforce “browser” UI then once you add an “extension” or “controller” to your Page (see next slide) Salesforce will generate the Controller shell code for you!
Then you can refresh your IDE and edit the Controller there to get type ahead and other IDE goodness
21
Visualforce Page – The Page (post Extension)
22
<apex:page standardController="Lead" extensions="VFC_LeadDeDupeExt" > <apex:form > <apex:pageBlock mode="edit" > <apex:pageMessages /> <apex:pageBlockButtons > <apex:commandButton value="Save" action="{!customSave}" /> <apex:commandButton value="Cancel" action="{!Cancel}" /> </apex:pageBlockButtons> <apex:sectionHeader title="Lead"/> <apex:pageBlockSection title="Details" columns="1" > <apex:inputField value="{!Lead.Firstname}" /> <apex:inputField value="{!Lead.Lastname}" required="true" /> <apex:inputField value="{!Lead.Company}" required="true" /> <apex:inputField value="{!Lead.Email}" required="true" /> </apex:pageBlockSection> </apex:pageBlock> </apex:form></apex:page>
Visualforce Page – The Controller
23
public with sharing class VFC_LeadDeDupeExt { ApexPages.StandardController stdController; private Lead currentLead;
public VFC_LeadDeDupeExt(ApexPages.StandardController controller) { stdController = controller; currentLead = (Lead)controller.getRecord(); } public PageReference customSave() { List<Lead> duplicates = [SELECT Id FROM Lead WHERE Email = :currentLead.Email]; if(duplicates != null && duplicates.size() > 0) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: A duplicate email address was found with Id: ' + duplicates[0].Id)); return null; } //try //{ return stdController.save(); //} //catch (Exception exp) //{ //TODO display pretty errors //} return null; }
}
Common Force.com Terms Trigger – custom code that runs on save (like a workflow rule) Visualforce – custom code that makes the screen look however
your heart desires Batch Apex – apex code that can run on Millions upon Millions of
records Scheduled Apex – apex code that runs at a set time each day
(up to 5 times per day (shhhh - there is an exception via Developer Console)) Force.com API (SOAP) – the “old fashioned” (i.e. good) way to
integrate data into/out of salesforce Force.com API (REST API) – the “new fashioned” way to do
real-time or highspeed integrations of small transactional data Native vs Non-Native Applications – Native applications run
100% on Force.com (and if we stretch, this also means Heroku). Non-native applications require a separate server to run a portion of the code. Why write non-native? That code supports non-Salesforce use cases. That code requires massive data volumes/complexities that Force.com would otherwise limit. That code was cheaper to write in Ruby/Java than Force.com.
24
Community Resources Developer Site:
http://developer.force.com/ Developer Discussions:
http://boards.developerforce.com/sforce/?category.id=developers
Linked In Community:Salesforce.com Professional CommunitySalesforce.com Certified ProfessionalsAustin Force.com Developer Group
Meetup Site
25
Technical Resources Introduction to the Force.com Database How to Create a Custom Object Introduction to Force.com Apex Code VisualForce Webinar (skip to 9:45) – nice overview VisualForce Quick Start (VisualForce
Developer's Guide p15-25, 32-39) Creating a Developer Edition Account Apex Language Reference and Wiki VisualForce Developer's Guide and Wiki Force.com Developer’s Guide Force.com Web Services API Developer's Guide Developer Technical Wiki
26
Contact Information More contact information then even I know what
to do with!
512.814.5775
Twitter: @calebsidel LinkedIn: www.linkedin.com/in/csidel Dreamforce Chatter
27