1

I have a contact Trigger and I'm trying to get the value of the Type picklist on Account for each Contact that the trigger is working on.

So far I've got this:

 Id cusAccRTs = [SELECT id from RecordType where Name = 'Customer' and SobjectType = 'Account' LIMIT 1].Id;

This gets me the RecordTypeId so I can use that for comparisons. But how can I get the value from the Account attached to the contact in a After trigger context?

I've tried:

c.Account.RecordTypeId == cusAccRTs && (c.Account.Type == 'Prospect' || c.Account.Type == 'Customer')

Setting system.debug on these values gets me null , so I guess I'll have to query for them into a Map first. Any pointers to help me with this?

1

You are on right track and the trigger.new variable in current SObject only has access to the fields in same object and does not access to related lists or lookup fields.

Hence you would need an additional query, and using a query + map would help you in bulkification.

This would need better exception handling, using describe calls would be better

// get the appropriate Record type
Id cusAccRTs = [SELECT id from RecordType where Name = 'Customer' and 
    SobjectType = 'Account' LIMIT 1].Id;

Use map for collecting the Account Ids and fill it with a query

// have a map
Map<Id, Account> mapAccount = new Map<Id, Account>();

// iterate over all contacts in trigger context
for(Contact c: Trigger.new) {

    // collect Accoung Ids in a map
    if (c.AccountId != null) {
        mapAccount.put(c.AccountId, null);
    }    
}

// fill in map with Accounts from contacts under trigger context
mapAccount = new Map<Id, Account>([SELECT Id, Type, RecordTypeId FROM Account 
    WHERE Id IN :mapAccount.keySet() ]);

iterate over Contact and perform your business logic

for(Contact c: Trigger.new) {

    if(c.AccountId != null) {
        // fetch the Account
        Account objAccount = mapAccount.get(c.AccountId);

        if (objAccount != null && 
            objAccount.RecordTypeId == cusAccRTs && 
            (objAccount.Type == 'Prospect' || objAccount.Type == 'Customer')) {
            // your code goes here
        }    
    }
}
  • Thanks Rahul, this works just fine and is understandable. I have learned something new today. – akarnid Oct 17 '17 at 10:58
1

Relational data is not available in the trigger context - only 1 level up, that is c.AccountId is as far as it goes. If you need anything further up the chain, you'll have to query it:

for (Contact c : [SELECT Id, Account.RecordTypeId, ANY-OTHER-FIELDS FROM Contact WHERE Id IN :trigger.new])
{
    if (c.Account.RecordTypeId == cusAccRTs)
    {
        // do your thing
    }
}
  • +1 Boris, sorry for the cross post. we almost posted on same time. Both of our answers solves the problem, I had just illustrated with maps. – Raul Oct 17 '17 at 11:01
  • Looks like we posted it at the same time. Great alternative! – Boris Bachovski Oct 17 '17 at 11:22

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.