Notes
Utility classes are helper classes that consists of reusable methods. From triggers we can call methods in such public classes. This helps to reduce code with in trigger and make it more structured.
The methods in Utility Class can be used in Test classes as well.
Get the Name of a specific Account by Id
public static String getAccountName(Id acId){
Account ac = [SELECT Id, Name FROM Account WHERE id=:acId];
return ac.Name;
}
Get all the active Accounts
public static List<Account> getActiveAccounts(){
List<Account> activeAccounts = [SELECT Id, Name FROM Account WHERE Active__c ='Yes'];
return activeAccounts;
}
Get all the Inactive Accounts
public static List<Account> getInActiveAccounts(){
List<Account> inactiveAccounts = [SELECT Id, Name FROM Account WHERE Active__c ='No'];
return inactiveAccounts;
}
Get the List of the Account Ids
public static List<Id> getAccountIds(){
List<Account> activeAccounts = [SELECT Id, Name FROM Account];
List<Id> acIds = new List<Id>();
for(Account ac : activeAccounts){
acIds.add(ac.Id);
}
return acIds;
}
Create 200 Accounts
public static void create200Accounts(){
List<Account> acList = new List<Account>();
for(Integer i=1; i<=200; i++){
acList.add(new Account(Name='New Account '+i));
}
}
Create n Accounts
public static void create200Accounts(Integer n){
List<Account> acList = new List<Account>();
for(Integer i=1; i<=n; i++){
acList.add(new Account(Name='New Account '+i));
}
}
Ge all the Accounts
public static List<Account> getAccounts() {
return [SELECT Id, Name, Industry, Phone FROM Account];
}
Get Account by Id
public static Account getAccountById(Id accountId) {
return [SELECT Id, Name, Industry, Phone FROM Account WHERE Id = :accountId];
}
Create an Account with a Parameter
public static void createAccount(Account account) {
insert account;
}
Update an Account with a Parameter
public static void updateAccount(Account account) {
update account;
}
Delete an Account with a Parameter
public static void deleteAccount(Account account) {
delete account;
}
Search Accounts by Name
public static List<Account> searchAccounts(String searchTerm) {
String searchQuery = '%' + searchTerm + '%';
return [SELECT Id, Name, Industry, Phone FROM Account WHERE Name LIKE :searchQuery];
}
Get Accounts with Contacts
public static List<Account> getAccountsWithContacts() {
return [SELECT Id, Name, Industry, Phone FROM Account WHERE Id IN (SELECT AccountId FROM Contact)];
}
Get Accounts without Contacts
public static List<Account> getAccountsWithoutContacts() {
return [SELECT Id, Name, Industry, Phone FROM Account WHERE Id NOT IN (SELECT AccountId FROM Contact)];
}
Get Accounts with Opportunities
public static List<Account> getAccountsWithOpportunities() {
return [SELECT Id, Name, Industry, Phone FROM Account WHERE Id IN (SELECT AccountId FROM Opportunity)];
}
Get Accounts without Opportunities
public static List<Account> getAccountsWithoutOpportunities() {
return [SELECT Id, Name, Industry, Phone FROM Account WHERE Id NOT IN (SELECT AccountId FROM Opportunity)];
}
Get all the Open Cases of an Account
public static List<Case> getOpenCasesByAccount(Account account) {
return [SELECT Id, Subject, Description, Priority FROM Case WHERE AccountId = :account.Id AND IsClosed = false];
}
Delete all the closed Cases of an Account
public static void deleteClosedCasesByAccount(Account account) {
List<Case> closedCases = [SELECT Id FROM Case WHERE AccountId = :account.Id AND IsClosed = true];
delete closedCases;
}
Delete all the Accounts those are Older Than Three Days
public static void deleteAccountsOlderThanThreeDays() {
DateTime threeDaysAgo = System.now().addDays(-3);
List<Account> accountsToDelete = [SELECT Id FROM Account WHERE CreatedDate < :threeDaysAgo];
delete accountsToDelete;
}
Activate an Account
public static void activateAccount(Account account) {
account.Active__c= 'Yes';
update account;
}
Deactivate an Account
public static void deactivateAccount(Account account) {
account.Active__c= 'No';
update account;
}
Create a specific Contact for an Account
public static void createContactForAccount(Account account) {
Contact newContact = new Contact(FirstName = 'John', LastName = 'Doe', AccountId = account.Id, Email = 'johndoe@example.com');
insert newContact;
}
Set Billing Address As Shipping Address
public static void setBillingAddressAsShippingAddress(Account account) {
account.ShippingStreet = account.BillingStreet;
account.ShippingCity = account.BillingCity;
account.ShippingState = account.BillingState;
account.ShippingPostalCode = account.BillingPostalCode;
account.ShippingCountry = account.BillingCountry;
update account;
}
Country Always as United States
public static void normalizeCountry(Account account) {
if (account.Country != null) {
String country = account.Country.toLowerCase();
if (country == 'america' || country == 'usa') {
account.Country = 'United States';
update account;
}
}
}
Generate Random Account Number for an Account
public static void generateAccountNumber(Account account) {
Integer numDigits = 6;
String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
String accNumber = '';
for (Integer i = 0; i < numDigits; i++) {
accNumber += chars.substring(Math.mod(Math.abs(Crypto.getRandomInteger()), chars.length()), Math.mod(Math.abs(Crypto.getRandomInteger()), chars.length() + 1));
}
account.AccountNumber = accNumber;
update account;
}
Activate Account Based on some Conditions
public static void activateHotAccount(Account account) {
if (account.Rating == 'Hot' && account.AnnualRevenue > 1000000) {
account.IsActive = true;
update account;
}
}
Delete Inactive Accounts which are 180 days old
public static void deleteInactiveAccounts() {
Date cutoffDate = Date.today().addDays(-180);
List<Account> accountsToDelete = [SELECT Id FROM Account WHERE IsActive = false AND LastModifiedDate < :cutoffDate];
delete accountsToDelete;
}
Update Related Contact Owners
public static void updateRelatedContactOwners(Account account) {
List<Contact> contactsToUpdate = [SELECT Id FROM Contact WHERE AccountId = :account.Id];
for (Contact c : contactsToUpdate) {
c.OwnerId = account.OwnerId;
}
update contactsToUpdate;
}
Check whether An Account has a Contact
public static Boolean hasContacts(Account account) {
List<Contact> relatedContacts = [SELECT Id FROM Contact WHERE AccountId = :account.Id];
return relatedContacts.size() > 0;
}
Create Phone is Missing Task
public static void createPhoneMissingTask(Account account) {
if (String.isBlank(account.Phone)) {
Task phoneMissingTask = new Task();
phoneMissingTask.Subject = 'Phone is Missing';
phoneMissingTask.Description = 'Please update the phone number for this Account';
phoneMissingTask.Status = 'Not Started';
phoneMissingTask.Priority = 'Normal';
phoneMissingTask.WhoId = account.OwnerId;
phoneMissingTask.WhatId = account.Id;
insert phoneMissingTask;
}
}
Create no Contact Task
public static void createNoContactTask(Account account) {
List<Contact> numContacts = [SELECT Id FROM Contact WHERE AccountId = :account.Id];
if (numContacts.size() == 0) {
Task noContactTask = new Task();
noContactTask.Subject = 'This Account don\'t have contact';
noContactTask.Description = 'Please add at least one Contact to this Account';
noContactTask.Status = 'Not Started';
noContactTask.Priority = 'Normal';
noContactTask.WhoId = account.OwnerId;
noContactTask.WhatId = account.Id;
insert noContactTask;
}
}