You need to sign in to do that
Don't have an account?
Bhushan Singh 13
SObject row was retrieved via SOQL without querying the requested field: Contact.AccountId
Hi ,
I have written one trigger when account record update that time related contact record also should be update . while i am updating account then i am getting error -
" Error:
TriggerTesting: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.AccountId Trigger.TriggerTesting: line 20, column 1"
Trigger -
trigger TriggerTesting on Account (after update) {
Map<Id, Account> accountWithDes = new Map<Id, Account>();
for(integer i=0; i<Trigger.new.size(); i++)
{
if(Trigger.new[i].Description !=Trigger.old[i].Description )
{
accountWithDes.put(Trigger.old[i].id, Trigger.New[i]);
}
}
System.debug('Account Object '+accountWithDes);
List<Contact> updateContact = new List<Contact>();
List<Contact> contacts = [select id, Description from Contact where accountId in : accountWithDes.keySet()];
System.debug('Contact Object '+contacts);
for(Contact c : contacts )
{
Account ac = accountWithDes.get(c.accountId);
c.Description = ac.Description;
updateContact.add(c);
}
update updateContact;
}
Please help me , I am new in salesforce.
Thanks & Regards
Bhushan
I have written one trigger when account record update that time related contact record also should be update . while i am updating account then i am getting error -
" Error:
TriggerTesting: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.AccountId Trigger.TriggerTesting: line 20, column 1"
Trigger -
trigger TriggerTesting on Account (after update) {
Map<Id, Account> accountWithDes = new Map<Id, Account>();
for(integer i=0; i<Trigger.new.size(); i++)
{
if(Trigger.new[i].Description !=Trigger.old[i].Description )
{
accountWithDes.put(Trigger.old[i].id, Trigger.New[i]);
}
}
System.debug('Account Object '+accountWithDes);
List<Contact> updateContact = new List<Contact>();
List<Contact> contacts = [select id, Description from Contact where accountId in : accountWithDes.keySet()];
System.debug('Contact Object '+contacts);
for(Contact c : contacts )
{
Account ac = accountWithDes.get(c.accountId);
c.Description = ac.Description;
updateContact.add(c);
}
update updateContact;
}
Please help me , I am new in salesforce.
Thanks & Regards
Bhushan
Please update your code like below
NOTE:- you was forget to add accountid in contact query
List<Contact> contacts = [select id,accountId, Description from Contact where accountId in : accountWithDes.keySet()];
Let us know if this will help you
All Answers
The following code will solve your problem.
Just you need to add the accountId in your SOQL .
trigger TriggerTesting on Account (after update) {
Map<Id, Account> accountWithDes = new Map<Id, Account>();
for(integer i=0; i<Trigger.new.size(); i++)
{
if(Trigger.new[i].Description !=Trigger.old[i].Description )
{
accountWithDes.put(Trigger.old[i].id, Trigger.New[i]);
}
}
System.debug('Account Object '+accountWithDes);
List<Contact> updateContact = new List<Contact>();
List<Contact> contacts = [select id,accountId, Description from Contact where accountId in : accountWithDes.keySet()];
System.debug('Contact Object '+contacts);
for(Contact c : contacts )
{
Account ac = accountWithDes.get(c.accountId);
c.Description = ac.Description;
updateContact.add(c);
}
update updateContact;
}
Thanks
Please update your code like below
NOTE:- you was forget to add accountid in contact query
List<Contact> contacts = [select id,accountId, Description from Contact where accountId in : accountWithDes.keySet()];
Let us know if this will help you
Thank you very much for helping ,
I have added accountId in SOQL that problem resolved, but while updating the list of contact it is throwing exception -
" Error:
TriggerTesting: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0032800000oURWlAAO; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateAccount: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: 00128000016j29HAAQ Trigger.updateAccount: line 29, column 1: [] Trigger.TriggerTesting: line 30, column 1"
It is saying AccountId is duplicate but i am updating based on the ContactId, so why it is checking for AccountId, Please help me.
Thanks & Regards
Bhushan Singh
List<Contact> contacts = [select id, Description from Contact where accountId in : accountWithDes.keySet()];
System.debug('Contact Object '+contacts);
for(Contact c : contacts )
{
Account ac = accountWithDes.get(c.accountId); // Error was coming due to this line you was try to access accountID without SOQL
c.Description = ac.Description;
updateContact.add(c);
}
yes i was getting this issue previous, but now i am getting error on the line if(updateContact.size() > 0 )
{
update updateContact;// here i am getting error
}
It is because of duplicate accountId , but why it is looking for accountId of Contact object.
Please help.
Thanks & Regards
Bhushan
You are right , I have written updateAccount Trigger on the Contact, but how we can manage both trigger when i will written Trigger in the both Object,
ex - I want to write Trigger on Contact Object for update Account and vise versa.
Please guide me .
Thanks For helping immediatly.
Thanks & Regards
Bhushan Singh