Sunday, 19 April 2020

Platform Events in Salesforce


Platform Event is based on Event-Driven Architecture which enable apps to communicate inside and outside of Salesforce. Platform events are based on the publish/subscribe model and work directly with a message bus which handles the queue of incoming events and processes listening for them. This is built in real time integration patterns in the Salesforce Platform which helps to reduce point-to-point integration

Here is some terminology we should remember :-
  • Event : A change in state that is meaningful in a business process.
  • Event message / Notification : A message that contains data about the event.
  • Event producer : The publisher of an event message over a channel.
  • Channel : A conduit in which an event producer transmits a message. Event consumers subscribe to the channel to receive messages. Also referred to as event bus in Salesforce.
  • Event consumer : A subscriber to a channel that receives messages from the channel.

Understanding of Platform Event

  • SObject like Salesforce Entity
    • Suffixed with __e
    • ReplayId fir replaying specific event
    • Only Checkbox, Date, Date/Time , Number , Text and Text Area field available.
  • Pub / Sub based communication 
    • No Polling required.
  • Heterogeneous playloads
    • Define events with different playloads

Difference between SObject and Platform Events


SObjects__c
Platform_Events__e
DMLs (Insert, Update, Delete)
Publish (Insert only)
SOQL
Streaming API
Triggers
Subscribers
Parallel context execution
Guaranteed order of execution

Considerations :-

  1. Platform event is appended with__e suffix for API name of the event.
  2. You can not query Platform events through SOQL or SOSL.
  3. You can not use Platform in reports, list views, and search. Platform events don’t have an associated tab
  4. Published platform events can’t be rolled back.
  5. All platform event fields are read-only by default
  6. Only after insert Triggers Are Supported
  7. You can access platform events both through API and declaratively
  8. You can control platform events though Profiles and permissions

Publishing / Subscribing Platform Events



Publish Platform Events :


We can publish the platform events in 3 ways:
  1. Publish Events Messaging using APEX.
    List<Order_Shipping__e> orderShippingList = new List<Order_Shipping__e>();
    Order_Shipping__e orderShipping = new Order_Shipping__e( Order_Number__c='12345', status__c=1 );
    orderShippingList.add(orderShipping);

    List<Database.SaveResult> results = EventBus.publish(newsEventList);

    for (Database.SaveResult sr : results) {
        if (sr.isSuccess()) {
            System.debug('Successfully published event.');
        } else {
            for(Database.Error err : sr.getErrors()) {
                System.debug('Error returned: ' + err.getStatusCode() );
            }
        }
    }
  2. Publish Events Messaging using Declarative tools 
    1. Process Builder 
    2. Cloud Flow Designer Tool / Visual Work flow
  3. Publish Events Messaging using Salesforce API from external app.

Subscribing Platform Events :


We can subscribe the platform events with following ways :
  1. Apex Trigger : Write an “after insert” Apex trigger on the event object to subscribe to incoming events.Triggers receive event notifications from various sources—whether they’re published through Apex or APIs.

    Trigger OrderShippingTrigger on Order_Shipping__e (after Insert) {
      
    }
  2. Subscrbe to platform event notification in Lightning components
    1. Lightning web components :  Use the empApi methods in your Lightning web component, import the methods from the lightning/empApi module as follows

      import { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled }
          from 'lightning/empApi';
    2. Subscibe in an Aura Component : Use the empApi methods in your Aura component, add the lightning:empApi component inside your custom component and assign an aura:id attribute to it
      <lightning:empApi aura:id="empApi"/>
  3. In an external app, you subscribe to events using CometD as well.
  4. Flow and process builder. Check this post.
Please check below two Apex Hours sessions to learn more about platform event.
  1. Platform Event basics
  2. Overcome Salesforce Governor Limits Using Platform Events
 

Further Learning



Thanks
Amit Chaudhary

Thursday, 2 April 2020

Lightning Web Components Naming Convention

In this post we will talk about Lightning Web Components Naming Convention rules. We will also talk about example of PascalCase, camelCase or kebab-case and how those naming convention rules works in LWC.

Components Bundles

Lightning web component bundles include all below files by default. Its also have some Optional component like CSS, SVG Icon.




Lets see what is the naming convention :

Naming Convention / Rules for Components Bundles

  • Component name Must begin with a lowercase letter
  • Name must contain only alphanumeric or underscore characters
    • Can’t contain a hyphen (dash)
  • Must be unique in the namespace (No other Aura or LWC can have this name)
  • Can’t include whitespace
  • Can’t end with an underscore
  • Can’t contain two consecutive underscores
  • Folder and files names must have same “prefix name”.

HTML File


Use camel case to name your component and use kebab-case to reference a component in the markup. For example



JavaScript File


Java Script Class name should be in PascalCase like below example :

CSS File


  • Needs to be created in the component bundle
  • Has the same name as the component
  • Uses standard CSS syntax
  • Unlike Aura, no need for .THIS

What is the different between camelCase, PascalCase and kebab-case ?


camelCase : Each word in the middle of the respective phrase begins with a capital letter. for example apexHours.
PascalCase: It is same like Camel Case where first letter always is capitalized. for example ApexHours. 
kebab-case: Respective phrase will be transferred to all lowercase with hyphen(-) separating words. for example apex-hours.

Case Name camelCase PascalCase kebab-case
Example helloWorld HelloWorld hello-world
Where to use Folder
File Names
Property name in JS
Class in Java Script Component reference in markup
HTML attribure name

 
 
Please check below post on Lightning Web Components:-

Thanks.
Amit Chaudhary