Wednesday, May 15, 2019

System Mode and User Mode in Salesforce


System Mode:

1.  System mode running apex code by ignoring user’s permissions.
2.  In system mode, Apex code has access to all objects and fields permissions, field-level security,      sharing rules aren’t applied for the current user. 

User Mode:

1. User mode running apex code by respecting user’s permissions and sharing of records.
2. Profile level permissions, field-level security, and sharing rules are applied for the current user.

Modes Of Execution in Salesforce

 Apex Class     System Mode
Triggers1. If triggers call an apex class with sharing keyword, Then record level access permissions will be considered.
Anonymous Apex     User Mode
Apex Webservices (SOAP API and REST API)    System Mode
Validation Rule    System Mode
Workflow Rule    System Mode
Process Builder    System Mode
Formula Fields    System Mode
Custom Button    System Mode
Roll Up Summary Fileds     System Mode
Auto Response Rule    System Mode
Assignment Rule    System Mode
Escalation Rule    System Mode
Approval Process    System Mode
FlowsBy Default runs in User Mode
1. if flow called from Process Builder it runs in System      Mode.
2. if flow called from Workflow it runs in System              Mode.
3. if flow called from Apex it runs depends on With or With Out Sharing of apex class


Test Class1. Test Method with System.runAs() runs in User Mode.
2. Test Method without System.runAs() runs                  in System Mode.
All Type of Jobs   System Mode
Email Service   User Mode
Visualforce PageStandard Controller --- User Mode
Standard Controller With Extensions --- System Mode
Custom Controller---- Based Sharing Keyword Used in Apex Class.

Visualforce ComponentIt depends on the Visualforce page where it is used.
Chatter in Apex User Mode
Macros System Mode
InvocableMethod1. if it is called from Flow it will run in User Mode.
2. if it is called from Process Builder (does it depends on with or without sharing is specified on that Class), then runs in System Mode
Publisher Action System Mode
Custom Button System Mode


Saturday, May 4, 2019

How to iterate map of list values in Lightning web components(lwc)

This post explains how to iterate map of list values in Lightning web components(lwc)

Demo:

Apex Class:
public inherited sharing class LWCExampleController {
    @AuraEnabled(cacheable=true)    
    public static map<String, list<String>> returnMapOfListValues() {
        map<String, list<String>> mapOfListValues = new map<String, list<String>>();
        
        mapOfListValues.put('India', new list<String>{'Hyderabad', 'Delhi', 'Mumbaie'});
        mapOfListValues.put('USA', new list<String>{'New York', 'Las Vegas', 'Miami'});
        mapOfListValues.put('China', new list<String>{'Shenzhen', 'Guangzhou', 'Shanghai'});
        return mapOfListValues;
    }
}


showMapOfListValuesDemo.html
<template>
    <lightning-card title="Iterate Map Values Demo">
        <template if:true={mapOfListValues}>
            <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">Country Name (Key)</div>
                        </th>
                        <th scope="col">
                            <div title="Value">Popular City Names (List Values)</div>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <template for:each={mapOfListValues} for:item="keyValue">
                        <tr key={keyValue.key}>
                            <th scope="col">
                                <div>{keyValue.key}</div>
                            </th>
                            <template for:each={keyValue.value} for:item="value">
                                <div key={value}>
                                    <b>{value}</b><br/>
                                </div>
                            </template>
                        </tr>
                    </template>
                </tbody>
            </table>
        </template>
    </lightning-card>
</template>

showMapOfListValuesDemo.js
import { LightningElement, track, wire } from 'lwc';
import getMapOfData from '@salesforce/apex/LWCExampleController.returnMapOfListValues';

export default class ShowMapOfListValuesDemo extends LightningElement {
    @track mapOfListValues = [];

    @wire(getMapOfData)
    mapOfData({data, error}) {
        if(data) {
            for(let key in data) {
                // Preventing unexcepted data
                if (data.hasOwnProperty(key)) { // Filtering the data in the loop
                    this.mapOfListValues.push({key: key, value: data[key]});
                }
            }
        }
        else if(error) {
            window.console.log(error);
        }
    }
}

showMapOfListValuesDemo.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="showMapOfListValuesDemo">
    <apiVersion>45.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>

Result:

How to iterate map values in Lightning Web Components(lwc)

This post explains how to iterate map values in Lightning web components(lwc)

Demo:

Apex Class
public inherited sharing class LWCExampleController {
    @AuraEnabled(cacheable=true)    
    public static map<String, String> returnMapOfValues() {
        map<String, String> mapOfValues = new map<String, String>();
        mapOfValues.put('Telanagana', 'Hyderabad');
        mapOfValues.put('Arunachal Pradesh', 'Itanagar');
        mapOfValues.put('Assam', 'Dispur');
        mapOfValues.put('Bihar', 'Patna');
        mapOfValues.put('Gujarat', 'Gandhinagar');
        return mapOfValues;
    }
}
showMapLWCDemo.html
<template>
    <lightning-card title="Iterate Map Values Demo">
        <template if:true={mapOfValues}>
            <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">State Name (Key)</div>
                        </th>
                        <th scope="col">
                            <div title="Value">Capital City Name (Value)</div>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <template for:each={mapOfValues} for:item="keyValue">
                        <tr key={keyValue.key}>
                            <th scope="col">
                                <div>{keyValue.key}</div>
                            </th>
                            <th scope="col">
                                <div>{keyValue.value}</div>
                            </th>
                        </tr>
                    </template>
                </tbody>
            </table>
        </template>

    </lightning-card>
</template>

showMapLWCDemo.js
import { LightningElement, track, wire } from 'lwc';
import getMapOfData from '@salesforce/apex/LWCExampleController.returnMapOfValues';

export default class ShowMapLWCDemo extends LightningElement {
    @track mapOfValues = [];

    @wire(getMapOfData)
    mapOfData({data, error}) {
        if(data) {
            for(let key in data) {
                // Preventing unexcepted data
                if (data.hasOwnProperty(key)) { // Filtering the data in the loop
                    this.mapOfValues.push({value:data[key], key:key});
                }
            }
        }
        else if(error) {
            window.console.log(error);
        }
    }
}

showMapLWCDemo.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="showMapLWCDemo">
    <apiVersion>45.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>

Result