Khandagale-Saurabh / Salesforce

1 stars 0 forks source link

Trigger #2

Open Khandagale-Saurabh opened 2 years ago

Khandagale-Saurabh commented 2 years ago

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 image

image

[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 ;

      acc.Number_of_Contacts__c = lstCont.size();
  }
  update lstAccount;

} }

@IsTest public class AccountProcessorTest { public static testmethod void TestAccountProcessorTest() { Account a = new Account(); a.Name = 'Test Account'; Insert a;

    Contact cont = New Contact();
    cont.FirstName ='Bob';
    cont.LastName ='Masters';
    cont.AccountId = a.Id;
    Insert cont;

    set<Id> setAccId = new Set<ID>();
    setAccId.add(a.id);

    Test.startTest();
        AccountProcessor.countContacts(setAccId);
    Test.stopTest();

    Account ACC = [select Number_of_Contacts__c from Account where id = :a.id LIMIT 1];
    System.assertEquals ( Integer.valueOf(ACC.Number_of_Contacts__c) ,1);

}

}

====================

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.

Khandagale-Saurabh commented 2 years ago

Here is List of Trigger Context Variables isExecuting isInsert isUpdate isDelete isBefore isAfter isUndelete new newMap old oldMap size

Khandagale-Saurabh commented 2 years ago

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.

Khandagale-Saurabh commented 2 years ago

https://intellipaat.com/blog/tutorial/salesforce-tutorial/triggers-in-salesforce/#no7 [Imp]

Khandagale-Saurabh commented 2 years ago

image

Khandagale-Saurabh commented 2 years ago

image

Khandagale-Saurabh commented 2 years ago

image

Khandagale-Saurabh commented 2 years ago

image

Khandagale-Saurabh commented 2 years ago

Better way to handel duplicate image

Khandagale-Saurabh commented 2 years ago

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; }

Khandagale-Saurabh commented 2 years ago

custm exception

image

Khandagale-Saurabh commented 2 years ago

image

Khandagale-Saurabh commented 1 year ago

Thumb Rule

image
Khandagale-Saurabh commented 10 months ago

Bulkify trigger : code should work with one and nth data

code bulkify

Khandagale-Saurabh commented 5 months ago

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.