Sunday, June 5, 2022

How to use static resource in LWC?

In this blog post we will learn how we can use static resource in LWC.

First create "testingstaticresourcewithLWC" zip file with the img folder as shown below.


How to use static resource in LWC?

This file has the below folders, 

staticresourcetest  which further has testimage under it as a PNG file.

How to use static resource in LWC?


 
How to use static resource in LWC?


Now, let us try to get this is LWC.

staticresourceLWC.html

<template>

    <lightning-card title="Static Resource Test">

     <img src={extractedimgfromLWC}>

    </lightning-card>

</template>


staticresourceLWC.js

import { LightningElement } from 'lwc';

import resource_test from '@salesforce/resourceUrl/testingstaticresourcewithLWC';

export default class StaticresourceLWC extends LightningElement {

    extractedimgfromLWC = resource_test + '/staticresourcetest/testimage';

}


staticresourceLWC.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">

    <apiVersion>54.0</apiVersion>

    <isExposed>true</isExposed>

    <targets>

        <target>lightning__AppPage</target>

        <target>lightning__RecordPage</target>

        <target>lightning__HomePage</target>

    </targets>

</LightningComponentBundle>


Now, let us try to add this Account page to see the results.

How to use static resource in LWC?


Sunday, May 22, 2022

How to access custom labels In Lightning Web Component?

 We import label using  @salesforce/label scoped module. 

Syntax:

import labelName from '@salesforce/label/labelReference';

Where,

labelName—A name that refers to the label.

labelReference—The name of the label in your org in the format namespace.labelName.


Let us understand with the below example,

Create 2 label as shown below.




custom labels In Lightning Web Component


custom labels In Lightning Web Component

customLabelLWC.html

<template>

    <lightning-card title={label.lightningcardname}>

     <lightning-button label={label.buttonnamefromcustomlabel}> </lightning-button>

    </lightning-card>

</template>


  customLabelLWC.js

import { LightningElement } from 'lwc';

import lightningcardname from '@salesforce/label/c.lightningcardname';

import buttonnamefromcustomlabel from '@salesforce/label/c.buttonnamefromcustomlabel';

export default class CustomLabelLWC extends LightningElement {

label ={lightningcardname,buttonnamefromcustomlabel}

 }

customLabelLWC.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">

    <apiVersion>54.0</apiVersion>

    <isExposed>true</isExposed>

    <targets>

        <target>lightning__AppPage</target>

        <target>lightning__RecordPage</target>

        <target>lightning__HomePage</target>

    </targets>

</LightningComponentBundle>


Now, add this component to Account page to see the result.

How to access custom labels In Lightning Web Component?

Saturday, May 21, 2022

FIELDS() function in Salesforce

SOQL SELECT statement (for example, SELECT Id, Name FROM Account) can be complicated if you need to retrieve many fields and if you don’t know what fields an object has, you must first get a descriptionof the object. Typically, you use a call to first get a description of the object, then parse the description to identify the fields. Then you construct a SOQL query that specifies the fields, and then make another call to submit the query. 

In order to avoid all of the complexity mentioned above we can use  FIELDS() function which simplifies SELECT statements, avoids the need for multiple API calls, and provides a low-code method to explore the data in your org.

The FIELDS() function lets you select groups of fields without knowing their names in advance. 

You can now include any of these in the field list:


FIELDS(ALL)—to select all the fields of an object.

FIELDS(CUSTOM)—to select all the custom fields of an object.

FIELDS(STANDARD)—to select all the standard fields of an object.

In each case, FIELDS() respects field-level security so it only shows the fields that you have permission to access.


Examples:


SELECT FIELDS(ALL) FROM Account LIMIT 200

SELECT FIELDS(CUSTOM) FROM Account LIMIT 200

SELECT FIELDS(STANDARD) FROM Account


We can also mix FIELDS() with other field names in the field list. For example:


SELECT Name, Id, FIELDS(CUSTOM) FROM Account LIMIT 200

SELECT someCustomField__c, FIELDS(STANDARD) FROM Account


FIELDS() can also be used in subqueries. 


For example:


