+ Start a Discussion
sam_Adminsam_Admin 

Exception in trigger while editing a record

I got this error when i edit the record, how to fix it? Apex trigger AutoforwardOpp caused an unexpected exception, contact your administrator: AutoforwardOpp: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.AccountId: Trigger.AutoforwardOpp: line 91, column 1

Trigger AutoforwardOpp on Opportunity(after insert, after update)
{
    List <PartnerNetworkRecordConnection> prncList;
    List <PartnerNetworkConnection> connectionList;
    Map <ID, PartnerNetworkConnection> connMap;
    Map <ID, ID> oppIdvsAccountIdMap;
    Map<ID, Account> accountWithContactMap;
    
    ID cid;
    String status;
    String connName;

    if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)){
            
        connectionList = new List<PartnerNetworkConnection>();
        prncList = new List<PartnerNetworkRecordConnection>();
        
        //This would ideally return multiple active Connections if they exist hence its best you use a 
        //criteria to ensure only the appropriate connection record is returned. 
        connMap = new Map<ID, PartnerNetworkConnection>(
            [Select ID, ConnectionStatus, ConnectionName 
             From PartnerNetworkConnection 
             Where ConnectionStatus = 'Accepted']);
        
        //get connection details        
        for(ID connId :connMap.keySet()){
            cid = connMap.get(connId).ID;
            status = connMap.get(connId).ConnectionStatus;
            connName = connMap.get(connId).ConnectionName;
        }
        
        //Populate a map of Opp Ids and associated Account Ids
        oppIdvsAccountIdMap = new Map<ID, ID>();
        for(Opportunity oppRecord :Trigger.new){
        
            if(oppRecord.Department__c == 'US'){
                    //System.debug('Opp Id: ' + oppRecord.ID + '-> Account Id: ' + oppRecord.AccountId);
                    oppIdvsAccountIdMap.put(oppRecord.ID, oppRecord.AccountId);
            }
        }
        
        //Get associated Accounts and Contacts for every US opportunity if they exist
        if(oppIdvsAccountIdMap.keySet().size() > 0){
        
            accountWithContactMap = new Map<ID, Account>(
                [Select ID, Name,
                    (Select ID,Name From Account.Contacts)
                 From Account
                 Where ID IN :oppIdvsAccountIdMap.values()]);

            //Create PartnerNetworkRecordConnections for sharing the records
            for(ID oppId : oppIdvsAccountIdMap.keySet()){
            
                ID accountId = oppIdvsAccountIdMap.get(oppId);
                
                //Share Opportunity
                prncList.add(new PartnerNetworkRecordConnection(
                    ConnectionId = cid,
                    LocalRecordId = oppId,
                    SendClosedTasks = true,
                    SendOpenTasks = true,
                    SendEmails = true));
                
                //Share Account
                if(oppIdvsAccountIdMap.get(oppId) != null){
                
                    prncList.add(new PartnerNetworkRecordConnection(
                        ConnectionId = cid,
                        LocalRecordId = accountId,
                        SendClosedTasks = true,
                        SendOpenTasks = true,
                        SendEmails = true));
                }
                
                //Share associated Contacts
                if(accountWithContactMap.get(accountId).Contacts != null){
                                   
                    for(Contact contact :accountWithContactMap.get(accountId).Contacts){
                    
                        prncList.add(new PartnerNetworkRecordConnection(
                            ConnectionId = cid,
                            LocalRecordId = contact.ID,
                            ParentRecordId = Contact.AccountId,
                            SendClosedTasks = true,
                            SendOpenTasks = true,
                            SendEmails = true));
                    }
                }
            }//for
            
            //Insert record conneections
            if(!prncList.isEmpty()){
            
                try{
                    insert prncList;
                }
                catch(System.Dmlexception dmlExceptionInstance){
                    System.debug('Record Share Error:' + dmlExceptionInstance.getMessage());
                }
            }
        }//if
    }
}
Best Answer chosen by sam_Admin
jigarshahjigarshah
I believe the query refers in an incorrect way to access the respective Contact record's Account ID value. Use the below query. I just ran it in the Developer Console at my end and it works.
accountWithContactMap = new Map<ID, Account>(
                [Select ID, Name,
                    (Select ID, Name, Account.Id From Account.Contacts)
                 From Account
                 Where ID IN :oppIdvsAccountIdMap.values()]);

I beldieve this should fix the issue for you and if not use the Debug Log to print and view the value of accountId and accountWithContactMap.get(accountId).Contacts for each associated Account record.

All Answers

sam_Adminsam_Admin
Line 91 is   ParentRecordId = Contact.AccountId,
Maharajan CMaharajan C
Hi Sam,

Please try this small change in SOQL Line:

accountWithContactMap = new Map<ID, Account>(
                [Select ID, Name,
                    (Select ID,Name,AccountId From Account.Contacts)
                 From Account
                 Where ID IN :oppIdvsAccountIdMap.values()]);

Can you please Let me know if it works or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
​Raj
jigarshahjigarshah
The error occurs because of the AccountId field on the Contact record is being used without fetching it in the Soql Query. All you need to do is include the AccountId field within the Select clause of your Soql statement as shown below and it will fix the issue.
accountWithContactMap = new Map<ID, Account>(
                [Select ID, Name,
                    (Select ID, Name, AccountId From Account.Contacts)
                 From Account
                 Where ID IN :oppIdvsAccountIdMap.values()]);
Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if this answer helps address your issue.
sam_Adminsam_Admin
Thank you both i don't see the error but now the record is not getting forwarded at all, what caused the breakdown?
sam_Adminsam_Admin
It's only forwarding the records if the account related to opportunity doesn't have any contacts, if the contacts exists then the opportunity is not getting forwarded
jigarshahjigarshah
I believe the query refers in an incorrect way to access the respective Contact record's Account ID value. Use the below query. I just ran it in the Developer Console at my end and it works.
accountWithContactMap = new Map<ID, Account>(
                [Select ID, Name,
                    (Select ID, Name, Account.Id From Account.Contacts)
                 From Account
                 Where ID IN :oppIdvsAccountIdMap.values()]);

I beldieve this should fix the issue for you and if not use the Debug Log to print and view the value of accountId and accountWithContactMap.get(accountId).Contacts for each associated Account record.
This was selected as the best answer
sam_Adminsam_Admin
I updated it but still it's not forwarding the record
jigarshahjigarshah
Check the Debug Log to print and view the value of accountId and accountWithContactMap.get(accountId).Contacts for each associated Account record. That might help and provide direction on the actual root cause.