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
SFDC coderSFDC coder 

SOBJECT:Contact does not match domain of foreign key error

Hi all,

i am getting an error for the bind expression accidsLst in the below SOQL query
 
List<Contact> accidsLst=new List<Contact>();
         
         for(Contact con :newContactsLst)
         {
             contactsMap.put(con.Id,con);
         }
         
         accidsLst=[SELECT AccountId FROM Contact
                    WHERE Id IN:contactsMap.keySet()];
         
         for(Contact con :[SELECT Id,Primary_Contact__c FROM Contact
                           WHERE Id NOT IN:contactsMap.keySet() AND Primary_Contact__c=true
                           AND AccountId IN : accidsLst]) //error for accidsLst
         {
         }

Can anyone please help?

thanks
Best Answer chosen by SFDC coder
SFDC coderSFDC coder
Hi annop,
Thanks for your reply but that too doesn't work.

i solved it by using the below code:
 
for(Contact con :newContactsLst)
         {
             contactsMap.put(con.Id,con);
         }
         
         for(Contact cc:[SELECT AccountId FROM Contact
                        WHERE Id IN:contactsMap.keySet()])
                    {
                        accidsLst.add(cc.AccountId);
                    }
                    
         
         for(Contact con :[SELECT Id,Primary_Contact__c FROM Contact
                           WHERE Id NOT IN:contactsMap.keySet() AND Primary_Contact__c=true
                           AND AccountId IN : accidsLst])
         {
             Contact cnew=new Contact();
             cnew.Id=con.Id;
             cnew.Primary_Contact__c=false;
             updateContactLst.add(cnew);
         }
         
         if(updateContactLst.isEmpty()==false)
         {
             update updateContactLst;
         }
         
    }
}

 

All Answers

Anoop yadavAnoop yadav
Hi,
You should use the Account List instead of Contact List.

Try the below code.
List<Account> accidsLst=new List<Account>();
         
         for(Contact con :newContactsLst)
         {
             contactsMap.put(con.Id,con);
         }
         
         accidsLst=[SELECT AccountId FROM Contact
                    WHERE Id IN:contactsMap.keySet()];
         
         for(Contact con :[SELECT Id,Primary_Contact__c FROM Contact
                           WHERE Id NOT IN:contactsMap.keySet() AND Primary_Contact__c=true
                           AND AccountId IN : accidsLst]) //error for accidsLst
         {
         }

 
Anoop yadavAnoop yadav
Also
Change Line Number 8 and 9 Accordingly.
SFDC coderSFDC coder
Hi annop,
Thanks for your reply but that too doesn't work.

i solved it by using the below code:
 
for(Contact con :newContactsLst)
         {
             contactsMap.put(con.Id,con);
         }
         
         for(Contact cc:[SELECT AccountId FROM Contact
                        WHERE Id IN:contactsMap.keySet()])
                    {
                        accidsLst.add(cc.AccountId);
                    }
                    
         
         for(Contact con :[SELECT Id,Primary_Contact__c FROM Contact
                           WHERE Id NOT IN:contactsMap.keySet() AND Primary_Contact__c=true
                           AND AccountId IN : accidsLst])
         {
             Contact cnew=new Contact();
             cnew.Id=con.Id;
             cnew.Primary_Contact__c=false;
             updateContactLst.add(cnew);
         }
         
         if(updateContactLst.isEmpty()==false)
         {
             update updateContactLst;
         }
         
    }
}

 
This was selected as the best answer
Chidambar ReddyChidambar Reddy
Hi

You can do this by the following
 
Set<Id> accountIdSet = new Set<Id>();
         
         for(Contact con :newContactsLst)
         {
             contactsMap.put(con.Id,con);
             if(con.AccountId != NULL)
                     accountIdSet.add(con.AccountId);
         }
         
        
         
         for(Contact con :[SELECT Id,Primary_Contact__c FROM Contact
                           WHERE Id NOT IN:contactsMap.keySet() AND Primary_Contact__c=true
                          <i><b> AND AccountId IN : accountIdSet]) //error for accidsLst</b></i>
         {
         }

The error in your program is due to being accoutidslist as Accounts