Saturday, April 8, 2023

Static Variables and Static Methods in Apex

Static variables are variables that belong to an overall class, not a particular object of a class. Think of a static variable to be like a global variable – its value is shared across your entire org. Any particular object’s properties or values are completely irrelevant when using static. When a class containing static variables is first referenced, the static variables are initialized. The initialization process occurs only once per execution context, regardless of how many times the class is referenced.

This example illustrates static vs. non-static variables

public class classexmple {
  public Integer count = 0;
  public static Integer Scount = 0;
  public void executeMethod() {
    count = count + 1;
    Scount = Scount + 1;
  }
classexmple obj1 = new classexmple();
classexmple obj2 = new classexmple();
obj1.executeMethod();
obj2.executeMethod();
System.debug(obj1.count); // This is 1
System.debug(obj2.count); // This is 1
// Access static variables only by class name, 
not object name
System.debug(classexmple.Scount); // This is 2

Static methods, similarly, are methods that act globally and not in the context of a particular object of a class. Being global, static methods only have access to its provided inputs and other static (global) variables.

This example illustrates static vs. non-static methods

public class SecondExample {
  public void Method1() {
    // code
  }
  public static String Method2() {
    // code
    String a;
    return a;
  }
  public static String Method3(String param) {
    String a;
    //code
    return a;
  }
}

SecondExample obj = new SecondExample();
// Non-static methods require an object 
for context
obj.method1();
// Static methods aren't related to a 
particular object
// You don't even use an object to call a 
static method!
SecondExample.Method2();
SecondExample.Method3('value');


Let me know your thoughts in the comment section!!

Monday, April 3, 2023

What are Remote Site Settings in Salesforce

Salesforce allows you to access external website resources from a Salesforce application for your organization. You can access these external websites through Visualforce pages, Apex Callout, and via XmlHttpRequest calls. To avoid accessing malicious websites from Salesforce.com, the website first needs to be registered with remote site settings. Once the site is registered, it can be used within Salesforce.com. 

To Setup the Remote Site Setting 

  • search and click remote site settings in setup.site
  • Click on New Remote Site and enter the URL and other fields required.
  • Click Save.

Please find the description of the fields in remote site settings

site

Let me know your thoughts in the comment section!!

Friday, March 31, 2023

Wrapper Class in Salesforce

Wrapper Class in Apex Salesforce

In simple words, a wrapper class is a class inside a class. It is also known as a container class. It stores a group of similar or different data type variables into a single object.

Lets understand with an example:


public class ExampleCls {
    public class WrapperCls{
        public Boolean selected { getset;}
        public String record  { getset;}
        
    }
}

In this example, ExampleCls is the main class and
WrapperCls is the wrapper or the inner class.

This wrapper class contains two different data types,
one Boolean and other one is String

Use of Wrapper Class:

  1. It helps to stores a group of similar or different data type variables into a single object.
  2. We can use a wrapper class in Lightning Component when we need to send group of data from apex to lightning components.
  3. We can use Wrapper classes to parse the JSON request and response in REST API.

Wednesday, March 29, 2023

How to Check Custom Setting is List or Hierarchy in Apex

Today, we will learn how we can check whether the given custom setting is List of Hierarchy.

To Understand custom settings click here.

Apex Code:

String api = 'customSettingName__c';
Type reflector = Type.forName(api);
SObject sobj=(SObject)reflector.newInstance();
DescribeSObjectResult sobjResult = 
sobj.getSObjectType().getDescribe();
//some Name field info
SObjectField field = 
sobjResult.Fields.getMap().get('Name');
DescribeFieldResult fieldResult = 
field.getDescribe();
Integer length = fieldResult.getLength();
system.debug('length-'+length);

If length is 38 that means it is list, if length is 80 that means it is hierarchy


Please share your thoughts on this if there is any other way to do so.

Monday, March 27, 2023

What are Custom Settings in Salesforce

In Salesforce, custom settings are application metadata that offers a reusable set of static data that different departments in your company can access. 

Custom settings are classified into two types: 

  • Hierarchy Custom Settings
  • List Custom Settings 

The data in Hierarchy Custom Settings checks the organization, profile and user settings for the current user and makes the data visible for them accordingly. It uses a built-in hierarchical logic which lets you personalize settings for a specific user or a profile.

The data in List Custom Settings is directly visible to any user in the org. It provides a reusable set of static data which can be accessed across your organization.

Custom settings can be used to store a variety of data, such as configuration settings, feature switches, and business rules. They can be accessed using validation rules, workflows, Apex code, Lightning components, and the Salesforce API.


Advantages of Custom Settings

The main advantage of using Custom Settings is that the data is cached, which enables efficient access without the cost of repeated queries to the database. One doesn’t have to use SOQL queries which count against the governor limits.

Mostly Viewed