Sunday, March 26, 2023

How to Hide a Div Tag Like a Toast Message in Lightning Web Components

Lets understand how we can hide a div like a toast message.

HTML File:

<div data-id="overview" class="red-bg">
  <lightning-formatted-text value={errorMsg}>
</lightning-formatted-text>
</div>

In this HTML file, we are creating a div element and will set a data-id

Data-id value will be used in the JS file to show or hide the div.

Js File:

We can call this hidemsg() method from the success of a server call.

This above div tag will be hidden after 5 secs

hidemsg(){
 setTimeout(() => {
this.template.querySelector('[data-id="overview"]').style.display 
='none';
  }, "5000")
}

When this hidemsg() is called, we can add setTimeout()

method will put a delay.

Saturday, March 25, 2023

Retry Mechanism Design in Salesforce

Lets understand a scenario to implement the Retry Mechanism in Salesforce.

Scenario:

There is a third party system which sends data to Salesforce and Salesforce processes that data and updates the objects. If there is a failure in update/insert then Salesforce system should retry to process them again. So, in such cases we can design the solution using the stage object.

Objects: 

Consider we want to do insert/update on Account based on the data received. For that, we can create a staging object lets say Accountstaging__c which will have fields similar to the Account object.

Design:

Using the standard APIs, third party system can send data to a staging table. There will no validation put in place when the data is being added to the staging object. This is done to assure that all the data reaches Salesforce system without any failure.

Salesforce then picks the data using a trigger or batch and performs the business logic and finally stores the data into Account Object.

Bulk Data Load to Salesforce:

This means when the other systems want to send data in bulk. In that case, we need to process all and update account object accordingly.

Lets say we are about to receive 10,000 records. We can discuss with the other system team and ask them to send a unique id for each bulk load and two extra records. One record will tell us that bulk data load has started to Salesforce and other record will tell that all the data has been sent. After this Salesforce can start its processing.

Record will come in this manner:

Begin of File(bof)-->Actual 10,000 records-->End of File(eof)

Here bof and eof are the checkboxes fields on the account stage object.

Data Processing:

We can have a trigger on stage object which will check whether the eof is true. If yes, then call the batch and query all the records using that unique id and process the data. At the end, we can mark the records in staging object as success or failed.

Retry Mechanism:

Consider some failures when doing the bulk updates in the system. We will have the list of failed records by querying all the failed records for that batch id.
These failed records can be retried using time based Process Builder to call the same batch class with a time gap or can call the batch class immediately by passing the list of records.

So based on the retry processing, we can update the stage records status as success or failure.

This mechanism can be done multiple times on the failed records if we add an integer field called retry counter on staging object. This field will be incremented by 1 whenever there is a failure. While doing the retry we can check whether the retry counter is less than 3, then only get the records. In this way the failed records can be retried multiple times.


If you have any thoughts on this, lets connect and discuss more this.








How to Query Salesforce Objects from External System Without Code

Sometimes, external system wants to get the latest data from Salesforce to run the business logic in their system. In those cases, external system can do a simple query Salesforce objects with the help of Salesforce Standard APIs. There is no code required from a Salesforce developer

Lets understand this concept with an example.

Consider Workbench as a external system. To query salesforce objects in workbench we need to follow the below pattern:

 /services/data/v20.0/query?q=SELECT+Id+,+Name+FROM+USER

where every element is concatenated by + operator

After the query is added click on Execute and once the response comes then click on Show Raw Data. It will open up the blue screen on the right.

workbench


  • totalsize attribute is the number of records returned.
  • At a time workbench can display 2000 records under raw response.
  • If the number of records returned are more than 2000 then NextRecordsUrl will be used to get next set of 2000 records.
  • Done Attribute under raw response tells whether we have reached the last page or not. 
  • If done = false then we need to NextRecordUrl to get next records.
  • If done = false, that mean there are no more records after this.

We can also write where clause in the query
SELECT+Id+,+Name+FROM+USER+where+id+=+'+00500xx+'
Similarly, we can add any filter in the query and get back the response.


Let me know your thoughts on this and we can discuss more.

Call Approval Process on Button Click in Salesforce LWC

In some scenarios, we may need to call the standard approval process on click of a custom button. So in that case, we can have a lightning quick action in LWC similar to one in Aura components.

Step 1:

Create an action for an object. Lets say Opportunity. We need to specify the LWC component name. for create a LWC component and enter that name in Lightning Web Component field.

approval


