0

I have to create some triggers and flows in a 2nd-generation managed package that reference a record type developer name. That's fine, except that the record types that I need to reference are unmanaged in my production org.

These flows and triggers can work just fine in my scratch org, but then when creating a new package version, I either get the following in flow:

"field integrity exception: unknown (A condition has a reference to "$Record.RecordType.DeveloperName", which doesn't exist."

Or I get an error with one of my triggers stating that the field RecordType doesn't exist.

Any ideas on a workaround or what I could be missing?

1 Answer 1

3

First you need to check to see if record types are enabled on the object you're dealing with - one of the pains of being an ISV is that you have no control over the environment where your code runs. I'm not 100% sure on how you do that from flow. In Apex you can use the describe methods to see if the RecordTypeId field exists or not.

If record types are enabled, and you want to avoid a hard reference the best bet is to query for it by name, in flow and in Apex.

In Apex you'd do something like this - notice there are no hard references involved:


Account a = new Account();
String recordType = 'PersonAccount';

if (Schema.SObjectType.Account.fields.GetMap().Get('RecordTypeId') != null) {
    a.RecordTypeId = Database.query('select Id from RecordType where SobjectType = \'Account\' and DeveloperName = :someRTName');
}

For your use case, where you want to know if a lead's record type is within a certain list of record type developer names you could do something like this:

Boolean recordTypesEnabled = Schema.SObjectType.Account.fields.GetMap().Get('RecordTypeId') != null;

Set<Id> rtIds = new Set<Id>();
List<String> rtnames = new List<String>{'name1','name2','name3'};

for(SObject s : Database.Query('SELECT Id FROM RecordType WHERE SObjectName = \'Lead\' AND DeveloperName in : rtnames')) {
  rtIds.add(s.Id);
}

for(Lead l : trigger.new) {

  if(recordTypesEnabled && l.get('RecordTypeId') != null && rtIds.contains(l.get('RecordTypeId'))) {
      // do something
  }
}

I've not compiled this code but it should give you an idea of what to do!

3
  • Thanks I'm on the right track with this. How would I do this in a trigger on Lead, for example to check in the loop of leads if the lead record type belongs to 1 of multiple developer names? Commented Nov 30, 2023 at 11:45
  • @user1702965 See my edit above!
    – Matt Lacey
    Commented Nov 30, 2023 at 11:52
  • This worked. Only modification is that I needed to cast to ID: Id leadRecordTypeId = (Id)lead.get('RecordTypeId'); // Cast to Id // Check if RecordType is enabled, the Lead has a RecordTypeId, and it's one of the target types if (recordTypesEnabled && leadRecordTypeId != null && rtIds.contains(leadRecordTypeId)) { Commented Nov 30, 2023 at 13:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .