You need to sign in to do that
Don't have an account?
sam_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
}
}
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
}
}
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
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
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.