You need to sign in to do that
Don't have an account?
sumit d
total case age of all cases related to account in an account field
Hi All,
i have a Batch in which I want that the total of (Case_Age_Days__c) for all Cases related to account should populate in a field of account.
Can anyone help me out with this ?
public without sharing class BatchServiceScore implements Database.Batchable<sObject>,Database.Stateful{
public static Date backDate = System.Today().addDays(-365);
public static void run( Set<Id> AccIds ) {
List<Account> accountRecords = [ SELECT Id, Service_Score__c,NPS_Score__c,Customer_Satisfaction_Score__c,
Customer_Effort_Score__c
FROM Account WHERE ID IN : AccIds
];
executeHelper( accountRecords );
}
public Database.querylocator start(Database.BatchableContext BC){
return Database.getQueryLocator([ SELECT Id, Service_Score__c,NPS_Score__c,Customer_Satisfaction_Score__c,
Customer_Effort_Score__c
FROM Account
]);
}
public void execute(Database.BatchableContext BC, List<Account> scope){
executeHelper( scope );
}
public static void executeHelper( List<Account> scope ) {
List<Account> accsToUpdate = new List<Account>();
Map<Id, Account> mapAccountsWithCases = new Map<Id, Account>([ Select Id,
( SELECT Id, AccountId, Case_Age_Days__c,
Is_Negative_Case__c,Total_Case_Minutes_Per_Month__c
FROM Cases
WHERE CreatedDate >=: System.today().addDays(-365)
)
From Account
Where Id in :scope
]);
for( Account acct : scope ){
Decimal avgCaseAgeScore = 0;
Decimal caseHoursCountScore = 0;
Decimal caseRecordCountScore = 0;
Decimal service_Score = 0;
List<Case> accountCases = mapAccountsWithCases.get( acct.Id ).Cases;
accountCases = accountCases == null ? new List<Case>() : accountCases;
Decimal totalCases = accountCases.size();
System.debug('Total cases:-'+totalCases);
if( totalCases == 0 ){
caseRecordCountScore = 0;
}
else if( totalCases <= 5 ){
caseRecordCountScore = 25;
}
else if( totalCases <= 15 ){
caseRecordCountScore = 20;
}
else if( totalCases <= 30 ){
caseRecordCountScore = 10;
}
else if( totalCases > 30 ){
caseRecordCountScore = 5;
}
System.debug('caseRecordCountScore:-'+caseRecordCountScore);
Decimal AccountCaseAge = 0;
Decimal AvgAccCaseAge = 0;
Decimal CaseHoursCount = 0;
for (Case c : accountCases){
if(c.AccountId == acct.id){
AccountCaseAge = c.Case_Age_Days__c;
CaseHoursCount = c.Total_Case_Minutes_Per_Month__c;
}
}
System.debug('Case Age'+AccountCaseAge);
System.debug('Case Minutes:- '+CaseHoursCount);
AvgAccCaseAge = AccountCaseAge / totalCases;
System.debug('Case avreageAge:-'+AvgAccCaseAge);
if( AvgAccCaseAge <= 7 ){
avgCaseAgeScore = 25;
}
else if( AvgAccCaseAge <= 14 ){
avgCaseAgeScore = 20;
}
else if( AvgAccCaseAge <= 31 ){
avgCaseAgeScore = 10;
}
else if( AvgAccCaseAge <= 45 ){
avgCaseAgeScore = 5;
}
else if( AvgAccCaseAge > 45 ){
avgCaseAgeScore = 0;
}
System.debug('Case avreageAge Score :-'+avgCaseAgeScore);
if( CaseHoursCount == 0 ){
caseHoursCountScore = 0;
}
else if( CaseHoursCount <= 60 ){
caseHoursCountScore = 25;
}
else if( CaseHoursCount <= 300 ){
caseHoursCountScore = 20;
}
else if( CaseHoursCount <= 900 ){
caseHoursCountScore = 10;
}
else if( CaseHoursCount > 900 ){
caseHoursCountScore = 5;
}
System.debug('Case Hours Score :- '+caseHoursCountScore);
System.debug('service_Score after case review : '+service_Score);
if( acct.Service_Score__c != null
|| acct.Service_Score__c != service_Score ){
Decimal npsScoreContribution = 0;
if(acct.NPS_Score__c != null){
npsScoreContribution = acct.NPS_Score__c * 1.5;
}
Decimal CustomerEffortScore = 0;
if(acct.Customer_Effort_Score__c == 1){
CustomerEffortScore = 5;
}
else if(acct.Customer_Effort_Score__c == 2){
CustomerEffortScore = 4;
}
else if(acct.Customer_Effort_Score__c == 3){
CustomerEffortScore = 3;
}
else if(acct.Customer_Effort_Score__c == 4){
CustomerEffortScore = 2;
}
else if(acct.Customer_Effort_Score__c == 5){
CustomerEffortScore = 1;
}
System.debug('CustomerEffortScore after case review : '+CustomerEffortScore);
Decimal customerSatisfactionScore = 0;
if(acct.Customer_Satisfaction_Score__c != null){
customerSatisfactionScore = acct.Customer_Satisfaction_Score__c / 2.0;
System.debug('CustomerSatScore after case review : '+ customerSatisfactionScore);
}
acct.Service_Score__c = avgCaseAgeScore + caseHoursCountScore + caseRecordCountScore
+ npsScoreContribution + customerSatisfactionScore + customerEffortScore;
accsToUpdate.add(acct);
}
System.debug(' Service Score after : '+ acct.Service_Score__c);
}
if( accsToUpdate.size() > 0 ){
update accsToUpdate;
}
}
public void finish(Database.BatchableContext BC){
if( !Test.isRunningTest() ){
BatchOnAccountForMarketingActivityScore bch = new BatchOnAccountForMarketingActivityScore();
Database.executeBatch( bch, 1 );
}
}
}
i have a Batch in which I want that the total of (Case_Age_Days__c) for all Cases related to account should populate in a field of account.
Can anyone help me out with this ?
public without sharing class BatchServiceScore implements Database.Batchable<sObject>,Database.Stateful{
public static Date backDate = System.Today().addDays(-365);
public static void run( Set<Id> AccIds ) {
List<Account> accountRecords = [ SELECT Id, Service_Score__c,NPS_Score__c,Customer_Satisfaction_Score__c,
Customer_Effort_Score__c
FROM Account WHERE ID IN : AccIds
];
executeHelper( accountRecords );
}
public Database.querylocator start(Database.BatchableContext BC){
return Database.getQueryLocator([ SELECT Id, Service_Score__c,NPS_Score__c,Customer_Satisfaction_Score__c,
Customer_Effort_Score__c
FROM Account
]);
}
public void execute(Database.BatchableContext BC, List<Account> scope){
executeHelper( scope );
}
public static void executeHelper( List<Account> scope ) {
List<Account> accsToUpdate = new List<Account>();
Map<Id, Account> mapAccountsWithCases = new Map<Id, Account>([ Select Id,
( SELECT Id, AccountId, Case_Age_Days__c,
Is_Negative_Case__c,Total_Case_Minutes_Per_Month__c
FROM Cases
WHERE CreatedDate >=: System.today().addDays(-365)
)
From Account
Where Id in :scope
]);
for( Account acct : scope ){
Decimal avgCaseAgeScore = 0;
Decimal caseHoursCountScore = 0;
Decimal caseRecordCountScore = 0;
Decimal service_Score = 0;
List<Case> accountCases = mapAccountsWithCases.get( acct.Id ).Cases;
accountCases = accountCases == null ? new List<Case>() : accountCases;
Decimal totalCases = accountCases.size();
System.debug('Total cases:-'+totalCases);
if( totalCases == 0 ){
caseRecordCountScore = 0;
}
else if( totalCases <= 5 ){
caseRecordCountScore = 25;
}
else if( totalCases <= 15 ){
caseRecordCountScore = 20;
}
else if( totalCases <= 30 ){
caseRecordCountScore = 10;
}
else if( totalCases > 30 ){
caseRecordCountScore = 5;
}
System.debug('caseRecordCountScore:-'+caseRecordCountScore);
Decimal AccountCaseAge = 0;
Decimal AvgAccCaseAge = 0;
Decimal CaseHoursCount = 0;
for (Case c : accountCases){
if(c.AccountId == acct.id){
AccountCaseAge = c.Case_Age_Days__c;
CaseHoursCount = c.Total_Case_Minutes_Per_Month__c;
}
}
System.debug('Case Age'+AccountCaseAge);
System.debug('Case Minutes:- '+CaseHoursCount);
AvgAccCaseAge = AccountCaseAge / totalCases;
System.debug('Case avreageAge:-'+AvgAccCaseAge);
if( AvgAccCaseAge <= 7 ){
avgCaseAgeScore = 25;
}
else if( AvgAccCaseAge <= 14 ){
avgCaseAgeScore = 20;
}
else if( AvgAccCaseAge <= 31 ){
avgCaseAgeScore = 10;
}
else if( AvgAccCaseAge <= 45 ){
avgCaseAgeScore = 5;
}
else if( AvgAccCaseAge > 45 ){
avgCaseAgeScore = 0;
}
System.debug('Case avreageAge Score :-'+avgCaseAgeScore);
if( CaseHoursCount == 0 ){
caseHoursCountScore = 0;
}
else if( CaseHoursCount <= 60 ){
caseHoursCountScore = 25;
}
else if( CaseHoursCount <= 300 ){
caseHoursCountScore = 20;
}
else if( CaseHoursCount <= 900 ){
caseHoursCountScore = 10;
}
else if( CaseHoursCount > 900 ){
caseHoursCountScore = 5;
}
System.debug('Case Hours Score :- '+caseHoursCountScore);
System.debug('service_Score after case review : '+service_Score);
if( acct.Service_Score__c != null
|| acct.Service_Score__c != service_Score ){
Decimal npsScoreContribution = 0;
if(acct.NPS_Score__c != null){
npsScoreContribution = acct.NPS_Score__c * 1.5;
}
Decimal CustomerEffortScore = 0;
if(acct.Customer_Effort_Score__c == 1){
CustomerEffortScore = 5;
}
else if(acct.Customer_Effort_Score__c == 2){
CustomerEffortScore = 4;
}
else if(acct.Customer_Effort_Score__c == 3){
CustomerEffortScore = 3;
}
else if(acct.Customer_Effort_Score__c == 4){
CustomerEffortScore = 2;
}
else if(acct.Customer_Effort_Score__c == 5){
CustomerEffortScore = 1;
}
System.debug('CustomerEffortScore after case review : '+CustomerEffortScore);
Decimal customerSatisfactionScore = 0;
if(acct.Customer_Satisfaction_Score__c != null){
customerSatisfactionScore = acct.Customer_Satisfaction_Score__c / 2.0;
System.debug('CustomerSatScore after case review : '+ customerSatisfactionScore);
}
acct.Service_Score__c = avgCaseAgeScore + caseHoursCountScore + caseRecordCountScore
+ npsScoreContribution + customerSatisfactionScore + customerEffortScore;
accsToUpdate.add(acct);
}
System.debug(' Service Score after : '+ acct.Service_Score__c);
}
if( accsToUpdate.size() > 0 ){
update accsToUpdate;
}
}
public void finish(Database.BatchableContext BC){
if( !Test.isRunningTest() ){
BatchOnAccountForMarketingActivityScore bch = new BatchOnAccountForMarketingActivityScore();
Database.executeBatch( bch, 1 );
}
}
}