Skip to content

Instantly share code, notes, and snippets.

@dhoechst
dhoechst / QuoteApprovalController.cls
Created November 18, 2021 17:50
Salesforce CPQ Submit Quote Page that prevents multiple submits
public with sharing class QuoteApprovalController {
private SBQQ__Quote__c sbQuote;
ApexPages.StandardController qtController;
public SBAA__Approval__c[] approvals {
get {
if (approvals == null) {
approvals = SBAA.ApprovalAPI.preview(
sbQuote.Id,
SBAA__Approval__c.Quote__c
);
@dhoechst
dhoechst / OppTerrAssignDefaultLogicFilter.class
Last active January 30, 2023 21:13
Salesforce Opportunity Territory Assignment Logic
/*** Apex version of the default logic.
* If opportunity's assigned account is assigned to
* Case 1: 0 territories in active model
* then set territory2Id = null
* Case 2: 1 territory in active model
* then set territory2Id = account's territory2Id
* Case 3: 2 or more territories in active model
* then set territory2Id = account's territory2Id that is of highest priority.
* But if multiple territories have same highest priority, then set territory2Id = null
*/
@dhoechst
dhoechst / 0-ApexTestingBestPractices.md
Last active April 28, 2022 03:30
Testing Best Practices and Examples

This Gist shows code examples for Apex Testing Best Practices. It relys on having the TestFactory in your org.

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Accounts_with_Leads_2</members>
<members>Accounts_with_leads</members>
<name>ReportType</name>
</types>
<version>38.0</version>
</Package>
@dhoechst
dhoechst / DataTableAjax
Last active July 23, 2021 01:29
Datatable Example
<apex:page>
<head>
<apex:includescript value="//code.jquery.com/jquery-1.11.1.min.js" / >
<apex:includescript value="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js" />
<apex:stylesheet value="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css" />
<!-- I have a static resource called famfamfam which is the zip file from http://www.famfamfam.com/lab/icons/silk/famfamfam_silk_icons_v013.zip -->
<style>
public class Loader {
public static Map<Id, Account> acountIdToRecordMap {
get {
if (acountIdToRecordMap == null) {
acountIdToRecordMap = new Map<Id, Account> ([Select Name from Account]);
}
return acountIdToRecordMap;
}
private set;
}
@dhoechst
dhoechst / package.xml
Created July 3, 2017 16:49
Package.xml for metadate retrieval
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>*</members>
<name>Profile</name>
</types>
<types>
<members>MyTab</members>
<name>CustomTab</name>
</types>
@dhoechst
dhoechst / 1 - DynamicEmailController.class
Last active March 21, 2017 04:58
Dynamic Email Templates
public class DynamicEmailController {
public DynamicEmailController() {}
public String emailRelatedToId {get; set;}
public String emailTemplateId {get; set;}
public Boolean isHtml {get; set;}
private Messaging.SingleEmailMessage renderedEmail {
get {
@dhoechst
dhoechst / customlabels.eml
Created March 20, 2017 02:19
Custom Label Email Template
<messaging:emailTemplate subject="{!SUBSTITUTE($Label.AccountEmailSubject,'{0}', relatedTo.Name)}"
relatedToType="Account" language="{!relatedTo.Language__c}">
<messaging:htmlEmailBody >
{!$Label.EmailBody}
</messaging:htmlEmailBody>
</messaging:emailTemplate>
@dhoechst
dhoechst / 1 - Singleton
Last active December 23, 2015 12:39
Trying to understand the singleton pattern and why some people consider it bad.
// from http://wiki.developerforce.com/page/Apex_Design_Patterns_-_Singleton
// What makes this a singleton pattern? Is it that it uses a private constructor?
public class AccountFooRecordType {
// private static variable referencing the class
private static AccountFooRecordType instance = null;
public String id {get;private set;} // the id of the record type
// The constructor is private and initializes the id of the record type
private AccountFooRecordType(){