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
GardenwayGardenway 

Incompatible key type scheme.SObjectField for Map<Id,Account>

Hi,

 

I am trying to update my contact phone from the Account phone.  However, in trying to bulkify the code, I keep running into problems with the mapping code.  I get an error:  Incompatible key type scheme.SObjectField for Map<Id,Account> for the line which attempts to pull the account out of the map based on the account id key that is stored in contact object.

 

Any help is greatly appreciated.

 

trigger SetPhone on Contact (before insert, before update) {
    Set<Id> accountIds = new Set<Id>();
    
    for (Contact c: Trigger.new){
        accountIds.add(c.Account.Id);
    }
    
    Map<Id, Account> accountPhoneMap =
        new Map<Id,Account>(
            [Select Id, Phone FROM Account WHERE Id IN: accountIds]);
        
    for (Contact c: Trigger.new){
        if(c.Phone == null) {
            Account a = accountPhoneMap.get(contact.Account.Id);
//Error prior line: Incompatible key type scheme.SObjectField for Map<Id,Account>
            c.Phone = a.Phone;
        }
    }
}

 Thank you,

 

John

Best Answer chosen by Admin (Salesforce Developers) 
GardenwayGardenway

The following resolved the issue.  I am still not sure why the Force.com eclipse IDE was throwing an error message.  This is now compling.

 

thank you!

 

trigger SetPhone on Contact (before insert, before update) {
    Set<Id> accountIds = new Set<Id>();
    for (Contact c: Trigger.new){
        accountIds.add(c.AccountId);
    }
    Map<Id, Account> accountPhoneMap =
        new Map<Id,Account>(
            [Select Id, Phone FROM Account WHERE Id IN: accountIds]);
    for (Contact c: Trigger.new){
        if(c.Phone == null && accountPhoneMap.get(c.AccountId) != null) {
            c.Phone = accountPhoneMap.get(c.AccountId).Phone;
        }
    }
}

 

All Answers

GardenwayGardenway

The following resolved the issue.  I am still not sure why the Force.com eclipse IDE was throwing an error message.  This is now compling.

 

thank you!

 

trigger SetPhone on Contact (before insert, before update) {
    Set<Id> accountIds = new Set<Id>();
    for (Contact c: Trigger.new){
        accountIds.add(c.AccountId);
    }
    Map<Id, Account> accountPhoneMap =
        new Map<Id,Account>(
            [Select Id, Phone FROM Account WHERE Id IN: accountIds]);
    for (Contact c: Trigger.new){
        if(c.Phone == null && accountPhoneMap.get(c.AccountId) != null) {
            c.Phone = accountPhoneMap.get(c.AccountId).Phone;
        }
    }
}

 

This was selected as the best answer
sfdcfoxsfdcfox
You were using Contact.Account.Id, which resolves to a field, not an ID. Specifically, it resolves to Schema.Account.Id, which is a SObjectField token; when you renamed Contact to c, you were no longer referencing Schema implicitly, so you were allowed to save.
GardenwayGardenway
Thank you! That makes more sense.