I use a mocking framework that exposes a method for generating mock record ids. The utility method uses getKeyPrefix()
to mock an Id. This works for most object types but doesn't work for __Share
records.
I'm not sure how to mock these Ids because even though they are "unstable" salesforce somehow can validate the id when setting it and throws an error
Folder__Share testFolderShare1 = new Folder__Share(
Id = TestUtils.generateId(Folder__Share.SObjectType)
);
System.TypeException: Invalid id value for this SObject type: 02f000000000007AAA
How can I generate the key prefix for unstable types?
Util for generating Id:
private static Integer seed = 1;
public static Id generateId(SObjectType objectType) {
String result = String.valueOf(seed++);
String prefix = objectType.getDescribe().getKeyPrefix();
if (prefix == null && objectType.getDescribe().getAssociateEntityType() == 'Share') {
prefix = '02c';
}
return prefix + '0'.repeat(12 - result.length()) + result;
}
02c
as the prefix for share records but I'm not sure if this will work for every share sobject type. I updated the post to include the util which is a modified version taken from here salesforce.stackexchange.com/a/21297/17894