+ Start a Discussion
smriti sharan19smriti sharan19 

When a opportunity is created then billing address of accounts should get updated into the opportunity?

When a opportunity is created then billing address of accounts should get updated into the opportunity?

I have created a custom field on opportunity object BilingAddress__c. Now i want that whenever i create a new opportunity then the billing field on opportunity gets updated with the BillingAddress field of account.

Step 1: Trigger will be created on Opportunity object
Step 2. We will use after insert as it is based on two objects.

Can you give me code for this.
Best Answer chosen by smriti sharan19
Raj VakatiRaj Vakati
You no need trigger .. you can able to do it with the process builder .. please refer this image 


User-added image


Here is the trigger for you 

 
trigger oppTrigger on Opportunity (before insert) {
   
    List<Id> ids = new List<Id>();
   for(Opportunity opp : trigger.new){
     ids.add(opp.AccountId) ;
   }  
  
  // Add all fields to SOQL 
  
  Map<ids, Account> m = new Map<ids, Account>([SELECT Id, Name ,BilingAddress__c FROM Account where id in : ids]);
   for(Opportunity opp : trigger.new){
     Account a = m.get(opp.AccountId) ;
	 
	 opp.BilingAddress__c = a.BilingAddress__c ;
	 
   } 
   
   
  

}

 

All Answers

Raj VakatiRaj Vakati
You no need trigger .. you can able to do it with the process builder .. please refer this image 


User-added image


Here is the trigger for you 

 
trigger oppTrigger on Opportunity (before insert) {
   
    List<Id> ids = new List<Id>();
   for(Opportunity opp : trigger.new){
     ids.add(opp.AccountId) ;
   }  
  
  // Add all fields to SOQL 
  
  Map<ids, Account> m = new Map<ids, Account>([SELECT Id, Name ,BilingAddress__c FROM Account where id in : ids]);
   for(Opportunity opp : trigger.new){
     Account a = m.get(opp.AccountId) ;
	 
	 opp.BilingAddress__c = a.BilingAddress__c ;
	 
   } 
   
   
  

}

 
This was selected as the best answer
smriti sharan19smriti sharan19
We can do this using  
Workflow, process builder, formula field, flow,trigger. Which is the best way to do it? 
smriti sharan19smriti sharan19
User-added image

It throws error - Error: Compile Error: Invalid type: ids at line 10 column 3
Raj VakatiRaj Vakati
Go for Process Builder 

try this code
 
trigger oppTrigger on Opportunity (before insert) {
   
    List<String> ids = new List<String>();
   for(Opportunity opp : trigger.new){
     ids.add(opp.AccountId) ;
   }  
  
  // Add all fields to SOQL 
  
  Map<ids, Account> m = new Map<ids, Account>([SELECT Id, Name ,BilingAddress__c FROM Account where Id in : ids]);
   for(Opportunity opp : trigger.new){
     Account a = m.get(opp.AccountId) ;
	 
	 opp.BilingAddress__c = a.BilingAddress__c ;
	 
   } 
   
   
  

}


 
Raj VakatiRaj Vakati
use this code pls
 
trigger oppTrigger on Opportunity (before insert) {
    
    Set<String> ids = new Set<String>();
    for(Opportunity opp : trigger.new){
        ids.add(opp.AccountId) ;
    }  
    
    // Add all fields to SOQL 
    
    Map<Id, Account> m = new Map<Id, Account>([SELECT Id, Name ,BilingAddress__c FROM Account Where Id IN: ids]);
    for(Opportunity opp : trigger.new){
        Account a = m.get(opp.AccountId) ;
        
        opp.BilingAddress__c = a.BilingAddress__c ;
        
    } 
    
    
    
    
}

 
smriti sharan19smriti sharan19
User-added image
Hi I am getting this error. 
BilingAddress__c  is a custom field in contact
BillingAddress is standard field in account
Raj VakatiRaj Vakati
BillingAddress  is compound field and you cannt use it direcly .. use it as BillingCity ,BillingStreet ,BillingZip etc
Abdul KhatriAbdul Khatri
Hi,

I am not sure why we wanted to go through the hassle of Trigger, Process Builder etc. when we can achieve this through a simple formula field. Convert your BillingAddress__c Field to Formula Field like mentioned below. This way as soon as you create an opportunity with Acccount tie to ti this field will automatically get populated with the Account Billing Address if defined. Also the beauty of this if there is any change on the Account level you will see the same here.

User-added image

Here is the code if it is not clear in the image
Account.BillingStreet + BR() + Account.BillingCity + " " + Account.BillingState + " - " +  Account.BillingPostalCode + BR() +  Account.BillingCountry

Please explain me the need of trigger for my knowledge otherwise I see it a best solution. Thanks.