You need to sign in to do that
Don't have an account?
Kunal Purohit 4
How to write handler for given trigger
Hello Folks,
How to write Handler for below trigger.?
trigger SumAmountOpp on Opportunity (After Insert,After Update,After Delete, After Undelete) { List<Account> accList=new List<Account>(); Set<Id> setAccIds = new Set<Id>(); if(Trigger.isInsert){ if(trigger.isAfter){ for(Opportunity con : Trigger.new){ if(con.AccountId != null && con.Amount>10000.00){ setAccIds.add(con.AccountId); } } } } system.debug('setAccIds ==> '+setAccIds); if(Trigger.isUpdate){ if(trigger.isAfter){ for(Opportunity con : Trigger.new){ if(con.AccountId!=Trigger.oldMap.get(con.Id).AccountId && con.Amount>10000.00){ setAccIds.add(con.AccountId); setAccIds.add(Trigger.oldMap.get(con.Id).AccountId); } } } } if(Trigger.isDelete){ if(trigger.isAfter){ for(Opportunity con : Trigger.old) { if(con.AccountId != null && con.Amount>10000.00){ setAccIds.add(con.AccountId); } } } } if(Trigger.isUndelete){ if(trigger.isAfter){ for(Opportunity con : Trigger.new) { if(con.AccountId != null && con.Amount>10000.00){ setAccIds.add(con.AccountId); } } } } for(Account acc :[Select id,Total_Opportunity__c ,(Select Amount from Opportunities where Amount>10000.00) from Account where Id in : setAccIds]){ acc.Total_Opportunity__c = acc.Opportunities.size(); acclist.add(acc); } if(acclist.size()>0){ update accList; } }
Can you try below trigger and handler.
Handler:
Trigger:
Replace Total_Opty_Amount__c with Total_Opportunity__c
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
Can you confirm if Total_Opportunity__c is curency field on Account object?
Thanks,
Total_Opportunity__c is Number field in Account Object.
Can you try below trigger and handler.
Handler:
Trigger:
Replace Total_Opty_Amount__c with Total_Opportunity__c
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
Thanks Sai Praveen. It worked for me. Now, I am trying to write a test class for same. Can you please help me how to accompolish the same? Here, is my test class.
@isTest
public class AmountatOpportunityTest {
@istest
static void OppCount()
{
Account acc= new Account();
acc.name= 'sample account';
acc.ISSN_Number__c = 111;
insert acc;
Account acc1 = new Account();
acc1.Name = 'test account';
acc1.ISSN_Number__c = 2222;
insert acc1;
Opportunity opp = new Opportunity();
opp.AccountId = acc.Id;
opp.Amount=15000.00;
opp.Name = 'Test sum';
opp.StageName = 'Test picklist';
opp.CloseDate= date.newInstance(2023, 11, 10);
insert opp;
Opportunity Opp1 = new Opportunity();
opp1.AccountId = acc1.Id;
opp1.Amount=15000.00;
opp1.Name = 'Test sum2';
opp1.StageName = 'Test picklist2';
opp1.CloseDate= date.newInstance(2023, 11, 10);
insert opp1;
List<Opportunity> OppLst = new List<Opportunity>();
OppLst.add(opp);
OppLst.add(opp1);
Map<Id, Opportunity> OppMap = new Map<Id, Opportunity>();
OppMap.Put(opp.Id, opp);
OppMap.Put(opp1.Id, opp1);
AmountAtOpportunityClass ac = new AmountAtOpportunityClass();
ac.afterdelete(OppLst);
ac.afterinsert(OppLst);
ac.afterupdate(OppLst, OppMap);
}
}
It is shoiwng error:
System.ListException: Duplicate id in list: 0012w000019RuxzABC
Thanks for confirming on it. As this seems to test class for the above which will be seperate question can you post this as new question so I can answer it so it will avoid confusion for others as well.
Thanks,