Open Khandagale-Saurabh opened 2 years ago
Here is List of Trigger Context Variables isExecuting isInsert isUpdate isDelete isBefore isAfter isUndelete new newMap old oldMap size
Salesforce Workflow:
It is an automated process that can shoot an action that is based on evaluation and rule criteria. Performing DML operations in the workflow is not possible. You can obtain a workflow over an object. You can’t create a query from the database.
Salesforce Trigger:
It is a piece of code that is executed either before or after a record is updated or inserted. More than 15 DML operations can be used in a single trigger. More than 20 SOQLs can be used from the database in a trigger. You can access triggers across an object and related to that object.
Limitations of Workflows That Triggers in Salesforce Overcome Workflows cannot create or update a separate object. You can’t reference certain fields when using workflows. You will not have your workflow doing more than just field updates and emails.
Better way to handel duplicate
Maximum trigger depth exceed=> How to avoid trigger recursion? or count =1; create a static variable in class boolean flag =true> use in trriger as=> if(classname.flag)=>{ flag=false; }
custm exception
Thumb Rule
Bulkify trigger : code should work with one and nth data
1]Dekh : when you want any id ,or filed that will get after insert of data the use after :=> when we creat account , acid will create after taht opportunity will be creted.
we have workflw, prcBuilder,aproval prcss etc=>we can work with this when data is inserted into database but if we want to work where we want to work before data is inserted to databse but it fires on DML after /before
[Exercises]
====================
Trigger Scenario 1::
====================
Create Custom field called “Number of Locations” on the Account Object (Data Type=Number). Develop a Trigger which creates contacts equal to the number which is entered in the "Number of Locations" field on the Account Object.
public class AccountProcessor { @future public static void countContacts(Set setId)
{
List lstAccount = [select id,Number_of_Contacts__c , (select id from contacts ) from account where id in :setId ];
for( Account acc : lstAccount )
{
List lstCont = acc.contacts ;
} }
@IsTest public class AccountProcessorTest { public static testmethod void TestAccountProcessorTest() { Account a = new Account(); a.Name = 'Test Account'; Insert a;
}
}
====================
Trigger Scenario 2::
====================
Create the object called "Customer Project" and create "Status" field under this object with picklist data type (Values = Active, Inactive). Create the relationship between this custom object and Opportunity so that Customer Project is related list to the Opportunity.
Create "Active Customer project" field on Opportunity object (Data Type = Checkbox)
The logic is when we create Customer Project for an Opportunity with the "Status = Active", then "Active Customer project" check box on the Opportunity Detail page is automatically checked.
====================
Trigger Scenario 3::
====================
Create "Sales Rep" field with data type (Text) on the Account Object. When we create the Account record, the Account Owner will be automatically added to Sales Rep field.
When we update the Account owner of the record, then also the Sales Rep will be automatically updated.
====================
Trigger Scenario 4::
====================
Create the object called "Books" and create field "Price" (data type is Currency) under this object.
Whenever we enter some amount of money in the Price field and once we click on save button, the value
we entered in the Price field is 10% less than the actual price. This is applicable for while both
inserting and updating records.