0

I need to write a trigger so that custom data fields (City__c, Country__c ...) can be called to account billing address by selecting a lookup value in account object.

Trigger fetchaddress on Account (before insert , before update) {
    set<id> acid = new set<id>();
    for(Account ac: Trigger.new){
        if (ac.id != null) 
            acid.add(ac.id);
    }
    Map<id,Address__c> fetch = new Map<id,Address__c>([select id,name,City__c,Country__c,Postal_Code__c,State__c,Billing_Street__c from Address__c where id in : acid]);

    for(Account a : Trigger.new){
        Address__c ad = fetch.get(a.id);

        if(A.id !=null)

        a.Billingstreet = ad.Billing_Street__c;
        a.Billingcity = ad.City__c;
        a.Billingstate = ad.State__c;
        a.BillingPostalcode = ad.Postal_Code__c;
        a.Billingcountry = ad.Country__c;
    }
}

Here is what I tried but failed to get the desired result.

  • 1
    This trigger is on the Account object. Is it meant to fire upon insert or update of the Address__c object? You won't be able to query for child objects in a before insert trigger on the parent. – David Reed Feb 22 '18 at 18:59
2

So, I’m assuming that the relationship between the Account and Address__c is a lookup from the Account to the address and so if an address is selected upon insert or edit this address should be stamped onto the account?

Under the above assumption you would need to query the Address__c records based on their id rather than the id of the account itself, in the loop where you check for the ac.id != null and add the id to a set you should be checking the id in the lookup to the address record, something like:

if((Trigger.isInsert && ac.address__c != null) ||
    (Trigger.isUpdate && ac.address__c != null &&
     ac.address__c != (Id)Trigger.oldMap.get(ac.id).get('address__c'))
{
    acid.add(ac.address__c);
}

The above logic would then allow you to successfully query the required address records to then stamp the BillingAddress fields with.

Note: i have added logic to the update section of the trigger so that it would only pick up addresses that have actually changed upon update, this may not be the overall desired outcome so different logic to address this may be needed if this is not the case

Another suggestion in this trigger would be to only pull records from the map if they meet the above criteria and they can be found in the map, this would help to stop null pointer issues in the event that an address could not be found.

for(Account a : Trigger.new)
{
    if(a.address__c != null && fetch.containsKey(a.address__c))
    {
        Address__c ad = fetch.get(a.address__c);

        a.Billingstreet = ad.Billing_Street__c;
        a.Billingcity = ad.City__c;
        a.Billingstate = ad.State__c;
        a.BillingPostalcode = ad.Postal_Code__c;
        a.Billingcountry = ad.Country__c;
    }
}

I hope this helps!

  • Thank You Art of cloud Your assumption was 100% correct, Address__c object contains a number of address and to choose any one of them , a lookup is provided in the account object. After ammending the changes suggested by you, I am getting the following error on creating or updating new account : Error : system.NullPointerException: Attempt to de-reference a null object – Nicola thompson Feb 23 '18 at 9:57
  • @Nicolathompson no problem, i just spotted the error in my code, a little hasty posting the 2nd section, i'll amend it now, i am guessing its the error when fetch.get(a.id); is called, this should be fetch.get(a.address__c); – Art of Cloud Feb 23 '18 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.