Apex FP provides functional constructs for SObject
collections!
List<Opportunity> largeOpportunities = SObjectCollection.of(opportunities)
.filter(Fn.Match
.field(Opportunity.Amount).greaterThan(150000)
.also(Opportunity.AccountId).equals(accountId))
.asList();
List<Task> prospectingOpportunityTasks = SObjectCollection.of(Trigger.new)
.filter(Fn.Match.recordFields(new Opportunity(Stage = 'Prospecting')))
.mapAll(Fn.MapTo(Task.SObjectType)
.setField(Task.Subject, 'Follow up')
.mapField(Task.WhatId, Opportunity.Id))
.asList();
List<Task> largeProspectingOpportunityFollowUpTasks = SObjectCollection.of(Trigger.new)
.filter(Fn.Match.recordFields(new Opportunity(Stage = 'Prospecting')))
.mapSome(
Fn.Match.field(Opportunity.Amount).greaterThan(100000),
Fn.MapTo(Task.SObjectType)
.setField(Task.Subject, 'Follow up')
.mapField(Task.WhatId, Opportunity.Id)
)
.asList();
List<Opportunity> largeOpportunities = SObjectCollection.of(opportunities)
.forEach(Fn.Modify
.setField(Opportunity.Rank__c, 'Excellent')
)
.asList();
SObjectCollection.of(opportunities).forEach(Fn.Debug);
Map<Id, List<Account>> accountsByParentId = SObjectCollection.of(accounts).groupByIds(Account.ParentId);
List<Opportunity> idAndAmountOpportunities = SObjectCollection.of(opportunities)
.pick(new Set<Schema.SObjectField>{Opportunity.Id, Opportunity.Amount})
.asList();
List<Decimal> amounts = SObjectCollection.of(opportunities).pluckDecimals(Opportunity.Amount);
OptionalDecimal average = SObjectCollection.of(opportunities).mapToDecimal(Opportunity.Amount).average();
OptionalDecimal sum = SObjectCollection.of(opportunities).mapToDecimal(Opportunity.Amount).sum();
OptionalDecimal min = SObjectCollection.of(opportunities).mapToDecimal(Opportunity.Amount).min();
OptionalDecimal max = SObjectCollection.of(opportunities).mapToDecimal(Opportunity.Amount).max();
Find more examples in the documentation.