Skip to content

Instantly share code, notes, and snippets.

@ChrisBland
Last active December 18, 2015 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisBland/5721696 to your computer and use it in GitHub Desktop.
Save ChrisBland/5721696 to your computer and use it in GitHub Desktop.
Salesforce ID issue - with Tests! Both the Picklist and the String values here fail
@isTest
private class TestIDs {
@isTest static void test_method_one() {
Map<Id,SObject> oldMap = new Map<Id,SObject>();
Opportunity o = new Opportunity();
o.Name = 'Test Oppty';
o.StageName = 'Prospecting';
o.CloseDate = System.Today().addDays(5);
Database.Insert(o);
oldMap.put(o.Id, o);
TriggerHandler th = new TriggerHandler();
SObject sobj = oldMap.values()[0];
Boolean isPicklistId = th.isInstanceOf( sobj.get('StageName') );
Boolean isDateId = th.isInstanceOf( sobj.get('CloseDate') );
Boolean isStringId = th.isInstanceOf( sobj.get('Name') );
System.AssertEquals(isPicklistId, false); //==> THIS FAILS
System.AssertEquals(isDateId, false);
System.AssertEquals(isStringId, false); //==> THIS FAILS
}
}
public class TriggerHandler{
public TriggerHandler(){}
public Boolean isInstanceOf(Object obj){
if(obj InstanceOf Id){
return true;
}else{
return false;
}
}
}
@ChrisBland
Copy link
Author

Hello readers of this Gist;

This is a way to reproduce the errors in Summer '13 related to Ids. What is happening:

When you use SObjects, you are given Objects back when you do a .get(). When you pass an object to instanceOf method comparing to type Id. The tests on line 21 & 23 fail.

If you are at Salesforce.com & would like to discuss these, please contact me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment