You need to sign in to do that
Don't have an account?
pavan kumar 177
After delete event is not working
got a requirement like to calculate average in account based on particular record type of case. My requirement is:
I have a field called ARP in account currency. I want to calculate overall average of ARP field in particular record type called customer order whether Case details either new or add-on.
So I'm trying write a trigger a trigger.
Everything is working but after delete event is not working means.
Example when i create a case with ARPU will be 21.And if i delete the case it's still showing 21 in account level.Even i am trigger.old in trigger to handler class.
My trigger would be
trigger ForARPU on case (after insert,after update,after delete)
{
if(Trigger.isAfter && trigger.isInsert || Trigger.isAfter && trigger.isUpdate )
{
//It will call a static method called ARPUInsert in ARPUCases class.
ARPUCases.ARPUInsert(Trigger.New);
}
if(Trigger.isAfter && trigger.isDelete)
{
//It will call a static method called ARPUInsert in ARPUCases class.
ARPUCases.ARPUDelete(Trigger.Old);
}
}
Handler class would be
/**
* This class is used to calculate average of Arpu in customer order record type
* with cases details either Add-on or New subscription and update in Related Account field called ARPU.
* This class is fire whenever record type called customer order case has been inserted or updated or deleted.
* @author Pavan Kumar
* @since 2016-09-28
*/
public class ARPUCases
{
/** @var recordTypeID - is to filter cases with particular record type */
/** @var Accountstoupdate - is to update the particular accounts */
/** @var CaseIDs - is to retrive all related case id's */
public static Set<Account> accSet = new Set<Account>();
public static void ARPUInsert(List<Case> CaseTriggers)
{
/**
* This method is to caluclate average of all ARPU amounts with case have customer order recordtype
* as well as case details with either ADD-On (OR) New Subscription and update in related account
* field called ARPU.
* This method will fire only when any case with record type called customer order is created as well updated.
*/
if (Trigger.isInsert || Trigger.isUpdate)
{
Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'Customer_Order'].Id;
List<Account> Accountstoupdate =new List<Account>();
List<Case> CaseIDs = new List<Case>();
for (Case s : CaseTriggers)
{
if(s.RecordTypeId == recordTypeId && s.Case_Details__c =='New Subscription' || s.Case_Details__c =='Add On' )
{
// Loop through and add caseId's to the list.
CaseIDs.add(s);
}
}
/** @Var CaseAccountIds - to get accountId's of cases with list called CaseIDs */
set<Id> CaseAccountIds = new set<Id>();
for(Case c : [SELECT Id,ARPU__c,accountId FROM Case WHERE Id IN :CaseIDs])
{
// Loop through and add AccountId's to the list.
CaseAccountIds.add(c.AccountId);
}
/** @Var Accountswithcases-to get account details to caluclate average */
/** @Var results-to get average of particular account based on the cases */
List<Account> Accountswithcases = [select Id,name,ARPU__c from Account where id =:CaseAccountIds];
Map<Id, AggregateResult> results = new Map<Id, AggregateResult>(
[SELECT AccountId Id, AVG(ARPU__c) average FROM Case WHERE AccountId = :Accountswithcases GROUP BY AccountId]);
For(account a1: Accountswithcases)
{
if(results.get(a1.Id) != null)
{
// Loop through aggregate results array and typecast the average of ARPU and update in account level.
a1.ARPU__c = (Decimal)results.get(a1.Id).get('average');
Accountstoupdate.add(a1);
}
}
if(Accountstoupdate.size()>0)
{
// DML statement to update all the accounts related with cases
update Accountstoupdate;
}
}
}
public static void ARPUDelete(List<Case> CaseTriggers)
{
/**
* This method is to caluclate average of all ARPU amounts with case have customer order recordtype
* as well as case details with either ADD-On (OR) New Subscription and update in related account
* field called ARPU.
* This method will fire only when any case with record type called customer order is deleted.
*/
if (Trigger.isDelete)
{
Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'Customer_Order'].Id;
List<Account> Accountstoupdate =new List<Account>();
List<Case> CaseIDs = new List<Case>();
for (Case s : CaseTriggers)
{
if(s.RecordTypeId == recordTypeId && s.Case_Details__c =='New Subscription' || s.Case_Details__c =='Add On')
{
// Loop through and add caseId's to the list.
CaseIDs.add(s);
}
}
System.debug('Data in CaseIDs:'+ CaseIDs.size());
System.debug('Data in details CaseIDs:'+ CaseIDs);
/** @Var CaseAccountIds - to get accountId's of cases with list called CaseIDs */
set<Id> CaseAccountIds = new set<Id>();
for(Case c : CaseTriggers)
{
// Loop through and add AccountId's to the list.
CaseAccountIds.add(c.AccountId);
}
System.debug('After Data in CaseAccountIds:'+ CaseAccountIds.size());
System.debug('After Data Details in CaseAccountIds:'+ CaseAccountIds);
/** @Var Accountswithcases-to get account details to caluclate average */
/** @Var results-to get average of particular account based on the cases */
List<Account> Accountswithcases = [select Id,name,ARPU__c from Account where id =:CaseAccountIds];
Map<Id, AggregateResult> results = new Map<Id, AggregateResult>(
[SELECT AccountId Id, AVG(ARPU__c) average FROM Case WHERE AccountId = :Accountswithcases GROUP BY AccountId]);
System.debug('Results:'+ results.size());
System.debug('Data in Accountswithcases:'+ Accountswithcases.size());
For(account a1: Accountswithcases)
{
if(results.get(a1.Id) != null )
{
// Loop through aggregate results array and typecast the average of ARPU and update in account level.
a1.ARPU__c = (Decimal)results.get(a1.Id).get('average');
Accountstoupdate.add(a1);
}
}
System.debug('After Results:'+ Accountstoupdate.size());
System.debug('Size of the Results:'+ Accountstoupdate);
if(Accountstoupdate.size()>0)
{
// DML statement to update all the accounts related with cases.
update Accountstoupdate;
}
}
}
}
I am attaching my debug logs
I have a field called ARP in account currency. I want to calculate overall average of ARP field in particular record type called customer order whether Case details either new or add-on.
So I'm trying write a trigger a trigger.
Everything is working but after delete event is not working means.
Example when i create a case with ARPU will be 21.And if i delete the case it's still showing 21 in account level.Even i am trigger.old in trigger to handler class.
My trigger would be
trigger ForARPU on case (after insert,after update,after delete)
{
if(Trigger.isAfter && trigger.isInsert || Trigger.isAfter && trigger.isUpdate )
{
//It will call a static method called ARPUInsert in ARPUCases class.
ARPUCases.ARPUInsert(Trigger.New);
}
if(Trigger.isAfter && trigger.isDelete)
{
//It will call a static method called ARPUInsert in ARPUCases class.
ARPUCases.ARPUDelete(Trigger.Old);
}
}
Handler class would be
/**
* This class is used to calculate average of Arpu in customer order record type
* with cases details either Add-on or New subscription and update in Related Account field called ARPU.
* This class is fire whenever record type called customer order case has been inserted or updated or deleted.
* @author Pavan Kumar
* @since 2016-09-28
*/
public class ARPUCases
{
/** @var recordTypeID - is to filter cases with particular record type */
/** @var Accountstoupdate - is to update the particular accounts */
/** @var CaseIDs - is to retrive all related case id's */
public static Set<Account> accSet = new Set<Account>();
public static void ARPUInsert(List<Case> CaseTriggers)
{
/**
* This method is to caluclate average of all ARPU amounts with case have customer order recordtype
* as well as case details with either ADD-On (OR) New Subscription and update in related account
* field called ARPU.
* This method will fire only when any case with record type called customer order is created as well updated.
*/
if (Trigger.isInsert || Trigger.isUpdate)
{
Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'Customer_Order'].Id;
List<Account> Accountstoupdate =new List<Account>();
List<Case> CaseIDs = new List<Case>();
for (Case s : CaseTriggers)
{
if(s.RecordTypeId == recordTypeId && s.Case_Details__c =='New Subscription' || s.Case_Details__c =='Add On' )
{
// Loop through and add caseId's to the list.
CaseIDs.add(s);
}
}
/** @Var CaseAccountIds - to get accountId's of cases with list called CaseIDs */
set<Id> CaseAccountIds = new set<Id>();
for(Case c : [SELECT Id,ARPU__c,accountId FROM Case WHERE Id IN :CaseIDs])
{
// Loop through and add AccountId's to the list.
CaseAccountIds.add(c.AccountId);
}
/** @Var Accountswithcases-to get account details to caluclate average */
/** @Var results-to get average of particular account based on the cases */
List<Account> Accountswithcases = [select Id,name,ARPU__c from Account where id =:CaseAccountIds];
Map<Id, AggregateResult> results = new Map<Id, AggregateResult>(
[SELECT AccountId Id, AVG(ARPU__c) average FROM Case WHERE AccountId = :Accountswithcases GROUP BY AccountId]);
For(account a1: Accountswithcases)
{
if(results.get(a1.Id) != null)
{
// Loop through aggregate results array and typecast the average of ARPU and update in account level.
a1.ARPU__c = (Decimal)results.get(a1.Id).get('average');
Accountstoupdate.add(a1);
}
}
if(Accountstoupdate.size()>0)
{
// DML statement to update all the accounts related with cases
update Accountstoupdate;
}
}
}
public static void ARPUDelete(List<Case> CaseTriggers)
{
/**
* This method is to caluclate average of all ARPU amounts with case have customer order recordtype
* as well as case details with either ADD-On (OR) New Subscription and update in related account
* field called ARPU.
* This method will fire only when any case with record type called customer order is deleted.
*/
if (Trigger.isDelete)
{
Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'Customer_Order'].Id;
List<Account> Accountstoupdate =new List<Account>();
List<Case> CaseIDs = new List<Case>();
for (Case s : CaseTriggers)
{
if(s.RecordTypeId == recordTypeId && s.Case_Details__c =='New Subscription' || s.Case_Details__c =='Add On')
{
// Loop through and add caseId's to the list.
CaseIDs.add(s);
}
}
System.debug('Data in CaseIDs:'+ CaseIDs.size());
System.debug('Data in details CaseIDs:'+ CaseIDs);
/** @Var CaseAccountIds - to get accountId's of cases with list called CaseIDs */
set<Id> CaseAccountIds = new set<Id>();
for(Case c : CaseTriggers)
{
// Loop through and add AccountId's to the list.
CaseAccountIds.add(c.AccountId);
}
System.debug('After Data in CaseAccountIds:'+ CaseAccountIds.size());
System.debug('After Data Details in CaseAccountIds:'+ CaseAccountIds);
/** @Var Accountswithcases-to get account details to caluclate average */
/** @Var results-to get average of particular account based on the cases */
List<Account> Accountswithcases = [select Id,name,ARPU__c from Account where id =:CaseAccountIds];
Map<Id, AggregateResult> results = new Map<Id, AggregateResult>(
[SELECT AccountId Id, AVG(ARPU__c) average FROM Case WHERE AccountId = :Accountswithcases GROUP BY AccountId]);
System.debug('Results:'+ results.size());
System.debug('Data in Accountswithcases:'+ Accountswithcases.size());
For(account a1: Accountswithcases)
{
if(results.get(a1.Id) != null )
{
// Loop through aggregate results array and typecast the average of ARPU and update in account level.
a1.ARPU__c = (Decimal)results.get(a1.Id).get('average');
Accountstoupdate.add(a1);
}
}
System.debug('After Results:'+ Accountstoupdate.size());
System.debug('Size of the Results:'+ Accountstoupdate);
if(Accountstoupdate.size()>0)
{
// DML statement to update all the accounts related with cases.
update Accountstoupdate;
}
}
}
}
I am attaching my debug logs
public static void ARPUDelete(List<Case> CaseTriggers)
{
/**
* This method is to caluclate average of all ARPU amounts with case have customer order recordtype
* as well as case details with either ADD-On (OR) New Subscription and update in related account
* field called ARPU.
* This method will fire only when any case with record type called customer order is deleted.
*/
if (Trigger.isDelete)
{
Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'Customer_Order'].Id;
List<Account> Accountstoupdate =new List<Account>();
List<Case> CaseIDs = new List<Case>();
for (Case s : CaseTriggers)
{
if(s.RecordTypeId == recordTypeId && s.Case_Details__c =='New Subscription' || s.Case_Details__c =='Add On')
{
// Loop through and add caseId's to the list.
CaseIDs.add(s);
}
}
System.debug('Data in CaseIDs:'+ CaseIDs.size());
System.debug('Data in details CaseIDs:'+ CaseIDs);
/** @Var CaseAccountIds - to get accountId's of cases with list called CaseIDs */
set<Id> CaseAccountIds = new set<Id>();
for(Case c : CaseTriggers)
{
// Loop through and add AccountId's to the list.
CaseAccountIds.add(c.AccountId);
}
System.debug('After Data in CaseAccountIds:'+ CaseAccountIds.size());
System.debug('After Data Details in CaseAccountIds:'+ CaseAccountIds);
/** @Var Accountswithcases-to get account details to caluclate average */
/** @Var results-to get average of particular account based on the cases */
List<Account> Accountswithcases = [select Id,name,ARPU__c from Account where id =:CaseAccountIds];
Map<Id, AggregateResult> results = new Map<Id, AggregateResult>(
[SELECT AccountId Id, AVG(ARPU__c) average FROM Case WHERE AccountId = :Accountswithcases GROUP BY AccountId]);
System.debug('Results:'+ results.size());
System.debug('Data in Accountswithcases:'+ Accountswithcases.size());
For(account a1: Accountswithcases)
{
if(results.get(a1.Id) != null )
{
// Loop through aggregate results array and typecast the average of ARPU and update in account level.
a1.ARPU__c = (Decimal)results.get(a1.Id).get('average');
Accountstoupdate.add(a1);
}
}
System.debug('After Results:'+ Accountstoupdate.size());
System.debug('Size of the Results:'+ Accountstoupdate);
if(Accountstoupdate.size()>0)
{
// DML statement to update all the accounts related with cases.
update Accountstoupdate;
}
}
}
}
On delete it is still showing 21 as a value because for the following reasons
If you see your code , when there is no case for related account it does not run the loop showing you the 21
to make it work , i think you need to do following
Hope this helps....