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
Ajosh JohnAjosh John 

Apex trigger to populate Billing city and Billing State from Account onto Activity

Hello everyone,

Need help with this.

I need a trigger on Event object which populates two custom fields(event) with the Billing City and Billing Country from related Account object.

Any help on this is appreciated.

AJ
Hargobind_SinghHargobind_Singh
Hi AJ, 

I would strongly suggest using a process builder if you can. would save you some trouble of creating, deploying and managing the code. Here is a related post, which might help: 


https://success.salesforce.com/answers?id=90630000000CtkDAAS
Mathew Andresen 5Mathew Andresen 5
Something like this should work as well.  You will need to modify slightly with the API names of the fields you want to update
 
trigger Event_Trigger on Event (before insert) {
    
    set<id> acctSet = new set<id>();  // map of all of the account Id from the events
    map<string, event> eventMap = new Map<string, event>(); // so we can get the right event
    
    for (Event e:trigger.new) {
        acctSet.add(e.whatId);
        eventMap.put(e.whatId, e);
        
    }
    
    List<Account> acctList = [SELECT BillingCity, BillingStreet, BillingState, BillingCountry FROM Account WHERE Id IN:acctSet];
    
    for (Account acct:acctList) {
        Event e = eventMap.get(acct.id);
        e.Description = acct.billingCity + ' ' + acct.billingStreet + ' ' + acct.billingState;  // fill in whatever the fields you really want to update are
        
        
    }
    
 
}

 
Ajosh JohnAjosh John
Thank You Hargobind and Mathew. I used Mat's code and customized to my requirement needs. Evenrything works well now.