Last active
December 18, 2015 03:58
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.