function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dhruv NirgudeDhruv Nirgude 

I wrote one Future metho. In my code i got some errors so I need some assistance with this problem. here is my code along with error notification.

public class CalculateCaseOrigin {
@future
    public static void countCasesBasedOnOrigin(List<Id> accIds){
  List<Account> accList = [Select Id, Cases_Through_Email__c, Cases_Through_Phone__c, Cases_Through_Web__c'
                          , (Select Id, Origin From Cases)
                          From Account where Id IN: accIds];       
     
        for (Account acc = accList){
       Integer Email=0, Web=0, Phone=0;     
       
            for (Case ca : acc.Cases){
                if(ca.Origin == 'Phone'){
                phone++;
                }else if(ca.Origin =='Web'){
                 Web++;
                }else if(ca.Origin == 'Email'){
                 Email++;   
                }    
                }       
              acc.Cases_Through_Email__c = Email;
              acc.Cases_Through_Phone__c = Phone;
              acc.Cases_Through_web__c = Web;
                  }     
       if(!accList.isEmpty)     
        update accList;
           }     
        }   
    
errors - 
*Missing closing quote character ' on string.

*Unexpected token '\n                          , (Select Id, Origin From Cases)\n                          From Account where Id IN: accIds];       \n     \n        for (Account acc = accList){\n       Integer Email=0, Web=0, Phone=0;     \n       \n            for (Case ca : acc.Cases){\n                if(ca.Origin == '.

*Expecting 'FROM' but was: '\n                          , (Select Id, Origin From Cases)\n                          From Account where Id IN: accIds];       \n     \n        for (Account acc = accList){\n       Integer Email=0, Web=0, Phone=0;     \n       \n            for (Case ca : acc.Cases){\n                if(ca.Origin == '

*Missing ';' at '){\n                phone++;\n                }else if(ca.Origin =='

*Illegal string literal: Line breaks are not allowed in string literals

*Missing ';' at '){\n                 Web++;\n                }else if(ca.Origin == '


*Illegal string literal: Line breaks are not allowed in string literals

*Expecting ';' but was: '<EOF>'

*Method must have a body
Best Answer chosen by Dhruv Nirgude
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Dhruv,

Can you try the below code.
 
public class CalculateCaseOrigin {
@future
    public static void countCasesBasedOnOrigin(List<Id> accIds){
  List<Account> accList = [Select Id,Cases_Through_Email__c, Cases_Through_Phone__c, Cases_Through_Web__c
                          , (Select Id, Origin From Cases)
                          From Account where Id IN: accIds];       
     
        for (Account acc : accList){
       Integer Email=0, Web=0, Phone=0;     
       
            for (Case ca : acc.Cases){
                if(ca.Origin == 'Phone'){
                phone++;
                }else if(ca.Origin =='Web'){
                 Web++;
                }else if(ca.Origin == 'Email'){
                 Email++;   
                }    
                }       
              acc.Cases_Through_Email__c = Email;
              acc.Cases_Through_Phone__c = Phone;
              acc.Cases_Through_web__c = Web;
                  }     
       if(!accList.isEmpty())     
        update accList;
           }     
        }   

If this solution helps, please mark it as bst answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Dhruv,

Can you try the below code.
 
public class CalculateCaseOrigin {
@future
    public static void countCasesBasedOnOrigin(List<Id> accIds){
  List<Account> accList = [Select Id,Cases_Through_Email__c, Cases_Through_Phone__c, Cases_Through_Web__c
                          , (Select Id, Origin From Cases)
                          From Account where Id IN: accIds];       
     
        for (Account acc : accList){
       Integer Email=0, Web=0, Phone=0;     
       
            for (Case ca : acc.Cases){
                if(ca.Origin == 'Phone'){
                phone++;
                }else if(ca.Origin =='Web'){
                 Web++;
                }else if(ca.Origin == 'Email'){
                 Email++;   
                }    
                }       
              acc.Cases_Through_Email__c = Email;
              acc.Cases_Through_Phone__c = Phone;
              acc.Cases_Through_web__c = Web;
                  }     
       if(!accList.isEmpty())     
        update accList;
           }     
        }   

If this solution helps, please mark it as bst answer.

Thanks,
 
This was selected as the best answer
Dhruv NirgudeDhruv Nirgude
After removing the code errors and run the program i saw failed status in Apex Jobs.

First error: Update failed. First exception on row 1 with id 0015g00000N8j5YAAR; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, For this particular combination we can not create more than one child record: []
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Dhruv,

This class is updating an account record that has more than 1 contact. But there is some validation that Account cannot have more than one constact because of which the class is failing.

You may need to remove the validation if you wish you don't need it or if validation is needed then it is working as expected.

Thanks,