2

Using the IDGenerator class, used on FFLib, how can I generate a RecordType object Id?

It is impossible to use IDGenerator.generate(RecordType.SobjectType).

If not possible to get the sObjectType, is there a way to get just the prefix of a record type object?

  • The RecordType prefix is '012'. – David Reed May 30 '18 at 15:55
  • 1
    @cropredy pointed out that it's because RecordType.SObjectType is actually a SObjectField instead of an SObjectType. You can reference it by other means, such as sObjectType.RecordType.sObjectType or RecordType.Id.getSobjectType(), etc. It's rather unfortunate that this one object is broken relative to everything else. – sfdcfox May 31 '18 at 1:56
3

You can get the key prefix with getKeyPrefix:

String keyPrefix = SObjectType.RecordType.getKeyPrefix();

IDGenerator should also work in this case, as it's basically doing the same thing you would be doing.

| improve this answer | |
  • see my answer how you have to tweak the arg to fflib_IdGenerator for RecordType – cropredy May 31 '18 at 1:03
3

The reason why IDGenerator.generate(RecordType.SobjectType) doesn't work is that SobjectType is a field name on SObject RecordType and you can't compile the expression

This works though:

fflib_IdGenerator.generate(new RecordType().getSobjectType())
| improve this answer | |
  • Ah, yes. That explains a lot. Your method isn't necessary, though, you can also write the rather obtuse: sObjectType.RecordType.sObjectType as well. – sfdcfox May 31 '18 at 1:50
  • @sfdcfox ah good point. I never used that construction before – cropredy May 31 '18 at 2:07
1

Worth noting fflib Id generator fails after 1000 Id's. I've ported it into something simpler:

static Id getId(String keyPrefix, Decimal n){
    String fakeIdPrefix = '000000000000'.substring(0, 12 - n.precision());
    return Id.valueOf(keyPrefix + fakeIdPrefix + n);
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.