Skip to content

Instantly share code, notes, and snippets.

Salesforce Developer Workshop

@joshbirk
joshbirk / CanJSDemo.page
Created February 26, 2014 22:36
Visualforce Remote Objects example using the CanJS framework
<apex:page showHeader="false" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false">
<HTML>
<HEAD>
<script src="{!URLFOR($Resource.jquery)}"></script>
<script src="{!URLFOR($Resource.CanJS,'can.jquery.js')}"></script>
<script src="{!URLFOR($Resource.bootstrap,'js/bootstrap.js')}"></script>
<link rel="stylesheet" href="{!URLFOR($Resource.bootstrap, 'css/bootstrap.css')}"></link>
<apex:remoteObjects >
<apex:remoteObjectModel name="Contact" jsShorthand="contact" fields="Name,Id,Email"/>
<script>
@joshbirk
joshbirk / TestFactory.cls
Created March 10, 2014 19:53
New TestFactory
@isTest
public class TestDataFactory {
public static Invoice__c createOneInvoiceStatement(
Boolean withLineItem) {
// Create Warehouse if one does not exist
Warehouse__c w;
List<Warehouse__c> warehouse = [SELECT ID from Warehouse__c];
if(warehouse.size() == 0) {
@isTest
private class TestCleanUpBatchClass {
static testmethod void test() {
// The query used by the batch job.
String query = 'SELECT Id,CreatedDate FROM Merchandise__c ' +
'WHERE Id NOT IN (SELECT Merchandise__c FROM Line_Item__c)';
// Create some test merchandise items to be deleted
// by the batch job.
Warehouse__c w = new Warehouse__c(
@joshbirk
joshbirk / CreateMerch.cls
Created March 10, 2014 20:29
New Execute Anonymous
// Create Warehouse if one does not exist
Warehouse__c w;
List<Warehouse__c> warehouse = [SELECT ID from Warehouse__c];
if(warehouse.size() == 1) {
w = new Warehouse__c(
Name='Warehouse 1',
City__c='San Francisco',
Location__Latitude__s=37.7833,
Location__Longitude__s=122.4167);
trigger RestrictInvoiceDeletion on Invoice__c (before delete) {
// With each of the invoice statements targeted by the trigger
// and that have line items, add an error to prevent them
// from being deleted.
for (Invoice__c invoice :
[SELECT Id
FROM Invoice__c
WHERE Id IN (SELECT Invoice__c FROM Line_Item__c) AND
Id IN :Trigger.old]){
Trigger.oldMap.get(invoice.Id).addError('Cannot delete invoice statement with lineitems');
@joshbirk
joshbirk / ListDetailController.cls
Last active August 29, 2015 13:57
PageBlock Components Test
public with sharing class ListDetailController {
public List<Account> accounts {get; set;}
public Account account_detail {get; set;}
public Id account_id {get; set;}
public Boolean showBack {get; set;}
// The extension constructor initializes the private member
// variable mysObject by using the getRecord method from the standard
// controller.
@joshbirk
joshbirk / TestPage.page
Created March 28, 2014 20:47
Quick OneStarter Example
<apex:page standardController="Invoice__c" extensions="LineItemRemotingExtension" showHeader="false">
<apex:stylesheet value="{!URLFOR($Resource.OneStarter,'icons.css')}"/>
<apex:stylesheet value="{!URLFOR($Resource.OneStarter,'styles.css')}"/>
<apex:stylesheet value="{!URLFOR($Resource.OneStarter,'OneStarter.css')}"/>
<apex:includeScript value="//code.jquery.com/jquery-1.10.2.min.js" />
<apex:includeScript value="{!URLFOR($Resource.TouchSwipe,'jquery.touchSwipe.min.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.OneStarter,'jquery.onestarter.js')}"/>
<apex:includeScript value="/canvas/sdk/js/publisher.js"/>
@joshbirk
joshbirk / 1: WarehousePull.cls
Last active August 29, 2015 13:58
HTTP Mock Extra Credit
/*
This is an Apex class which calls out to an endpoint to find new merchandise items to be inserted into the system.
Before it can be deployed to production, however, it needs a unit test. Since it makes an HTTP callout, however,
the unit test will require a mock class.
Before you can test the class, you will also need to open a Remote Site to let your Salesforce instance know
it is a legitimate site:
1. Go to Setup | System Administration | Security Controls | Remote Site Settings.
2. Click New.
@joshbirk
joshbirk / 1: Create Streaming Topics
Created April 7, 2014 18:12
ELEVATE Extra Credit: jQuery and the Streaming API
/*
The Streaming API allows clients to get updates on data changes in near real-time.
This is done by keeping an open connection instead repeatedly polling the server, which is much more lightweight.
To accomplish this connection, you need a cometD capable client. Luckily, there is a jQuery plugin for this -
which makes jQuery in Visualforce an easy way to test out the API.
To complete this extra credit:
1. Create a Streaming Topic with the following code.
2. Include jQuery, the cometD and iCanHas libraries in a Visualforce page.
3. Add the necessary JavaScript to connect to the Streaming API