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 

why am i not be able to save my records in Account object?

Can’t Save Record

We can't save this record because the “Quick Action In Account” process failed. Give your Salesforce admin these details. OpportunityTrigger: execution of AfterInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Opportunity.AccountId Class.OppoTriggerHandler.calculateAnnualRevenue: line 53, column 1 Class.OppoTriggerHandler.onAfterInsert: line 11, column 1 Trigger.OpportunityTrigger: line 4, column 1 Error ID: 851754416-130695 (1954185174) 11, column 1 Trigger.OpportunityTrigger: line 4, column 1 Error ID: 851754416-130695 (1954185174).
 
Best Answer chosen by Dhruv Nirgude
AnkaiahAnkaiah (Salesforce Developers) 
Hi Druv,

You need to add the Opportunity.AccountId field in the soql query to resolve this issue.

check where you are quering the opportunity fields and include the AccountId field in that query.

If this helps, Please mark it as best answer.

Thanks!!

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Druv,

You need to add the Opportunity.AccountId field in the soql query to resolve this issue.

check where you are quering the opportunity fields and include the AccountId field in that query.

If this helps, Please mark it as best answer.

Thanks!!
This was selected as the best answer
Dhruv NirgudeDhruv Nirgude
Hey Ankaiah, 
Here is my code where should i put it?

public class OppoTriggerHandler {
    public static void onAfterInsert(List<opportunity> newList){ 
    Set<Id> accIds = new  Set<Id>();
    List<Account> accToBeUpdated= new List<Account>();
     
       for(Opportunity opp : newList){
           if(opp.AccountId != null){
           accIds.add(opp.AccountId);    
        }      
    }    
 accToBeUpdated = calculateAnnualRevenue(accIds);
        if(!accToBeUpdated.isEmpty()){

            update accToBeUpdated;
        }    
    }
    public static void onAfterUpdate(List<Opportunity> newList, Map<Id,Opportunity> oldMap){
    Set<Id> accIds = new Set<Id>();
    List<Account> accToBeUpdated = new List<Account>();
        
        for(Opportunity opp : newList){
            if(opp.AccountId != null && opp.Amount != oldMap.get(opp.Id).Amount){
             accIds.add(opp.AccountId);     
            }   
        }
  accToBeUpdated = calculateAnnualRevenue(accIds);
 
        if(!accToBeUpdated.isEmpty()){ 
         update accToBeUpdated;
        }
    }
    public static void OnAfterDelete (List<Opportunity> oldList){
    Set<Id> accIds = new Set<Id>();
   List<Account> accToBeUpdated = new List<Account>();
        
        for(Opportunity opp : oldList){
            if(opp.AccountId != null){
             accIds.add(opp.AccountId);      
            }     
        }      
  accToBeUpdated = calculateAnnualRevenue(accIds);
   
        if(!accToBeUpdated.isEmpty()){
        update accToBeUpdated;
        }      
    }     
    public static List<Account> calculateAnnualRevenue(Set<Id> accIds){
    List<Account> accToBeUpdated = new List<Account>(); 
   Map<Id, Decimal> accIdToAnnualRevenue = new Map<Id, Decimal>();
   
        for(Opportunity opp : [Select Id, Amount From Opportunity Where Opportunity.AccountId IN : accIds]){
      Decimal total = 0;
            if(accIdToAnnualRevenue.containsKey(opp.AccountId)){
          total = accIdToAnnualRevenue.get(opp.AccountId);
            }
            if(opp.Amount != null){
          total = total +opp.Amount;
            }         
     accIdToAnnualRevenue.put(opp.AccountId,total);
        }
        if(!accIdToAnnualRevenue.isEmpty()){
            for(Id i : accIdToAnnualRevenue.keySet()){
          Account acc = new Account();
          acc.Id = i;
         acc.AnnualRevenue = accIdToAnnualRevenue.get(i);       
        accToBeUpdated.add(acc);   
            }       
        }   
    return accToBeUpdated;    
    }  
}
AnkaiahAnkaiah (Salesforce Developers) 
Hi Druv,

You need to modify the below.
from
Select Id, Amount From Opportunity Where Opportunity.AccountId IN : accIds]
to
Select Id,AccountId, Amount From Opportunity Where AccountId IN : accIds]

Try with below code.
public class OppoTriggerHandler {
    public static void onAfterInsert(List<opportunity> newList){ 
    Set<Id> accIds = new  Set<Id>();
    List<Account> accToBeUpdated= new List<Account>();
     
       for(Opportunity opp : newList){
           if(opp.AccountId != null){
           accIds.add(opp.AccountId);    
        }      
    }    
 accToBeUpdated = calculateAnnualRevenue(accIds);
        if(!accToBeUpdated.isEmpty()){

            update accToBeUpdated;
        }    
    }
    public static void onAfterUpdate(List<Opportunity> newList, Map<Id,Opportunity> oldMap){
    Set<Id> accIds = new Set<Id>();
    List<Account> accToBeUpdated = new List<Account>();
        
        for(Opportunity opp : newList){
            if(opp.AccountId != null && opp.Amount != oldMap.get(opp.Id).Amount){
             accIds.add(opp.AccountId);     
            }   
        }
  accToBeUpdated = calculateAnnualRevenue(accIds);
 
        if(!accToBeUpdated.isEmpty()){ 
         update accToBeUpdated;
        }
    }
    public static void OnAfterDelete (List<Opportunity> oldList){
    Set<Id> accIds = new Set<Id>();
   List<Account> accToBeUpdated = new List<Account>();
        
        for(Opportunity opp : oldList){
            if(opp.AccountId != null){
             accIds.add(opp.AccountId);      
            }     
        }      
  accToBeUpdated = calculateAnnualRevenue(accIds);
   
        if(!accToBeUpdated.isEmpty()){
        update accToBeUpdated;
        }      
    }     
    public static List<Account> calculateAnnualRevenue(Set<Id> accIds){
    List<Account> accToBeUpdated = new List<Account>(); 
   Map<Id, Decimal> accIdToAnnualRevenue = new Map<Id, Decimal>();
   
        for(Opportunity opp : [Select Id,AccountId, Amount From Opportunity Where AccountId IN : accIds]){
      Decimal total = 0;
            if(accIdToAnnualRevenue.containsKey(opp.AccountId)){
          total = accIdToAnnualRevenue.get(opp.AccountId);
            }
            if(opp.Amount != null){
          total = total +opp.Amount;
            }         
     accIdToAnnualRevenue.put(opp.AccountId,total);
        }
        if(!accIdToAnnualRevenue.isEmpty()){
            for(Id i : accIdToAnnualRevenue.keySet()){
          Account acc = new Account();
          acc.Id = i;
         acc.AnnualRevenue = accIdToAnnualRevenue.get(i);       
        accToBeUpdated.add(acc);   
            }       
        }   
    return accToBeUpdated;    
    }  
}

If this helps, Please mark it as best answer.

Thanks!!