You need to sign in to do that
Don't have an account?
SV M
Undelete action isn't working in my apex Trigger
Hi,
I have an apex trigger to count the total Opportunities related to an Account and Sum of Opportunity Amount related to Account. I am trying to implement "after undelete" in my trigger. But it isn't working. Can someone help to do this?
Below is my Trigger
trigger OpportunityCount on Opportunity (after insert, after delete, after update, after Undelete) {
Map<Id, List<Opportunity>> acctIdOpptyListMap = new Map<Id, List<Opportunity>>();
Set<Id> acctIds = new Set<Id>();
List<Opportunity> opptyList = new List<Opportunity>();
if(trigger.isUpdate && trigger.isInsert){
for(Opportunity oppty : trigger.New){
if(oppty.AccountId != null){
acctIds.add(oppty.AccountId);
}
}
}
if(trigger.isDelete){
for(Opportunity oppty : trigger.old){
if(oppty.AccountId != null){
acctIds.add(oppty.AccountId);
}
}
}
if(acctIds.size() > 0){
opptyList = [SELECT Amount, AccountId
FROM Opportunity
WHERE AccountId IN : acctIds];
for(Opportunity oppty : opptyList){
if(!acctIdOpptyListMap.containsKey(oppty.AccountId)){
acctIdOpptyListMap.put(oppty.AccountId, new List<Opportunity>());
}
acctIdOpptyListMap.get(oppty.AccountId).add(oppty);
}
List<AggregateResult> lstResult = [SELECT AccountId, COUNT(Id) countId
FROM Opportunity
WHERE AccountId IN:acctIds
GROUP BY AccountId];
List<Account> lstAccount = new List<Account>();
for(AggregateResult result:lstResult){
Account acct = new Account (Id=(Id)result.get('AccountId'), Total_Opportunities__c = (Integer)result.get('countId'));
lstAccount.add(acct);
}
update lstAccount;
List<Account> acctList = new List<Account>();
acctList = [SELECT Total_Amount__c
FROM Account
WHERE Id IN: acctIds];
for(Account acct : acctList){
List<Opportunity> tempOpptyList = new List<Opportunity>();
tempOpptyList = acctIdOpptyListMap.get(acct.Id);
Double totalOpptyAmount = 0;
for(Opportunity oppty : tempOpptyList){
if(oppty.Amount != null){
totalOpptyAmount += oppty.Amount;
}
}
acct.Total_Amount__c = totalOpptyAmount;
}
update acctList;
}
}
I have an apex trigger to count the total Opportunities related to an Account and Sum of Opportunity Amount related to Account. I am trying to implement "after undelete" in my trigger. But it isn't working. Can someone help to do this?
Below is my Trigger
trigger OpportunityCount on Opportunity (after insert, after delete, after update, after Undelete) {
Map<Id, List<Opportunity>> acctIdOpptyListMap = new Map<Id, List<Opportunity>>();
Set<Id> acctIds = new Set<Id>();
List<Opportunity> opptyList = new List<Opportunity>();
if(trigger.isUpdate && trigger.isInsert){
for(Opportunity oppty : trigger.New){
if(oppty.AccountId != null){
acctIds.add(oppty.AccountId);
}
}
}
if(trigger.isDelete){
for(Opportunity oppty : trigger.old){
if(oppty.AccountId != null){
acctIds.add(oppty.AccountId);
}
}
}
if(acctIds.size() > 0){
opptyList = [SELECT Amount, AccountId
FROM Opportunity
WHERE AccountId IN : acctIds];
for(Opportunity oppty : opptyList){
if(!acctIdOpptyListMap.containsKey(oppty.AccountId)){
acctIdOpptyListMap.put(oppty.AccountId, new List<Opportunity>());
}
acctIdOpptyListMap.get(oppty.AccountId).add(oppty);
}
List<AggregateResult> lstResult = [SELECT AccountId, COUNT(Id) countId
FROM Opportunity
WHERE AccountId IN:acctIds
GROUP BY AccountId];
List<Account> lstAccount = new List<Account>();
for(AggregateResult result:lstResult){
Account acct = new Account (Id=(Id)result.get('AccountId'), Total_Opportunities__c = (Integer)result.get('countId'));
lstAccount.add(acct);
}
update lstAccount;
List<Account> acctList = new List<Account>();
acctList = [SELECT Total_Amount__c
FROM Account
WHERE Id IN: acctIds];
for(Account acct : acctList){
List<Opportunity> tempOpptyList = new List<Opportunity>();
tempOpptyList = acctIdOpptyListMap.get(acct.Id);
Double totalOpptyAmount = 0;
for(Opportunity oppty : tempOpptyList){
if(oppty.Amount != null){
totalOpptyAmount += oppty.Amount;
}
}
acct.Total_Amount__c = totalOpptyAmount;
}
update acctList;
}
}
Use a block like this for undelete as well :
if(trigger.isUnDelete){
for(Opportunity oppty : trigger.old){
if(oppty.AccountId != null){
acctIds.add(oppty.AccountId);
}
}
}
I have gone through your problem.
Please you this block for after undelete event.
if(trigger.isUnDelete){
for(Opportunity oppty : trigger.new){
if(oppty.AccountId != null){
acctIds.add(oppty.AccountId);
}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
The above code will now work using trigger inser or update due to this " trigger.isUpdate && trigger.isInsert " . here i used custom field Amount__c
updated code :
trigger OpportunityCount on Opportunity (after insert, after delete, after update, after Undelete) {
Map<Id, List<Opportunity>> acctIdOpptyListMap = new Map<Id, List<Opportunity>>();
Set<Id> acctIds = new Set<Id>();
List<Opportunity> opptyList = new List<Opportunity>();
if(trigger.isInsert && trigger.isUpdate ){
for(Opportunity oppty : trigger.New){
if(oppty.AccountId != null){
acctIds.add(oppty.AccountId);
}
}
}
if(trigger.isDelete){
for(Opportunity oppty : trigger.old){
if(oppty.AccountId != null){
acctIds.add(oppty.AccountId);
}
}
}
if(trigger.isUnDelete){
for(Opportunity oppty : trigger.new){
if(oppty.AccountId != null){
acctIds.add(oppty.AccountId);
}
}
}
if(acctIds.size() > 0)
{
opptyList = [SELECT Amount__c, AccountId FROM Opportunity WHERE AccountId IN : acctIds];
for(Opportunity oppty : opptyList)
{
if(!acctIdOpptyListMap.containsKey(oppty.AccountId))
{
acctIdOpptyListMap.put(oppty.AccountId, new List<Opportunity>());
}
acctIdOpptyListMap.get(oppty.AccountId).add(oppty);
}
List<AggregateResult> lstResult = [SELECT AccountId, COUNT(Id) countId FROM Opportunity WHERE AccountId IN:acctIds GROUP BY AccountId];
List<Account> lstAccount = new List<Account>();
for(AggregateResult result:lstResult){
Account acct = new Account (Id=(Id)result.get('AccountId'), Total_Opportunities__c = (Integer)result.get('countId'));
lstAccount.add(acct);
}
update lstAccount;
List<Account> acctList = new List<Account>();
acctList = [SELECT Total_Amount__c__c FROM Account WHERE Id IN: acctIds];
for(Account acct : acctList)
{
List<Opportunity> tempOpptyList = new List<Opportunity>();
tempOpptyList = acctIdOpptyListMap.get(acct.Id);
Double totalOpptyAmount__c = 0;
for(Opportunity oppty : tempOpptyList)
{
if(oppty.Amount__c != null){
totalOpptyAmount__c += oppty.Amount__c;
}
}
acct.Total_Amount__c__c = totalOpptyAmount__c;
}
update acctList;
}
}