You need to sign in to do that
Don't have an account?
pavan kumar 177
Trigger is not working accurately
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 fine in sandbox but in production when i update any old cases it's not updating accurately.If i do update in sandbox is working fine.
And for example there is case with above criteria like 18.55.But it updating 6.55.When i check from random accounts it is calculating average with all cases to a respective account.
When i try to create any cases with new accounts it is working fine.Please help me guys to sort out.
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);
}
}
Handler trigger
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.
*/
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;
}
}
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 fine in sandbox but in production when i update any old cases it's not updating accurately.If i do update in sandbox is working fine.
And for example there is case with above criteria like 18.55.But it updating 6.55.When i check from random accounts it is calculating average with all cases to a respective account.
When i try to create any cases with new accounts it is working fine.Please help me guys to sort out.
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);
}
}
Handler trigger
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.
*/
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;
}
}
pavan kumar 177
I tried to check from debug logs.In aggregate query it's calculating average with all the cases in account.