How to Write a trigger from Scratch || most basic to advanced approach in Salesforce #salesforce

preview_player
Показать описание
In this video we have covered a rollup summary trigger and we have started from a very basic approach and then tried to improve that trigger one improvement at a time . the purpose of this video is to show how we can write a trigger and then how to discuss problems in a trigger and how to improve those problems one by one.

You can find the code in the first pinned comment.

🚀 Level Up Your Salesforce Skills! 🚀
Sign up for my Salesforce Interview Prep Course and get:
1️⃣ Access to All Recordings: Covering all the important Salesforce topics.
2️⃣ Mock Interviews: Practice and get better at interviews.
3️⃣ Community Support: Get your doubts answered anytime by our awesome community.
4️⃣ Resume Review: Personalized feedback to make your resume stand out.
5️⃣ 1:1 Career Advice: Chat with me directly about those tough career decisions.
6️⃣ Weekend Doubt Sessions: Join live Q&A sessions to clear up any confusion.
7️⃣ Helpful Notes: Quick and easy reference for your studies.

💥 All of this for just ₹499, the lowest price ever! Plus, you can check out the free preview to see how we cover Salesforce topics before you decide.

Рекомендации по теме
Комментарии
Автор

/*
trigger RollUpTrigger on Opportunity (after insert, after update) {
for(opportunity currOppty : trigger.new){
Account a = [select id, No_Of_Child_Opportunities__c from Account where id =:currOppty.AccountId];
list<Opportunity> allAssociatedOppty = [select id, name from Opportunity where AccountID =:a.id];
= allAssociatedOppty.size();
update a;
}
}
*/
// this looks like a very simple approach but it has so many flaws. it has multiple SOQL inside loop and it also has DML inside loop. We need to improve it.
/*
trigger RollUpTrigger on Opportunity (after insert, after update) {
set<id> allAccountIds = new set<id>();
for(opportunity currentOppty : trigger.new){

}
list<Account> allAccounts = [select id, name, No_Of_Child_Opportunities__c from Account where id IN :allAccountIds];
for(Account currAccount : allAccounts){
list<Opportunity> allAssociatedOppty = [select id from Opportunity where AccountId =:currAccount.id];
= allAssociatedOppty.size();
update currAccount;
}

}
*/
// even though we have avoided one SOQL query inside loop, we still have a query in it and we need to improve that as well.
// we have to create a map of account id and list of opportunities to make this better.
// ?
/*
trigger RollUpTrigger on Opportunity (after insert, after update) {
set<id> allAccountIds = new set<id>();
if(trigger.isInsert){
for(opportunity currentOppty : trigger.new){

}
}
if(trigger.isUpdate){
for(opportunity currentOppty : trigger.new){
!= currentOppty.AccountId ) && currentOppty.AccountId != null){

}

}
}
if(trigger.isDelete){
for(opportunity currentOppty : trigger.old){

}
}


list<Account> allAccounts = [select id, name, No_Of_Child_Opportunities__c, (select id from opportunities) from Account where id IN :allAccountIds];
map<id, list<Opportunity>> AccToOpptyMap = new map<id, list<opportunity>>();
list<Opportunity> allOppty = [select id, accountid from opportunity where Accountid IN:allAccountIds];
for(Opportunity opp : allOppty){


}
else{
AccToOpptyMap.put(opp.AccountId, new list<opportunity>{opp});
}
}
list<Account> AccToUpdate = new list<Account>();
for(Account a : allAccounts){
=
AccToUpdate.add(a);
}
update AccToUpdate;
}

*/
// this is a bulkified approach and it works well but there is a shorter approach as well which we can take.






trigger RollUpTrigger on Opportunity (after insert, after update) {
set<id> allAccountIds = new set<id>();
if(trigger.isInsert){
for(opportunity currentOppty : trigger.new){

}
}
if(trigger.isUpdate){
for(opportunity currentOppty : trigger.new){
!= currentOppty.AccountId ) && currentOppty.AccountId != null){

}

}
}
if(trigger.isDelete){
for(opportunity currentOppty : trigger.old){

}
}


list<Account> allAccounts = [select id, name, No_Of_Child_Opportunities__c, (select id from opportunities) from Account where id IN :allAccountIds];
map<id, decimal> idToCountMap = new map<id, decimal>();
list<AggregateResult> totalOppty = [select count(id)totalOppty, accountId from Opportunity where accountId IN:allAccountIds group by accountId];
for(AggregateResult AR : totalOppty){
ID AccountID = (ID)AR.get('accountId');
decimal totalOppty =
idToCountMap.put(AccountID, totalOppty);
}
list<Account> accToUpdate = new list<Account>();
for(Account currAccount : allAccounts){
=
accToUpdate.add(currAccount);
}
update accToUpdate;
}
"

Mtripathi
Автор

Can you make more such videos on triggers topics which are important for interviews? It would be really helpful as this one was great.

udeeshamanohar
Автор

I am fresher for Salesforce what topics should I have to prepare for interview

sevouboi
Автор

How can we join the class that you are taking ?

extremeweirdness