1- Apex Testing and SOQL

 What to test 

  1. Single action
  2. Bulk action
  3. Positive action
  4. Negative action
  5. Restricted users
(HINT:- write test cases with different positive and negative numbers as input)
(HINT:- Sometimes the results are not expected so in that case change the original class ) 

**Minimum Test coverage in SF Apex is 75%


**System.assert() etc but use Assert class for testing


----------------------------------------SOQL-----------------------------------

IN , LIKE, LIMIT, OFFSET

// Get latest 5 records and skip first 5

SELECT Name, Status, LeadSource, CreatedDate FROM Lead ORDER BY CreatedDate DESC LIMIT 5 OFFSET 5


 Date Literals

In SOQL, date literals make it easy to filter data based on dynamic or specific date ranges without having to calculate exact dates manually. These literals work with date, datetime, and time fields and are particularly useful in WHERE clauses. Here’s a breakdown of commonly used date literals in SOQL:

Commonly Used Date Literals

  1. Today and Relative Days

    • TODAY: Represents the current day.
      SELECT Id, Name FROM Account WHERE CreatedDate = TODAY
    • YESTERDAY: Represents the day before today.
    • TOMORROW: Represents the day after today.
  2. Relative Days

    • LAST_N_DAYS:n: Represents the last n days, including today.
      SELECT Id, Name FROM Opportunity WHERE CloseDate = LAST_N_DAYS:30
    • NEXT_N_DAYS:n: Represents the next n days, including today.
    • N_DAYS_AGO:n: Represents n days ago from today.
  3. Relative Weeks

    • THIS_WEEK: Represents the current calendar week (Sunday through Saturday).
    • LAST_WEEK: Represents the previous calendar week.
    • NEXT_WEEK: Represents the upcoming calendar week.
  4. Relative Months

    • THIS_MONTH: Represents the current calendar month.
    • LAST_MONTH: Represents the previous calendar month.
    • NEXT_MONTH: Represents the upcoming calendar month.
    • LAST_N_MONTHS:n: Represents the last n months, including the current month.
      SELECT Id, Name FROM Account WHERE CreatedDate = LAST_N_MONTHS:6
    • NEXT_N_MONTHS:n: Represents the next n months.
  5. Relative Quarters

    • THIS_QUARTER: Represents the current calendar quarter.
    • LAST_QUARTER: Represents the previous calendar quarter.
    • NEXT_QUARTER: Represents the upcoming calendar quarter.
    • LAST_N_QUARTERS:n: Represents the last n quarters.
    • NEXT_N_QUARTERS:n: Represents the next n quarters.
  6. Relative Years

    • THIS_YEAR: Represents the current calendar year.
    • LAST_YEAR: Represents the previous calendar year.
    • NEXT_YEAR: Represents the upcoming calendar year.
    • LAST_N_YEARS:n: Represents the last n years.
    • NEXT_N_YEARS:n: Represents the next n years.
      SELECT Id, Name FROM Opportunity WHERE CloseDate = LAST_N_YEARS:2
  7. Fiscal Periods

    • THIS_FISCAL_QUARTER: Represents the current fiscal quarter.
    • LAST_FISCAL_QUARTER: Represents the previous fiscal quarter.
    • NEXT_FISCAL_QUARTER: Represents the upcoming fiscal quarter.
    • THIS_FISCAL_YEAR: Represents the current fiscal year.
    • LAST_FISCAL_YEAR: Represents the previous fiscal year.
    • NEXT_FISCAL_YEAR: Represents the upcoming fiscal year.
    • LAST_N_FISCAL_YEARS:n: Represents the last n fiscal years.
    • NEXT_N_FISCAL_YEARS:n: Represents the next n fiscal years.

Example Query Using Multiple Date Literals

Get opportunities closing in the current quarter:

SELECT Id, Name, CloseDate FROM Opportunity WHERE CloseDate = THIS_QUARTER

These literals make it easy to write dynamic, date-based SOQL queries without worrying about specific date calculations. Let me know if you’d like more examples with specific date types!

Comments

Popular posts from this blog

API Names in Salesforce

18 - LWC - BEST PRACTICES - For accessing HTML elements and their values in JavaScript

6 - Object Relationships (Lookup and Master-Detail and Junction Objects)