+ Start a Discussion
Alex MezaAlex Meza 

Incompatible key type Schema.SObjectField

I am trying to set up a connection where I can connect two custom objects that have the same custom field values such as first, last name, and/or zipcode, via a look up field that connects them by their salesforce generated record ID. If matching records based off of these custom feilds are found then the connection needs to autopopulate this lookup field that points back to each objecet, which in this case is just the standard Salesforce generated ID of each record in their respective custom object.

I was able to get started with some help and have come up with the apex trigger code below, that i wrote to match just based off of one custom field match in First Name, once I get the basic structure down then I will expand to include other custom fields too match. 

However, I am running into an error on line 13 where it says Incompatible key type Schema.SObjectField for Map<String,Voter_File__c>.  Also for the purpose of just figuring out this inital code and logic i've simplfied it to only look for matches base off first name.

With this my first custom objects= TGA Email List, and my second is= Voter File

Where the lookup field I am trying to update is Voter_ID_del__c on the TGA Email List custom object, where Voter_ID_del__c is the salesforce generated id record for a record in the Voter File custom object. 

Also note because realtionships between these two custom objects should be one to one, there is a lookup field on the Voter File object that reads Email_ID_del__c which is simply the Unique salesforce generated id record of the TGA Emai List Record. 

Am I on the right track so far also is there anything wrong I am doing. Thanks. 
 
trigger EmailtoVoterFile on TGA_Email_List__c (before insert,before update,after insert,after update)
{
	Set<String> set_Str = new Set<string>();
    Map<String,Voter_File__c> mp_voterfileObj;
    
    if(Trigger.isAfter && Trigger.isUpdate)
    {
        for(Voter_File__c voterfileobj : [Select ID,First_Name__c,Email_ID_del__c From Voter_File__c])
        {
            		if(mp_VoterfileObj==null)
                        mp_VoterFileObj = new map<String,Voter_File__c>();
        
            mp_VoterFileObj.put(Voter_File__c.First_Name__c,VoterFileObj);

 
                    }
        
        for(TGA_Email_List__c TGAEmailListobj : Trigger.new)
    {
        if(mp_VoterFileObj!=null && mp_Voter_File__c.containsKey(TGA_Email_List__c.First_Name__c))

        {

       mp_Voter_File__c.get(TGA_Email_List__c.First_Name__c).Voter_ID_del__c = Voter_File__c.id;

}

}
 
if(mp_VoterFileObj!=null && mp_Voter_File__c.values()!=null)
		update mp_Voter_File__c.values();

    }
}


 
Best Answer chosen by Alex Meza
DixitDixit
Also, if you are using a loop, use the name you gave to the Sobject, in your case: "voterfileobj" so:

"voterfileobj.First_Name__c" should help

All Answers

DixitDixit
It may be that is not expecting the "__c" on the first value you are putting. 

For relationship fields you should use "__r", so it should be: Voter_File__r.First_Name__c

 
DixitDixit
Also, if you are using a loop, use the name you gave to the Sobject, in your case: "voterfileobj" so:

"voterfileobj.First_Name__c" should help
This was selected as the best answer