You need to sign in to do that
Don't have an account?

Is this possible to fetch the list of account id which are in trigger.new(Contact--object).
Hi everybody,
I am trying to write the following trigger on contact.
"When a new contact is created for a existing account then set contact otherphone as account phone."
I have written this which is working fine.
Trigger TestNewCon on Contact (before insert){
for(Contact c : Trigger.new){
for(Account acc: [Select id,phone from Account where id = :c.AccountId]){
c.otherphone = acc.phone;
}
}
Is there any other way to write this trigger because i want to avoid writing SOQL query inside for loop(NOT a best practise).
My plan is to fetch all the Accounts which are related to contacts first.
I am trying to write the following trigger on contact.
"When a new contact is created for a existing account then set contact otherphone as account phone."
I have written this which is working fine.
Trigger TestNewCon on Contact (before insert){
for(Contact c : Trigger.new){
for(Account acc: [Select id,phone from Account where id = :c.AccountId]){
c.otherphone = acc.phone;
}
}
Is there any other way to write this trigger because i want to avoid writing SOQL query inside for loop(NOT a best practise).
My plan is to fetch all the Accounts which are related to contacts first.
You are right, you must not use SOQL inside a loop and always use trigger so as to process bulk data.
Please refer below code for your understanding:
All Answers
You are right, you must not use SOQL inside a loop and always use trigger so as to process bulk data.
Please refer below code for your understanding:
Thanks @Danish Hoda for response.
I have also created a trigger with different approach(Not using map):