Skip to content

Instantly share code, notes, and snippets.

View alan-morey's full-sized avatar

Alan Morey alan-morey

View GitHub Profile
@alan-morey
alan-morey / TestSpy.cls
Last active August 29, 2015 13:57
Apex Test Spy
@isTest
public class TestSpy {
public static final String UNKNOWN = '<unknown>';
public static final String VOID_RETURN_TYPE = '<void>';
String methodName;
List<Object[]> callArgs;
Exception throwResult;
Boolean isVoidReturn = true;
/**
* Salesforce does not allow the ability to write to certain SObject fields
* such as LastModifiedDate, CreatedDate etc. Even if those objects are
* not going to be inserted into the DB. This makes testing around these
* fields difficult as we always need to hit the database to load these values
* and it's not possible to inject the values you require.
*
* This method gets around that limitation by using JSON. We first create
* a representation of the data as a map of fields/values pairs. Then we
* transform the map into a JSON representation. This respresentation includes
@alan-morey
alan-morey / Anonymous.cls
Last active August 29, 2015 14:02
Possible regression in Apex equals() implementation with Salesforce Summer '14
public class PairNumbers {
Integer x,y;
public PairNumbers(Integer a, Integer b) {
x=a;
y=b;
}
public Boolean equals(Object obj) {
if (obj != null && obj instanceof PairNumbers) {
@alan-morey
alan-morey / LongAndIntegerMinValueLiteralBug.cls
Created October 1, 2014 22:44
Salesforce Apex bug with Long and Integer when trying to represent the minimum value as a literal
// Apex seems to have a bug when trying to represent the minimum value for both
// Long and Integer primitive types as a literal value.
//
//Long MIN_LONG = -9223372036854775808L; // Compile Error: Invalid Long: 9223372036854775808L
//Integer MIN_INT = -2147483648; // Compile Error: Invalid Integer: 2147483648
// Workaround: Overflow the MAX values
Long LONG_MAX_VALUE = 9223372036854775807L;
Long LONG_MIN_VALUE = LONG_MAX_VALUE + 1; // Overflow => -9223372036854775808L
@alan-morey
alan-morey / SystemHashCodeBugTest.cls
Created April 6, 2015 19:33
Salesforce Spring '15 - System.hashCode() producing unexpected result when using same value with different reference types.
// Unexpected results for System.hashCode() when using same values for Salesforce IDs with different reference types.
//
// https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_system.htm?CSHID=apex_methods_system_system.htm#apex_system_system_hashcode
//
// API v33
@isTest
class SystemHashCodeBugTest {
static final String STR_VAL = 'foobar';
static final Id USER_ID_VAL = UserInfo.getUserId();
static final Id LITERAL_ID_VAL = '001000000000001';
@alan-morey
alan-morey / Uuid.cls
Last active August 29, 2015 14:18
UUID Apex
public class Uuid {
static final String GROUP_SEP = '-';
Uuid() {}
public static String randomUuid() {
String hex = EncodingUtil.convertToHex(Crypto.generateAesKey(128));
return String.join(new String[] {
hex.substring(0,8),
@alan-morey
alan-morey / ScheduleUtil.cls
Created July 24, 2012 21:46
Apex ScheduleUtil
global class ScheduleUtil {
global static String cronExpression(DateTime dt) {
return dt.format('s m H d M ? yyyy');
}
static testMethod void testCronExpression() {
DateTime dt = DateTime.now();
String expected = dt.second() + ' ' + dt.minute() + ' ' + dt.hour() + ' ' + dt.day() + ' ' + dt.month() + ' ? ' + dt.year();
@alan-morey
alan-morey / DescribeMetadataResultFileParser.php
Created November 1, 2012 16:27
PHP classes to parse result files from the Force.com Mirgration Tool "describeMetadata" and "listMetadata" ant tasks.
<?php
class DescribeMetadataResultFileParser {
const BOUNDARY = '************************************************************';
const XML_NAME_MARKER = 'XMLName: ';
const DIR_NAME_MARKER = 'DirName: ';
const SUFFIX_MARKER = 'Suffix: ';
const HAS_META_FILE_MARKER = 'HasMetaFile: ';
const IN_FOLDER_MARKER = 'InFolder: ';
const CHILD_OBJECTS_MARKER = 'ChildObjects: ';
@alan-morey
alan-morey / ForceService.groovy
Created November 24, 2012 01:29
Simple wrapper for querying Salesforce using PartnerConnection in Groovy. Dependencies are pulled in from maven repository via Grape.
@Grab(group='com.force.sdk', module='force-connector', version='22.0.9-BETA')
import com.force.sdk.connector.ForceConnectorConfig
import com.force.sdk.connector.ForceServiceConnector
import com.sforce.soap.partner.PartnerConnection
import com.sforce.soap.metadata.FileProperties
import com.sforce.soap.metadata.ListMetadataQuery
import java.net.URLEncoder
@alan-morey
alan-morey / BehaviorUnexpectedForStringValueOfWithDateAndDateTime.cls
Last active December 24, 2015 13:29
Unexpected result when using String.valueOf() to get the value of a Date and DateTime, when using an Object reference.
// Asserts fail for the following
/////////////////////////////////
DateTime dt = DateTime.now(); // VARIABLE_ASSIGNMENT dt = 2013-10-03T05:35:37.373Z
System.assertEquals(String.valueOf(dt), String.valueOf((Object) dt));
// Assertion Failed: Expected: 2013-10-02 22:35:37, Actual: 2013-10-03 05:35:37
Date d = Date.today(); // VARIABLE_ASSIGNMENT d = 2013-10-02T00:00:00.000Z
System.assertEquals(String.valueOf(d), String.valueOf((Object) d));
// Assertion Failed: Expected: 2013-10-02, Actual: 2013-10-02 00:00:00