Friday, January 24, 2020

How to get All parent objects and child objects related to the current object

Assume Opportunity is the current object, you want to know all parent object and child objects info of the current object.

All Parent Objects

Sample Code
for(Schema.SobjectField iterator: Opportunity.SobjectType.getDescribe().fields.getMap().Values()) {
    if(iterator.getDescribe().getType() == Schema.DisplayType.REFERENCE) {
        system.debug('Parent Objects ====> '+iterator.getDescribe().getReferenceTo());
    } 
}

Output


All Child Objects

Sample Code
Schema.DescribeSObjectResult sObjectInfo = Opportunity.SObjectType.getDescribe();
for (Schema.ChildRelationship iterator: sObjectInfo.getChildRelationships()) {
  system.debug('Child Objects ====> '+iterator.getChildSObject());
}

Monday, January 20, 2020

Using Wrapper Class in Lighting Web Components(lwc)

What is a Wrapper Class?
A Wrapper class is a class whose instances are a collection of other objects.
OR
A wrapper or container class is a class, a data structure, or an abstract data type which contains different objects or collection of objects as its members.

This post explains how to use wrapper class properties in Lighting Web Components
in this demo, I am displaying Account and related Contacts using Wrapper Class.


HTML Code
<template>
    <lightning-card title="Wrapper Class in Lightning Web Components">

        <template if:true={data}>
                <table class="slds-table slds-table_bordered slds-table_cell-buffer slds-table_col-bordered">
                    <thead>
                        <tr class="slds-text-title_caps">
                            <th scope="col">
                                <div title="Key">Account Name</div>
                            </th>
                            <th scope="col">
                                <div title="Value">Account Related Contacts</div>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <template for:each={data} for:item="keyValue">
                            <tr key={keyValue.Id}>
                                <th scope="col">
                                    <div>{keyValue.objAcc.Name}</div>
                                </th>
                                <th scope="col">
                                    <template for:each={keyValue.lstCons} for:item="con">
                                        <div key={con.Id}>{con.Name}</div>
                                    </template>
                                </th>
                            </tr>
                        </template>
                    </tbody>
                </table>
        </template>
</template>

JS Code
import { LightningElement, wire } from 'lwc';
import AccAndCons from '@salesforce/apex/LWCExampleController.fetchAccAndCons';

export default class WrapperClassInLWC extends LightningElement {
    data;
    error;


    @wire(AccAndCons)
    accs({error, data}) {
        if(data) {
            this.data = data;
            window.console.log('data ==> '+JSON.stringify(data));
        }
        else if(error) {
            this.error = error;
        }
    }
}

Apex Class
public inherited sharing class LWCExampleController {

    @AuraEnabled(Cacheable = true)
    public static List<WrapperDemo> fetchAccAndCons() {
        List<WrapperDemo> lstWrapper = new List<WrapperDemo>();

        for(Account acIterator : [ SELECT Id, Name, (Select Id, Name From Contacts) FROM Account WHERE Id = '001B000000vZWOHIA4'] ) {
            lstWrapper.add(new WrapperDemo(acIterator, acIterator.Contacts));
        }

        return lstWrapper;
        
    }
 
 // Wrapper Class
 public class WrapperDemo {
        @AuraEnabled public Account objAcc;
        @AuraEnabled public list<Contact> lstCons;

        public WrapperDemo(Account acc, list<Contact> lstCons) {
            this.objAcc = acc;
            this.lstCons = lstCons;
        }
    }
}

Output

Thursday, January 9, 2020

How to abort the Scheduled batch job using Apex in Salesforce

The Scheduled batch job information internally stored in CornTrigger object in salesforce.

Ex:  let as assume you scheduled the batch job using System.schedule(jobName, cronExp, schedulable) method it returns the CornTrigger record Id.

String strCornExp = '0 38 * * * ?';
Id cornId = System.schedule('Test Job', strCornExp, new ScheduleJob());

Query the information based on record Id and use the System.abortJob(cornTriggerId); method to abort the scheduled job.

CronTrigger obj = [SELECT Id, CronJobDetail.Name, CronJobDetail.Id,State FROM CronTrigger where CronJobDetail.Name = 'Test Job'];
System.abortJob(obj.Id);

Sunday, January 5, 2020

How to get Salesforce Base URL in Lightning Web Component(lwc)

To get Salesforce base URL use the window.location object can be used to get the current page address (URL).
The window.location.origin property returns the origin URL of the current page.



Ex URL: https://lwcpractice1-dev-ed.lightning.force.com/lightning/r/Account/001B000000vZWOHIA4/view

HTML File
<template>
    <lightning-card title="Salesforce Base URL in Lighting Web Component">
        <div class="slds-text-heading_medium slds-text-align-center">
            SFDC Base URL : <p></p><lightning-formatted-url value={sfdcBaseURL} ></lightning-formatted-url></p>
        </div>
    </lightning-card>
</template>

JS File
import { LightningElement } from 'lwc';

export default class BaseURL extends LightningElement {
    sfdcBaseURL;

    renderedCallback() {
        this.sfdcBaseURL = window.location.origin;
    }
}

Output