Strategy pattern in Salesforce Apex — SS Blog

Study Section
2 min readMar 30, 2022
Strategy pattern in Salesforce Apex — SS Blog
Strategy pattern in Salesforce Apex — SS Blog

In the Strategy pattern, we implement multiple solutions for the same problem and solutions can be selected at run time depending on the condition.
In Apex, we can use custom metadata or custom settings to define these conditions.
Let’s understand this pattern by jumping to a solution via sudo-code.
Suppose you have multiple URLs and you need to use the appropriate URL at run time. We can configure this condition using Custom Metadata or custom Setting. for example. if we want to choose a URL depending on the profile we can define that in Metadata or Custom Setting.
In this example, we will get 2 URLs and use them at run time depending on the condition.
Public Class RedirectTo{
public String url;
public RedirectTo(){
Id profileId= userinfo.getProfileId();
String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
if(profileName.equals('System Administrator')){
url = URLs.getSystemAdminURL();
}
else{
url = URLs.getNonSystemAdminURL();
}
}
}
Public class URLs{
public static String getSystemAdminURL(){
//Code to genrate URL
}
public static String getNonSystemAdminURL(){
//Code to genrate URL
}
}

jQuery presents a tree-like structure of all the elements on a webpage simplifying the syntax and further manipulating such elements. The jQuery Certification exam by StudySection will secure your fundamental knowledge and a basic understanding of jQuery as an asset to improve your skills.

Originally published at https://studysection.com on March 30, 2022.

--

--

Study Section

The most loved online platform for eCertification in several subjects including but not limited to Software Development, Aptitude and more.