Saturday, January 5, 2019

wire service in Lightning Web Components(LWC)


This post explains wire service in Lightning web components(lwc)

What is the wire service?

  1. Lightning web components(lwc) use reactive wire service to read Salesforce data.
  2. It is Built on Lightning Data service.
  3. Components use @wire in their JavaScript class to read data from one of the wire adapters in the lightning/ui*Api namespace.
Reactive wire service supports reactive variables, which are prefixed with $. If a reactive variable changes, the wire service provisions(requests) new data.

wire service syntax

import { adapterId } from 'adapterModule';
@wire(adapterId, adapterConfig)
propertyOrFunction;

adapterId (Identifier) ---> The identifier of the wire adapter.
adapterModule (String) —>The identifier of the module that contains the wire adapter function, in the format namespace/moduleName.
adapterConfig (Object) —> A configuration object specific to the wire adapter. Configuration object property values can be either strings or references to objects and fields imported from @salesforce/schema.
propertyOrFunction —> A private property or function that receives the stream of data from the wire service.
  • If a property is decorated with @wire, the results are returned to the property’s data property or error property. 
  • If a function is decorated with @wire, the results are returned in an object with a data property and an error property.

Example

 Import references to salesforce Object and fields
1. To import a reference to an object, use this syntax.
import objectName from '@salesforce/schema/object';
Ex:
import PROPERTY_OBJECT from '@salesforce/schema/Property__c';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
2. To import a reference to a field, use this syntax.
import FIELD_NAME from '@salesforce/schema/object.field';
Ex:
import POSITION_LEVEL_FIELD from '@salesforce/schema/Property__c.Name';
import ACCOUNT_NAME_FIELD from '@salesforce/schema/Account.Name';
3. To import a reference to a field via a relationship, use this syntax.
import SPANNING_FIELD_NAME from '@salesforce/schema/object.relationship.field';
Ex:
import ACCOUNT_OWNER_NAME_FIELD from '@salesforce/schema/Account.Owner.Name';

Decorate a Property with @wire

  • Wiring a property with @wire is useful when you want to consume the data or error.
  • If the property decorated with @wire is used as an attribute in the template and its value changes,
  • the wire service provisions(requests) the data and triggers the component to rerender.
Ex:
import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

export default class Record extends LightningElement {
    @api recordId;

    @wire(getRecord, { recordId: '$recordId', fields: ['Opportunity.Name'] })
    record;
}

Decorate a Function with @wire

  • Wiring a function is useful to perform logic whenever new data is provided or when an error occurs. 
  • The function is invoked whenever a value is available, which can be before or after the component is connected or rendered.
Ex:
import { LightningElement, api, track, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

export default class WireFunction extends LightningElement {
    @api recordId;
    @track record;
    @track error;

    @wire(getRecord, { recordId: '$recordId', fields: ['Opportunity.Name'] })
    wiredAccount({ error, data }) {
        if (data) {
            this.record = data;
        } else if (error) {
            this.error = error;
        }
    }
}

Wednesday, January 2, 2019

Switch Statement in Apex


This post explains about switch statement in Salesforce
Apex provides a switch statement that tests whether an expression matches one of several values and branches accordingly.

Syntax

switch on expression {
    when value1 {       // when block 1
        // code block 1
    }   
    when value2 {       // when block 2
        // code block 2
    }
    when value3 {       // when block 3
        // code block 3
    }
    when else {       // when else block, optional
        // code block 4
    }
}
Apex switch statement expressions can be one of the following types.
  • Integer
  • Long
  • sObject
  • String
  • Enum
Example
String strDayName = 'Sunday';

Switch on strDayName {
    when 'Sunday' {
        System.debug('today is Sunday');
    }
    when 'Monday' {
        System.debug('today is Monday');
    }
    when else {
        System.debug('No Value match');
    }
}
The switch statement evaluates the expression and executes the code block for the matching when value. If no value matches, then when else code block is executed. If there isn’t a when else block, no action is taken.
Resource
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_switch.htm

Sunday, December 30, 2018

Use of Inherited Sharing in Apex Class


This post explains the use of inherited sharing in apex class
If you specify inherited sharing Keyword on an Apex class,  which allows the class to run in the sharing mode of the class that called it. Using inherited sharing enables you to pass security review and ensure that your privileged Apex code is not used in unexpected or insecure ways.

  1. An Apex class with inherited sharing runs as with sharing when used as a Visualforce page controller, Apex REST service, or an entry point to an Apex transaction.
  2. An Apex class with Inherited Sharing is being called from some other class which is having without sharing setting, then it will run in without sharing mode.
Example
This example declares an Apex class with inherited sharing and a Visualforce invocation of that Apex code. Because of the inherited sharing declaration, only opportunities for which the running user has sharing access are displayed. If the declaration is omitted, even opportunities that the user has no rights to view are displayed due to the insecure default behavior of omitting the declaration.
Apex Class
public inherited sharing class OpportunityMethods {
    public list<Opportunity> getAllTheOpps(){
        return [SELECT Id, Name, StageName FROM Opportunity];
    }
}
Visualforce page
<apex:page controller="OpportunityMethods">
    <apex:repeat value="{!getAllTheOpps}" var="record">
        {!record.Name}
    </apex:repeat>
</apex:page>

Happy Learning!!