Sunday, November 17, 2024

Naming Conventions in Apex: Best Practices for Classes, Methods, Variables, and Comments

 In Apex, the Salesforce platform's programming language, following consistent naming conventions is crucial for writing clean, readable, and maintainable code. Proper naming conventions help developers understand the purpose of variables, methods, and classes without needing to read the entire code. This article outlines best practices for naming Apex classes, methods, variables, and comments, using the Opportunity object as an example. 

1. Naming Apex Classes

Apex class names should be descriptive and camel case. Class names generally represent the entity or the functionality they handle.

Use Pascal Case: Start each word with a capital letter (e.g., MyClassName).

Be Descriptive: The name should clearly explain the purpose of the class. For example, if a class is designed to handle logic related to Opportunities, the name should include "Opportunity".

Example: 

public class OpportunityHandler {

    // Class implementation goes here

}

In this example, the class name OpportunityHandler indicates that this class will likely deal with Opportunity-related logic.

2. Naming Apex Methods

Apex methods (functions) should also be named using camel case. Method names should be action-oriented, clearly indicating what the method does. Use verbs to start method names (e.g., calculateTotalAmount(), updateOpportunityStage()).

Use camel case: The first word should start with a lowercase letter, and each subsequent word should start with an uppercase letter.

Descriptive Names: The method name should describe what action the method performs.

Example:

public class OpportunityHandler {

    // Method to update Opportunity status based on criteria

    public void updateOpportunityStage(Opportunity opp) {

        if (opp.Amount > 100000) {

            opp.StageName = 'Negotiation/Review';

        } else {

            opp.StageName = 'Prospecting';

        }

        update opp;

    }

}

In this example, the method updateOpportunityStage clearly describes its action of updating the Opportunity's stage based on the amount.

3. Naming Variables

Variable names should also follow camel case, but should be more concise while still being descriptive. It is important to choose variable names that are easy to understand and reflect the type of data they hold.

Rules for Variable Naming:

Use camel case: Like methods, variables should start with a lowercase letter.

Descriptive but concise: The variable name should describe what data it holds but should not be overly long.

Avoid abbreviations: Only use common abbreviations that everyone will understand.

Example:

public class OpportunityHandler     {

    public void updateOpportunityStage(Opportunity opp) {

        Decimal oppAmount = opp.Amount;

        

        if (oppAmount > 100000) {

            opp.StageName = 'Negotiation/Review';

        } else {

            opp.StageName = 'Prospecting';

        }

        update opp;

    }

}

Here, the variable oppAmount is a concise and clear name for the Opportunity's amount.

4. Naming Constants

If you use constants in Apex, these should be written in UPPERCASE letters, with words separated by underscores. Constants are values that do not change during the execution of the program, such as a fixed Opportunity stage.

Example: 

public class OpportunityHandler {

    private static final String NEGOTIATION_STAGE = 'Negotiation/Review';

    

    public void updateOpportunityStage(Opportunity opp) {

        if (opp.Amount > 100000) {

            opp.StageName = NEGOTIATION_STAGE;

        } else {

            opp.StageName = 'Prospecting';

        }

        update opp;

    }

}

Here, NEGOTIATION_STAGE is a constant that holds a fixed value for the Opportunity stage.

5. Comments in Apex

Comments in your code are essential for explaining why certain logic is implemented. Use comments to describe the purpose of complex methods, classes, and sections of code.

Types of Comments:

Single-line comments: Use // for comments that only span a single line.

Multi-line comments: Use /* */ for comments that span multiple lines. 

Example:

public class OpportunityHandler {

    // This method updates the Opportunity stage based on the opportunity amount

    public void updateOpportunityStage(Opportunity opp) {

 

        // Check if the Opportunity amount is greater than 100,000

        if (opp.Amount > 100000) {

            opp.StageName = 'Negotiation/Review';  // Update stage to Negotiation

        } else {

            opp.StageName = 'Prospecting';  // Otherwise, set stage to Prospecting

        }

        update opp;  // Update the Opportunity record in Salesforce

    }

}

In this example, the comments explain the purpose of each code block and provide clarity for other developers working with the code.

Here's a recap of the key points:

Classes: Use Pascal case and descriptive names, e.g., OpportunityHandler.

Methods: Use camel case and action-oriented names, e.g., updateOpportunityStage.

Variables: Use camel case, descriptive but concise names, e.g., oppAmount.

Constants: Use uppercase letters and underscores for constant values, e.g., NEGOTIATION_STAGE.

Comments: Write clear and concise comments to explain the logic behind the code.

Friday, November 1, 2024

Profiles are retiring soon by Spring 2026 release! What Next?