Step 2:

Add that action in the required page layout in the section as shown below:

approval


So the configurations are done...


Step 3:

Let's do the code part

Open that already created LWC component and add this code.

LWC html file:

<template>
    <lightning-quick-action-panel header="Submit for Approval">
        <lightning-textarea label="Comments" value={commentsonchange={commentChange
                                class="textAreaBody">
        </lightning-textarea>
        <div slot="footer">
            <lightning-button variant="neutral" class="slds-m-left_x-small" label="Cancel" 
                                onclick={closeAction}>
            </lightning-button>
            <lightning-button variant="brand" class="slds-m-left_x-small" label="Submit" 
                                onclick={handleSubmitClick>
            </lightning-button>
        </div>
    </lightning-quick-action-panel>
</template>

------------------------------------------------

LWC Js file:

import { LightningElement,wire,track,api } from 'lwc';
import submitApproval from '@salesforce/apex/OppApproval.submitApproval';
import { CloseActionScreenEvent } from "lightning/actions";
import { CurrentPageReference } from 'lightning/navigation';

export default class OppApprovalLWC extends LightningElement {
@track comments;
@api recordId;

closeAction() {
    this.dispatchEvent(new CloseActionScreenEvent());
    }
@wire(CurrentPageReference)
    getStateParameters(currentPageReference) {
        if (currentPageReference) {
            this.recordId = currentPageReference.state.recordId;
        }
    }
commentChange(event) {
        this.comments = event.detail.value;
    }

handleSubmitClick(event){

        submitApproval({ oppId: this.recordId, comments: this.comments})
        .then(result =>{
            eval("$A.get('e.force:refreshView').fire();");
            this.dispatchEvent(new CloseActionScreenEvent());
        })
        .catch(error =>{
            
        });
        
    }
}

-------------------------------------------------

LWC Meta XML file:

<?xml version="1.0"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>54.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordPage</target>
        <target>lightning__RecordAction</target>
    </targets>
    <targetConfigs>
        <targetConfig targets="lightning__RecordAction">
        <actionType>ScreenAction</actionType>
        </targetConfig>
    </targetConfigs>
</LightningComponentBundle>


-------------------------------------------------

Apex Class:


public class OppApproval {

 @auraEnabled

    public static void submitApproval(String oppId,String comments){
        try{
            Approval.ProcessSubmitRequest approvalRequest = new Approval.ProcessSubmitRequest();                
            approvalRequest.setObjectId(OppId);
            approvalRequest.setComments(comments);
     approvalRequest.setSkipEntryCriteria(true);
                Approval.ProcessResult approvalResult = Approval.process(approvalRequest);
                if(approvalResult .isSuccess()){
                    system.debug('success-'+approvalResult .isSuccess());
                }
                else{
                    system.debug('failed-'+approvalResult .isSuccess());
                }
            
        }
        catch (Exception e) {
            
            String errorMsg = e.getMessage();
            throw new AuraHandledException(ErrorMsg);
        }
        
    }
}

Salesforce Integration Design Patterns

Here are some of the integration patterns available in Salesforce:

1. Request-Reply: In this pattern, Salesforce sends a request to another system, and the other system responds with a reply. This pattern is useful when you need to get data from another system, or when you need to update data in another system.

2. Fire & Forget: Salesforce invokes a process in a remote system but doesn’t wait for completion of the process. Instead, the remote process receives and acknowledges the request and then hands off control back to Salesforce.

2. Batch Data Synchronization: In this pattern, data is exchanged between two systems periodically in batches. This is useful when large volumes of data need to be transferred between systems.

3. Data Virtualization: Salesforce accesses external data in real time. This removes the need to persist data in Salesforce and then reconcile the data between Salesforce and the external system

4. Middleware Integration: This pattern involves using middleware such as MuleSoft or Dell Boomi to connect systems. Middleware can help to manage the complexity of integrations by providing features such as data transformation, routing, and error handling.

5. Point-to-Point: This pattern is used when you need to connect Salesforce with a single system. Point-to-point integration can be simple to set up and maintain, but it can become complex if you need to connect Salesforce with multiple systems. 

Overall, the integration pattern you choose will depend on the complexity of the integration, the volume of data being transferred, and the real-time requirements of your business processes.

These patterns can be used individually or in combination to create a comprehensive integration strategy for your organization. The choice of pattern will depend on the specific requirements of your integration project.


Let me know your thoughts in the comment section!!

Mostly Viewed