SELECT

   Account.Name, 

   (SELECT FIELDS(ALL) FROM Account.Contacts LIMIT 200)

FROM Account


We can also use FIELDS() with /query in REST API as shown below:

GET https://yourInstance.salesforce.com/services/data/v54.0/query?​q=SELECT+FIELDS(STANDARD)+FROM+Account

Note: 

If you already know which fields you want to retrieve, you’ll get better performance by specifying them explicitly 

rather than using FIELDS() and retrieving more fields than you need.


Date Formats and Date Literals in WHERE Clause in Salesforce

 A) Filter Query Results Using Dates

The fieldExpression of a WHERE clause supports filtering query results based on date and dateTime fields.

This table shows the supported date and dateTime formats that you can use in the WHERE clause of an SOQL query.

Field Type     Format                                                   Example

date               YYYY-MM-DD                                    1999-01-01

 

    

dateTime    YYYY-MM-DDThh:mm:ss+hh:mm         1999-01-01T23:01:01+01:00

                   YYYY-MM-DDThh:mm:ss-hh:mm          1999-01-01T23:01:01-08:00

                   YYYY-MM-DDThh:mm:ssZ                    1999-01-01T23:01:01Z

For example, this query filters for Account records that were created after the specified date and time.

SELECT Id

FROM Account

WHERE CreatedDate > 2005-10-08T01:02:03Z


B) Filter Query Results Using Date Literals


YESTERDAY:

SELECT Id FROM Account WHERE CreatedDate = YESTERDAY

TODAY:

SELECT Id FROM Account WHERE CreatedDate > TODAY

TOMORROW:   

SELECT Id FROM Opportunity WHERE CloseDate = TOMORROW

LAST_WEEK:

SELECT Id FROM Account WHERE CreatedDate > LAST_WEEK

THIS_WEEK:

SELECT Id FROM Account WHERE CreatedDate < THIS_WEEK

NEXT_WEEK:

SELECT Id FROM Opportunity WHERE CloseDate = NEXT_WEEK

LAST_MONTH:

SELECT Id FROM Opportunity WHERE CloseDate > LAST_MONTH

THIS_MONTH:

SELECT Id FROM Account WHERE CreatedDate < THIS_MONTH

NEXT_MONTH:

SELECT Id FROM Opportunity WHERE CloseDate = NEXT_MONTH

LAST_90_DAYS:

Starts with the current day and continues for the past 90 days.

This range includes the current day, not just previous days. So it includes 91 days in total.

SELECT Id FROM Account WHERE CreatedDate = LAST_90_DAYS

NEXT_90_DAYS:

Starts 00:00:00 UTC of the next day and continues for the next 90 days.

SELECT Id FROM Opportunity WHERE CloseDate > NEXT_90_DAYS    

LAST_N_DAYS:n

For the number n provided, starts with the current day and continues for the past n days.

This range includes the current day, not just previous days. For example, LAST_N_DAYS:1 includes yesterday and today.

SELECT Id FROM Account WHERE CreatedDate = LAST_N_DAYS:365

NEXT_N_DAYS:n

For the number n provided, starts 00:00:00 UTC of the next day and continues for the next n days.

This range doesn't include the current day. For example, NEXT_N_DAYS:1 is equivalent to TOMORROW.

SELECT Id FROM Opportunity WHERE CloseDate > NEXT_N_DAYS:15

Similarly other literals available are,

LAST_N_WEEKS:n  

NEXT_N_WEEKS:n

NEXT_N_MONTHS:n

LAST_N_MONTHS:n

THIS_QUARTER

LAST_QUARTER

NEXT_QUARTER

NEXT_N_QUARTERS:n

LAST_N_QUARTERS:n

THIS_YEAR

LAST_YEAR

NEXT_YEAR

LAST_N_YEARS:n


For more details visit,


https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm

LIKE, IN, NOT IN, INCLUDES, EXCLUDES, Semi-Joins with IN and Anti-Joins with NOT IN, ORDER BY, GROUP BY, OFFSET in Salesforce SOQL

LIKE operator:


The % and _ wildcards are supported for the LIKE operator.

