+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

+1 vote
by (163k points)
 
Best answer

You populated the Contact field. But not updating it to salesforce. Add the below line of code after the for loop and check.

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