Sunday, 17 March 2019

SOQL used in Apex




workflow rule :
================
1. you can do field updates on the base record and maybe its parent record.
2.Cross object field updates are extremely limited here,and only sometimes available
    if you have a master detail relationship between them.
3. Even then, the type of field updates you can do are limited to simple formulas.


process builder :
===================
1. you can do field updates on the base record and may be its parent record.

2.you can do more cross object updates,but only if the objects are strictly associated with each other via a lookup or master detail relationship field.


SOQL :
=======

1.Salesforce object query language
2. The language to find any record(s) in your Salesforce org.

Note : Capitalization and spacing is ignored by SOQL.

1.cross -object queries
2.grouping Results

Querying parent Data :

select Id,
  Amount,
 StageName,
 Account.Name,
 Account.Industry,
 Account.Website,
 Account.Owner.UserName
 from Opportunity where
 Account.Industry ='Energy'
 AND  Account.AnnualRevenue > 50000
 AND  CreatedBy.Email !=null


select Id,Amount,StageName,
 Sales_Plan__r.Goal__c,
 Sales_Plan__r.Sponsor__c,
 Sales_Plan__r.Qtr__r.Id,
 Account.Industry
 from Opportunity
 where Sales_Plan__r.Target__c>0

Querying Child Data :
=========================

select Id,Amount,StageName,
  Account.Name
 (select Role,ContactId ,
   Contact.FirstName,
   Contact.Email
  from
 OpportunityContactRoles
  where Role !=null AND Role !='Influencer')
 from Opportunity


select Id, Amount,StageName,
 (Select Id, Name,Price__c from Competition__r)
 from Opportunity

grouping SOQL :
===============

select StageName,
       SUM(Amount),
       Max(CloseDate),
       MIN(ExpectedRevenue),
       AVG(Amount),
       COUNT(Amount)
  from Opportunity
  where Amount !=null
group by StageName

understanding soql with apex :
===============================

why we use soql in apex ?

First we use SOQL to access fields that aren't in Trigger.new.
Trigger.new has less fields included in it than you'd expect.

Second, we use SOQL to modify records that aren't in trigger.new.

Note :
Trigger.new only stores, one,records entering our trigger, and two,base fields of those records entering our trigger.
For everyhing else, we need to query the data using SOQL first.

ex :
Access Fields Not in Trigger.new

trigger MatchAccountOwner on Contact (before insert){
  for(Contact myCon : Trigger.new){
      if(myCon.AccountId !=null){
         myCon.OwnerId = myCon.Account.OwnerId; // the outside field is NULL
     }
 }
}

All fields outside of Trigger.new's memory are considered null.

we must use SOQL to query data that doesn't exist in Trigger.new.


why Apex needs SOQL ?

1. Access fields and records that exist outside of Trigger.new.


System.assertEquals() :
=======================
A method used to check if two values are equal.

Commonly seen in test classes of experienced coders.

System.assertEquals(Expected Value,Actual value,Error message);

The message to display if the actual value !=expected value

System.assertEquals() is only used in test classes.

Every test class should have at least one assertion.