When Opportunity is created for Contact, get Contact details for Opportunity

 When Opportunity is created for Contact, get Contact details for Opportunity.


Use this as reference, write your own code -


trigger OpportunityTrigger on Opportunity (before insert) {
Set<id> conIds = new Set<id>();
for(Opportunity opt : Trigger.new){
conIds.add(opt.Contact__c);
}
Map<Id,Contact> conMap = new Map<Id,Contact>();
List<Contact> contacts = [SELECT id,Phone,Email FROM Contact WHERE Id IN : conIds];
for(Contact con : contacts){
conMap.put(con.Id, con);

}
for(Opportunity opt:trigger.new){
if(opt.Contact__c != null)
{
opt.Phone__c =conMap.get(opt.Contact__c).phone;
opt.Email__c =conMap.get(opt.Contact__c).Email;
}
}
}

Comments