0

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;
}
3
  • 2
    It works fine for me with standard share objects at least. Can you share how you are generating the prefix specifically? Also the line that throws this error. If you generate a dummy Id on a record, then try to update it, this behavior would be expected.
    – Adrian Larson
    Commented Aug 13, 2022 at 3:06
  • I think I found the solution by using 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 Commented Aug 13, 2022 at 14:42
  • I should also add this is for a custom object share Commented Aug 13, 2022 at 14:49

0

You must log in to answer this question.

Browse other questions tagged .