-
Notifications
You must be signed in to change notification settings - Fork 458
AccountServiceLayer
Demonstrates what a Service Layer object might look like for teh Account object. Demonstrates the placement of shared code that is specific to the Account Object, and contains code that is called by the AccountTriggerHandler
Group Shared Code
TESTVISIBLE
private static didExecuteMethod
String
Method exists to demonstrate what it might look like to call a service layer method from a trigger handler.
public static void justWriteALogStatement(String toLog)
Name | Type | Description |
---|---|---|
toLog | String | String to Log. |
void
AccountServiceLayer.justWriteALogStatement('Hello World');
SUPPRESSWARNINGS
Changes the account's Shipping Street field to a hardcoded value. You should do this kind of work in a before trigger, but this is a demo.
Note: This method contains a false-positive PMD viloation around not checking FLS/CRUD before doing DML. This is because PMD is unaware of what the CanTheUser call right before it is doing - namely checking CRUD.
public static void changeShippingStreet(List<Account> accounts, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
accounts | List<Account> | List of account objects to change the shipping street on |
accessLevel | System.AccessLevel |
void
Account[] accounts = [SELECT Name FROM Account LIMIT 50];
AccountServiceLayer.changeShippingStreet(accounts);
System.debug([SELECT Name, ShippingStreet FROM Account WHERE Id in : accounts]);
Increments a counter stored in the Description field. Demonstration method of the kind of work a service layer may do.
public static List<Account> incrementCounterInDescription(List<Account> incomingAccounts, Boolean save)
Name | Type | Description |
---|---|---|
incomingAccounts | List<Account> | List of Account Objects. |
save | Boolean | Boolean determining if DML update is requested. |
List<Account>
Account[] accounts = [SELECT Description FROM Account LIMIT 50];
AccountServiceLayer.incrementCounterInDescription(accounts, true);
System.debug([SELECT Name, Description FROM Account WHERE Id in : accounts]);
TESTVISIBLE
Updates a list of accounts if the user has access to update
the Account Object. Demonstrates simple usage of
Security.stripInacessible()
, and DML with a try/catch block.
private static void safelySave(List<Account> accounts)
Name | Type | Description |
---|---|---|
accounts | List<Account> |
void
Account[] accounts = [SELECT Name FROM Account LIMIT 50];
for(Account acct: accounts){
acct.description = 'safely save example';
}
AccountServiceLayer.safelySave(accounts);
System.debug([SELECT Name, Description FROM Account WHERE Id in : accounts]);
Internal custom exception class