Complete Projects
Batch over Contacts missing email or phone → flag Data_Quality__c, collect statistics with Database.Stateful, email a summary in finish, scheduled via Schedulable, errors captured by BatchApexErrorEvent.
41.1 Opportunity discount approval (trigger + service + test)
trigger OpportunityTrigger on Opportunity (before update) {
new OpportunityTriggerHandler().run();
}
public with sharing class OpportunityTriggerHandler extends TriggerHandler {
protected override void beforeUpdate(List<SObject> newList, Map<Id, SObject> oldMap) {
Discount_Rule__mdt rule = Discount_Rule__mdt.getInstance('Max_Without_Approval');
Decimal maxPct = rule?.Percent__c ?? 15;
for (Opportunity o : (List<Opportunity>) newList) {
Opportunity old = (Opportunity) oldMap.get(o.Id);
Boolean discountChanged = o.Discount__c != old.Discount__c;
if (discountChanged && o.Discount__c > maxPct && !FeatureManagement.checkPermission('Approve_Discounts')) {
o.Discount__c.addError('Discounts above ' + maxPct + '% require approval.');
}
}
}
}
@isTest
private class OpportunityDiscountTest {
@isTest
static void blocksLargeDiscountForStandardUser() {
Account a = new Account(Name = 'A');
insert a;
Opportunity o = new Opportunity(Name = 'O', AccountId = a.Id, StageName = 'Prospecting',
CloseDate = Date.today(), Discou
…41.2 Nightly data-quality batch with reporting
Batch over Contacts missing email or phone → flag Data_Quality__c, collect statistics with Database.Stateful, email a summary in finish, scheduled via Schedulable, errors captured by BatchApexErrorEvent.
41.3 External order sync
LWC button → @AuraEnabled controller → OrderSyncService enqueues a Queueable with Database.AllowsCallouts → callout via Named Credential → updates Order.External_Id__c → publishes Order_Synced__e → LWC subscribes with empApi and shows a toast. Finalizer retries on 5xx responses.
41.4 Inbound REST API for a partner portal
@RestResource(urlMapping='/partner/v1/leads/*') with POST (bulk create with partial success response) and GET (status by external ID), OAuth via a Connected/External Client App, user mode enforcement, JSON DTOs, and full tests.
41.5 Configurable routing engine
Custom metadata defines routing rules (criteria → queue); a Strategy-pattern engine evaluates rules for new Cases in a before-insert trigger, with an invocable wrapper so Flow and Agentforce can request routing decisions.