Salesforce has officially announced that profiles will no longer serve as the primary method for managing permissions, with their retirement slated for Spring 2026.

While profiles will still exist, their functionality will be scaled back to basics, such as login hours and default settings. When setting up a user, profiles will still be required, but their role will be limited to managing things like:

  • Login hours and IP ranges
  • Record types and default apps
  • Page Layout Assignment will be still managed by profiles (Salesforce won't move this to permission sets as the future is App builder/Dynamic forms.

What's Changing?

  1. Key Permissions Shift to Permission Sets:
    Permissions like CRUD, field-level security, app access, and more will now be managed through permission sets.

  2. Enhanced Flexibility with Layered Permissions:
    You’ll be able to stack multiple permission sets and use permission set groups, providing greater flexibility for user access management.

  3. Simplified Administration:
    This transition reduces complexity, especially for large organizations with an overabundance of profiles.

Although this marks a major shift, it’s intended to streamline user management and simplify life for administrators in the long run.

Get Ahead of the Change:

Start planning your migration to permission sets today to ensure a smooth transition.

Saturday, October 26, 2024

What is Change Data Capture in Salesforce?

Change data capture is a streaming product also called CDC helps you to integrate your Salesforce data with external systems in real time for operation such as creation of a new record, updates to an existing record, deletion of a record, and undeleting of a record. This is very helpful in cases where we need to keep external system in sync with Salesforce in real time instead of doing periodic exports and Imports of data or repeated API calls.

 Availability for Standard and Custom objects for CDC and there syntax for usage with CDC :

 Change events are available for all custom objects defined in your Salesforce org and a subset of standard objects. A ChangeEvent object is available for each object that supports Change Data Capture. A change event isn’t a Salesforce object. It doesn’t support CRUD operations or queries. It’s included in the object reference so you can discover which Salesforce objects support change events. The name of a change event is based on the name of the corresponding object for which it captures the changes.

 Standard Object Change Event Name: <Standard_Object_Name>ChangeEvent

 As an example for Account we will use AccountChangeEvent.

 Custom Object Change Event Name: <Custom_Object_Name>__ChangeEvent

 As an example, for MyCustomObject__c we will use MyCustomObject__ChangeEvent.

 Security with Change Data Capture:

 Change Data Capture ignores sharing settings and sends change events for all records of a Salesforce object. Using Change Data Capture we can capture field changes for all records, CDC Deliver only the fields a user has access to based on field-level security. The fields that a change event doesn’t include are:

 The IsDeleted system field.

The SystemModStamp system field.

Any field whose value isn’t on the record and is derived from another record or from a formula, except roll-up summary fields, which are included.

Sample event message in JSON format for a new account record creation:

This example is an event message in JSON format for a new account record creation.

{

  "schema": "IeRuaY6cbI_HsV8Rv1Mc5g",

  "payload": {

    "ChangeEventHeader": {

      "entityName": "Account",

      "recordIds": [

        "<record_ID>"

      ],

      "changeType": "CREATE",

      "changeOrigin": "com/salesforce/api/soap/51.0;client=SfdcInternalAPI/",

      "transactionKey": "0002343d-9d90-e395-ed20-cf416ba652ad",

      "sequenceNumber": 1,

      "commitTimestamp": 1612912679000,

      "commitNumber": 10716283339728,

      "commitUser": "<User_ID>"

    },

    "Name": "Acme",

    "Description": "Everyone is talking about the cloud. But what does it mean?",

    "OwnerId": "<Owner_ID>",

    "CreatedDate": "2021-02-09T23:17:59Z",

    "CreatedById": "<User_ID>",

    "LastModifiedDate": "2021-02-09T23:17:59Z",

    "LastModifiedById": "<User_ID>"

  },

  "event": {

    "replayId": 6

  }

}

How to enable Change Data Capture?

To receive notifications on the default standard channel for record changes, select the custom objects and supported standard objects that you’re interested in on the Change Data Capture page.

From Setup, in the Quick Find box, enter Change Data Capture, and click Change Data Capture. The Available Entities list shows the objects available in your Salesforce org for Change Data Capture, You can select up to five entities, including standard and custom objects. To enable more entities, contact your Salesforce Account Representative to purchase an add-on license. The add-on license removes the limit on the number of entities you can select. Also, it increases the event delivery allocation for CometD and Pub/Sub API clients. With the add-on license, you can select up to 10 entities at a time in the Available Entities list. After selecting the first 10 entities, you can add more.

What is CDC in salesforce?

Note: You can also create a custom channel if you have multiple subscribers and each subscriber receives change events from a different set of entities. Also, use a custom channel with event enrichment to isolate sending enriched fields in change events on a specific channel. Custom channels group and isolate change events for each subscriber so subscribers receive only the types of events they need.

Subscribe to Change Events:

 You can subscribe to change events with CometD, Pub/Sub API, or Apex triggers.

 Let us try to understand with an example,

 Now, to view the CDC, open the workbench à Open queries tab à Streaming Push Topics.

 Click Generic Subscriptions à and type /Data/AccountChangeEvent and click Subscribe.

 The connection is established now as you can see in below image.

 

How to use Change Data Capture in Salesforce

As we have setup CDC for Account above. Let us try to update an account record and click Save.

Change Data Capture in Salesforce

Now, go back to workbench. You will be able to see the notification as shown below.

How to use Change Data Capture in Salesforce

Sunday, September 22, 2024

What are events in LWC? How Communications happens in LWC?

In Lightning Web Components (LWC), events are used for communication between components, particularly for passing information or triggering actions. 

In Lightning Web Components (LWC), there are three primary approaches for communication between components using events:

  1. Parent-to-Child Communication: This allows a parent component to pass data or trigger actions in a child component via public properties or methods.

  2. Child-to-Parent Communication (Custom Events): Child components can send data or trigger actions in their parent component by dispatching custom events.

  3. Publish-Subscribe Pattern (LMS): This pattern enables communication between two components that do not have a direct parent-child relationship, using the Lightning Message Service (LMS) to broadcast and subscribe to events across the application. 

Event Phase in LWC:

Lightning web components use only the bubbling phase. Dispatching events or adding listeners to the capture phase isn't supported. Simply think of the event’s path as starting with your component and then moving to its parent, and then grandparent, and so on.

Parent-to-Child Communication:

In Lightning Web Components, data can be passed from a parent to a child component using the following two methods:

1. Public Properties: These are reactive properties defined in the child component that the parent component can set directly.


2. Public Methods: These are methods in the child component that the parent component can invoke to pass data or trigger specific behavior.


Child-to-Parent Communication (Custom Events)

Child to Parent communication is achieved by triggering custom event from child LWC and handling it parent LWC as explained in blog: https://www.sfdc-lightning.com/2020/01/how-to-pass-data-from-child-component-to-parent-component-in-lightning-web-component.html

We will see the use of Publish-Subscribe Pattern (LMS) in our next blog.

Monday, September 16, 2024

What is Shadow DOM in LWC?

In Lightning Web Components (LWC), encapsulation refers to the isolation of a component’s internal structure, styles, and behavior from the rest of the application. This is primarily achieved using the Shadow DOM, which ensures that the component’s logic remains self-contained and unaffected by external factors, allowing it to function independently.The styles defined inside a component are scoped to that component and do not affect other components or the global application.The component’s internal DOM is hidden from the outside world, ensuring that its structure and behavior cannot be directly manipulated by external code.

In the below example, Shadow DOM in Lightning Web Components (LWC) is used to encapsulate the styles and DOM structure of the parent and child components, preventing style leakage between them:

The parent component (`ParentShadowDOM`) defines a `<p>` tag with blue text and large font, which applies only to the parent due to Shadow DOM encapsulation.

The child component (`childLWCComp`) has its own `<p>` tag styled with red text and small font.

Since both components use Shadow DOM, the styles of the parent component do not affect the child component, and vice versa, ensuring complete isolation of their DOM structure and styles. This maintains a clear separation between the two components, allowing each to have its own independent styling and behavior.

parentShadowDOM.html

<template>

    <div><p>I am parent comp</p></div>

    <div>

  <c-child-lwc-comp></c-child-lwc-comp>

    </div>

</template>

parentShadowDOM.js

import { LightningElement } from 'lwc';

export default class ParentShadowDOM extends LightningElement {}

parentShadowDOM.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">

    <apiVersion>61.0</apiVersion>

    <isExposed>false</isExposed>

</LightningComponentBundle>

parentShadowDOM.css

p{

    color: blue;

    font-size: large;

}

childLWCComp.html

<template>

    <div><p>I am child comp</p></div>

</template>

childLWCComp.js

import { LightningElement } from 'lwc';


export default class ChildLwcComp extends LightningElement {}

childLWCComp.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">

    <apiVersion>61.0</apiVersion>

    <isExposed>false</isExposed>

</LightningComponentBundle>

childLWCComp.css

p{

    color: red;

    font-size: small;

}

Let us now preview this component for testing by adding it in Aura App as shown below.

<aura:application>

<c:parentShadowDOM></c:parentShadowDOM>

</aura:application>


What is Shadow DOM in LWC?