0

I just learned about setting related records with external ids, and I love it. It makes test code much easier to read and write, and it means I have to query less in general.

In my ideal world, metadata-type SObjects like ListView, Folder, RecordType, QueueSobject, and Group would all have native external Ids (like DeveloperName, but globally unique). I was wondering if that was the case already. Has anyone explored this approach?

1 Answer 1

2

Generally speaking, no, there isn't a globally unique name system. However, if you take some due diligence, you can upsert most types of records via their Name:

Group g = new Group(Name='Some Unique Name');
upsert g Group.Name;

However, this generally doesn't work on polymorphic fields, so you still need to query for records in that case:

User someUser = [SELECT Id FROM User WHERE IsActive = TRUE LIMIT 1];
GroupMember gm = new GroupMember(Group=new Group(Name='Some Unique Name'), UserOrGroupId=someUser.Id);
upsert gm;

However, this may not always be ideal for every situation, but you can leverage this technique in many common use cases to reduce the number of queries you need to use.

This technique works for virtually any object that has a Name, such as Opportunity, Group, etc. Some objects like Folder do not allow upsert, and others are tricky to use because they don't have a Name field, do not have an indexed name field, or have an auto-number for their name field (Case, custom objects configured with auto-numbers).

Unfortunately, you'll have to consult the Setup screen to see which fields are indexed, or do some experiments.

3
  • 1
    Name is unique for Groups? Even between Type='Group' and Type='Queue'? Commented Jun 27, 2018 at 18:50
  • @CharlesKoppelman Nope. Definitely not unique. But if you're the kind of person that can keep your values unique, you can use the technique to refer to specific groups without a query.
    – sfdcfox
    Commented Jun 27, 2018 at 19:30
  • This is cool!! Good to know for saving soqls. Commented Jun 27, 2018 at 21:48

You must log in to answer this question.

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