+1 vote
51 views
by (163k points)

I am trying to populate Account Type on Contact by creating custom picklist 'Type' using Maps.

//Code

public class ContactAccountMap {
    public void accountMap() {
        Set<Id> accountIds = new Set<Id>();
        List<Contact> conList = [SELECT Id, Name, Type__c, AccountId FROM Contact];
        for(Contact con : conList) {
            accountIds.add(con.AccountId);
        }
        List<Account> accList = [SELECT Id, Name, Type FROM Account WHERE Id IN:accountIds];
        //System.debug('accounts'+accountIds);
        //System.debug('contacts'+conList);
        Map<Id,Account> accMap = new Map<Id,Account>([SELECT Id, Name, Type FROM Account WHERE Id in:accountIds]);
        for(Contact con : conList) {
            if(con.AccountId != NULL) {
                con.Type__c = accMap.get(con.AccountId).Type;
            }
        }
    }
}

The code doesn't throw any errors but the I am not getting the functionality. Can someone help me with this...

Thanks in advance...

1 Answer

Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
...