The % wildcard matches zero or more characters.

The _ wildcard matches exactly one character.

The text string in the specified value must be enclosed in single quotes.

The LIKE operator is supported for string fields only.

The LIKE operator performs a case-sensitive match for case-sensitive fields, and a case-insensitive match for case-insensitive fields.


For example, the following query matches Orange, Orang, and Ora, but not Oora:

SELECT AccountId, FirstName, lastname

FROM Contact

WHERE lastname LIKE 'Ora%'


IN operator:

The values for IN must be in parentheses. String values must be surrounded by single quotes.


SELECT Name FROM Account

WHERE BillingState IN ('California', 'New York')


NOT IN operator:

If the value doesn’t equal any of the values in a WHERE clause. For example:


SELECT Name FROM Account

WHERE BillingState NOT IN ('California', 'New York')


The values for NOT IN must be in parentheses. String values must be surrounded by single quotes.


INCLUDES EXCLUDES operator:

Applies only to multi-select picklists.


Semi-Joins with IN and Anti-Joins with NOT IN:


A) ID field Semi-Join

You can include a semi-join in a WHERE clause. For example, the following query returns account IDs if an associated opportunity is lost:


SELECT Id, Name 

FROM Account 

WHERE Id IN 

  ( SELECT AccountId

    FROM Opportunity

    WHERE StageName = 'Closed Lost' 

  )


B) ID field Anti-Join


The following query returns account IDs for all accounts that don’t have any open opportunities:


SELECT Id 

FROM Account 

WHERE Id NOT IN

  (

    SELECT AccountId

    FROM Opportunity

    WHERE IsClosed = false

  )


The following query returns opportunity IDs for all contacts whose source isn’t Web:


SELECT Id

FROM Opportunity

WHERE AccountId NOT IN 

  (

    SELECT AccountId

    FROM Contact

    WHERE LeadSource = 'Web'

  )

You can’t query on the same object in a subquery as in the main query.


SELECT Id, Name

FROM Account

WHERE Id IN

  (

    SELECT ParentId

    FROM Account

    WHERE Name = 'myaccount'

  )


You can’t use subqueries with OR.

COUNT, FOR UPDATE, ORDER BY, and LIMIT aren’t supported in subqueries.


ORDER BY:


Order BY clause is used to get results in particular order.


SELECT Name, Industry FROM Account ORDER BY Name


If there a multiple Account records with the same Name,the order of the results for the above query can vary.

So we can add the Id (or any other field that is unique in the results) to the ORDER BY clause. 


For example:


SELECT Name, Industry FROM Account ORDER BY Name, Id


ASC or DESC:


Specifies whether the results are ordered in ascending (ASC) or descending (DESC) order. Default order is ascending.


NULLS FIRST or NULLS LAST:


Orders null records at the beginning (NULLS FIRST) or end (NULLS LAST) of the results. 

By default, null values are sorted first.


For example, the following query returns a query result with Account records in alphabetical order by first name, 

sorted in descending order, with accounts that have null names appearing last:


SELECT Name

FROM Account

ORDER BY Name DESC NULLS LAST


OFFSET:


Here are a few points to consider when using LIMIT and OFFSET in your queries:


The maximum offset is 2,000 rows. Requesting an offset greater than 2,000 results in a NUMBER_OUTSIDE_VALID_RANGE error.


OFFSET is intended to be used in a top-level query, and is not allowed in most subqueries, so the following query is 

invalid and returns a MALFORMED_QUERY error.


SELECT Name, Id

FROM Account

WHERE Id IN 

   (

     SELECT AccountId

     FROM Opportunity

     LIMIT 100

     OFFSET 20

   )

ORDER BY Name


A subquery can use OFFSET only if the parent query has a LIMIT 1 clause. The following query is a valid use of OFFSET in a subquery:


SELECT Name, Id,

    (

        SELECT Name FROM Opportunities LIMIT 10 OFFSET 2

    )

FROM Account

ORDER BY Name

LIMIT 1


OFFSET cannot be used as a subquery along with WHERE clause, even if the parent query uses LIMIT 1.