Apex trigger contactaccountrelation caused an unexpected exception, contact your administrator: contactaccountrelation: System.LimitException: Too many SOQL queries: 101
trigger contactaccountrelation on Contact (before insert) {
list acc = [select id, name from account]; for(account a : acc){
for(contact con : [select lastname, accountid, other phone from contact where accountid =: a.id])
{ con.OtherPhone = a.Phone; } } }
Your this [select lastname, accountid, other phone from contact where accountid =: a.id] query inside the loop which is causing the isuue System.LimitException: Too many SOQL queries: 101. Its never a best practice to have query inside the loop.
Second issue with your code is:
you are pulling every account in this query select id, name from account which can give issue when is there is more record try to use filter in query as per you requirement.
[select lastname, accountid, other phone from contact where accountid =: a.id] query inside the loop which is causing the isuue System.LimitException: Too many SOQL queries: 101. Its never a best practice to have query inside the loop.
Second issue with your code is:
you are pulling every account in this query select id, name from account which can give issue when is there is more record try to use filter in query as per you requirement.
If it helps please mark it as best answer.
Thanks!
Please use the below trigger:
Don't use the SOQL Query inside for loop... Write the trigger in bulkified manner to avoid these kind of exceptions...
Thanks,
Maharajan.C