function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sarthak Patil 15Sarthak Patil 15 

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.
Best Answer chosen by Sarthak Patil 15
Danish HodaDanish Hoda
Hi Sarthak,
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:
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; 
        }
        }*/
		Map<Id, Acount> mapAcc = new Map<Id, Acount>();
		for(Contact c : Trigger.New){
			mapAcc.put(c.AccountId, null);
		}
		for(Account acc : [SELECT Id, Phone FROM Account WHERE Id IN :mapAcc.keySet()]){
			mapAcc.put(acc.Id, acc);
		}
		for(Contact c : Trigger.New){
			c.otherPhone = mapAcc.get(c.AccountId).Phone;
		}
}

 

All Answers

Danish HodaDanish Hoda
Hi Sarthak,
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:
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; 
        }
        }*/
		Map<Id, Acount> mapAcc = new Map<Id, Acount>();
		for(Contact c : Trigger.New){
			mapAcc.put(c.AccountId, null);
		}
		for(Account acc : [SELECT Id, Phone FROM Account WHERE Id IN :mapAcc.keySet()]){
			mapAcc.put(acc.Id, acc);
		}
		for(Contact c : Trigger.New){
			c.otherPhone = mapAcc.get(c.AccountId).Phone;
		}
}

 
This was selected as the best answer
Sarthak Patil 15Sarthak Patil 15

Thanks @Danish Hoda for response.
I have also created a trigger with different approach(Not using map):

Trigger TestNewCon on Contact (before insert){
        
        set<Id> setAccId = new set<Id>();
        for(Contact c: Trigger.new){
        setAccId.add(c.AccountId);
        }
       List<Account> NewAcc = new  List<Account>([Select id,phone,(Select name,otherphone from Contacts) from Account where id in :setAccId]);
        
        for(Account Acc : NewAcc){
        for(Contact c: Trigger.new){
            System.debug('Acc.phone '+ Acc.phone );
            c.otherphone = Acc.phone;
            System.debug('c.otherphone '+ c.otherphone );
            }
        }
}