Skip to content

Instantly share code, notes, and snippets.

@pchittum
Last active May 23, 2018 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pchittum/afbef488741caf3b137e229d730e805b to your computer and use it in GitHub Desktop.
Save pchittum/afbef488741caf3b137e229d730e805b to your computer and use it in GitHub Desktop.
SObject switch and polymorphic SObject fields
List<User> notifyUsers = new List<User>();
List<Group> notifyGroups = new List<Group>();
List<Property__c> properties = [SELECT
TYPEOF Owner
WHEN User THEN Id, Name, Username, Email
WHEN Group THEN Id, Name, Email, DoesSendEmailToMembers
END
FROM Property__c
WHERE ID in Trigger.new];
for (Property__c prop: properties) {
switch on prop.Owner {
when User u {
notifyUsers.add(u);
}
when Group g {
notifyGroups.add(g);
}
}
}
List<User> notifyUsers = new List<User>();
List<Group> notifyGroups = new List<Group>();
List<Property__c> properties = [SELECT
TYPEOF Owner
WHEN User THEN Id, Name, Username, Email
WHEN Group THEN Id, Name, Email, DoesSendEmailToMembers
END
FROM Property__c
WHERE ID in Trigger.new];
for (Property__c prop: properties) {
switch on (SObject) prop.Owner {
when User u {
notifyUsers.add(u);
}
when Group g {
notifyGroups.add(g);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment