Skip to content

Instantly share code, notes, and snippets.

View douglascayers's full-sized avatar

Doug Ayers douglascayers

View GitHub Profile
@douglascayers
douglascayers / CRMCasesConsoleController.java
Created April 13, 2015 20:28
Service Cloud - Visualforce Custom Case List View Concept
public class CRMCasesConsoleController {
private ApexPages.StandardSetController controller;
public List<Case> cases {
get { return controller.getRecords(); }
}
public Integer page {
get { return controller.getPageNumber(); }
@douglascayers
douglascayers / DuplicateRecordUtils.c
Last active March 16, 2022 06:35
Devising way to use Salesforce process builder and flow to automate merging duplicate records
/**
* Developed by Doug Ayers (douglascayers.com)
*/
public with sharing class DuplicateRecordUtils {
public static Set<ID> getDuplicateRecordIds( ID recordId ) {
Set<ID> duplicateRecordIds = new Set<ID>();
// Potential duplicate records are grouped together into 'duplicate record sets'.
@douglascayers
douglascayers / BitlyService.java
Last active January 7, 2021 06:04
Salesforce apex service to make http callout to Bitly url shortener service. The .java extension is just for syntax highlighting, save them as .cls in your project.
/**
* Simple service to make http callout to Bitly url shortener service.
*/
public class BitlyService {
/**
* Given a long URL will return the shortened version.
* https://dev.bitly.com/api-reference#createBitlink
*/
public String shorten(String url) {
HttpRequest req = new HttpRequest();
@douglascayers
douglascayers / BitlyShortenURLInvocable.java
Last active January 7, 2021 06:05
Salesforce apex class that exposes the BitlyService via @InvocableMethod to be called by Process Builder. The .java extension is just for syntax highlighting, save them as .cls in your project.
public class BitlyShortenURLInvocable {
@InvocableMethod(
label = 'shorten'
description = 'Given case IDs then generates a bitly short url for them'
)
public static void shorten(List<ID> caseIds) {
// You can't invoke http callouts from Process Builder or Flow
// because the database transaction has not committed yet, you will get error:
// "System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out"
@douglascayers
douglascayers / CampaignTrigger.trigger
Last active January 8, 2016 02:06
When cloning Campaigns, preserves the Campaign Member Record Type. Workaround for https://success.salesforce.com/issues_view?id=a1p30000000T2jzAAC
trigger CampaignTrigger on Campaign ( before insert ) {
List<Campaign> campaigns = Trigger.new;
Set<ID> clonedCampaignIds = new Set<ID>();
for ( Campaign campaign : campaigns ) {
if ( campaign.isClone() ) {
clonedCampaignIds.add( campaign.getCloneSourceId() );
@douglascayers
douglascayers / gist:aa2c3d07560e1cde3df6
Created January 31, 2016 08:26
JQuery Autocomplete Plugin styled with Salesforce Lightning Design System (SLDS) in Visualforce
<apex:page >
<!-- good ol' jquery! -->
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<script>$j = jQuery.noConflict();</script>
<!-- the infamous SLDS, aka my nemesis -->
<apex:stylesheet value="{!URLFOR($Resource.SLDS0121,'assets/styles/salesforce-lightning-design-system-vf.css')}"/>
@douglascayers
douglascayers / BitlyHttpCalloutMock.java
Last active January 7, 2021 05:23
Apex test classes for the BitlyService. The .java extension is just for syntax highlighting, save them as .cls in your project.
@IsTest
public class BitlyHttpCalloutMock implements HttpCalloutMock {
public HttpResponse respond(HttpRequest req) {
String endpoint = req.getEndpoint();
if ( endpoint.contains('/oauth/access_token') ) {
return buildOAuthResponse( req );
} else if ( endpoint.contains('/v4/shorten') ) {
@douglascayers
douglascayers / package.xml
Created April 25, 2016 03:14
Retrieves all Flows and Flow Definitions (aka Process Builders)
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>*</members>
<name>Flow</name>
</types>
<types>
<members>*</members>
<name>FlowDefinition</name>
</types>
@douglascayers
douglascayers / RedirectApexController.java
Created May 9, 2016 20:58
Removing the CSRF _CONFIRMATIONTOKEN from page request when passing all parameters to next redirect page. This began causing me issues in Summer '16 release: https://success.salesforce.com/issues_view?id=a1p3A000000jknNQAQ
public class RedirectApexController {
public RedirectApexController( ApexPages.StandardController stdController ) {}
public PageReference redirect() {
// ... do some logic to determine where to redirect to ...
PageReference page = new PageReference('/apex/MyPage');
// pass any parameters that came in on request on to the final destination
@douglascayers
douglascayers / LeadConvertedViewController.java
Created June 4, 2016 19:41
Simple Visualforce template for displaying converted lead records. Once a lead is converted the standard detail page is no longer accessible, must use reports or visualforce.
/**
* https://twitter.com/Joy_SH/status/738087166547398656
* https://help.salesforce.com/apex/HTViewSolution?id=000175908&language=en_US
*/
public with sharing class LeadConvertedViewController {
public Lead record { get; private set; }
public LeadConvertedViewController( ApexPages.StandardController stdController ) {
this.record = (Lead) queryRecord( stdController.getId() );