Monday, March 30, 2020

Tree Grid in Lightning Web Components(lwc)

HTML Code
<template>
    <lightning-card title="Tree Grid in Lightning Web Components">
        <lightning-tree-grid columns={gridColumns} data={gridData} key-field="Id">
        </lightning-tree-grid>
    </lightning-card>
</template>

Javascript Code
import {
    LightningElement,
    wire
} from 'lwc';
import fetchAccs from '@salesforce/apex/GenericController.returnAccsWithCons';

export default class TreeGridInLWC extends LightningElement {
    gridData;

    gridColumns = [{
            type: 'text',
            fieldName: 'Name',
            label: 'Name'
        },
        {
            type: 'text',
            fieldName: 'Industry',
            label: 'Industry'
        },
        {
            type: 'text',
            fieldName: 'FirstName',
            label: 'FirstName'
        },
        {
            type: 'text',
            fieldName: 'LastName',
            label: 'LastName'

        }
    ];


    @wire(fetchAccs)
    accsWithCons({
        error,
        data
    }) {
        if (data) {
            let parseData = JSON.parse(JSON.stringify(data));

            for (let i = 0; i < parseData.length; i++) {
                parseData[i]._children = parseData[i]['Contacts'];
                delete parseData[i]['Contacts'];
            }

            this.gridData = parseData;
        } else if (error) {
            console.log('error ====> ' + JSON.stringify(error));
        }
    }
}
Apex Class
public inherited sharing class GenericController {
    
    @AuraEnabled(Cacheable=true)
    public static list<Account> returnAccsWithCons(){
        return [SELECT Id, Name, Industry, Type, (SELECT Id, FirstName, LastName FROM Contacts) FROM Account];
    }
}

Output

Wednesday, March 25, 2020

Progress ring/circular in lightning web components(lwc)

HTML Code
<template>
    <lightning-card title="Progress ring in Lightning Web Component"><br/>
        <div>
            <lightning-progress-ring variant="base-autocomplete" value={progress} size="large" class="slds-align_absolute-center"> </lightning-progress-ring>
        </div>
        <div class="slds-text-align--center slds-text-title" style="color:forestgreen;">
            {processStatus}
        </div>
    </lightning-card>
</template>

JS Code
import { LightningElement, track } from 'lwc';

export default class ProgressBarDemoInLWC extends LightningElement {
    progress = 0;
    processStatus = 'In Progress';
    connectedCallback() {
        // eslint-disable-next-line @lwc/lwc/no-async-operation
        this._interval = setInterval(() => {
            this.progress = this.progress + 5;
            this.processStatus = 'Processing => ' + this.progress +'/100';
            if(this.progress === 100) {
                clearInterval(this._interval);
                this.processStatus = 'Completed';
            }
        }, 300);

    }
}

Output

Monday, March 23, 2020

System.CalloutException: Read timed out error in Salesforce

When you are sending the request to other systems sometimes you may face the fatal error like
System.CalloutException: Read timed out error

To resolve the issue increase the timeout limit,
the minimum timeout limit is  1 millisecond
the maximum timeout limit is 120 seconds (2minutes).

The request.setTimeout method accepts the parameter is an integer in milliseconds so pass the value in milliseconds

Ex: 20 sec * 1000  ms / sec = 20,000 ms

Sample Code

HttpRequest httpRequest = new HttpRequest();    
httpRequest.setEndpoint('https://www.salesforcecodecrack.com/');
httpRequest.setTimeout(120000);
httpRequest.setMethod('GET');   
httpRequest.setHeader('Authorization', 'OAuth ' + getSessionId());        
httpRequest.setHeader('Authorization', 'Bearer ' + getSessionId()); 
httpRequest.setHeader('Content-Type', 'application/json');
httpRequest.setHeader('Accept', 'application/json');

Http http = new Http();
HTTPResponse res = http.send(httpRequest);
String strResponse = res.getBody();