I'm having an issue with a trigger, and can't seem to figure it out, so I'm hoping someone can help me.
Basically, I wrote a trigger that runs when a lead is converted. It is supposed to populate a field called Contact__c on the new opportunity, with the ID of the Primary Contact of that Opp.
For some reason, it doesn't seem to be putting anything in that field. I added a bunch of debug statements, and it looks like it's having trouble pulling a value from the map
Here's the trigger:
trigger AddPrimaryContactToConvertedOpp on Lead (After Update) {
// THIS TRIGGER WILL OVERWRITE ANY CONTACT DEFINED IN THE CUSTOM FIELD CONTACT__C ON THE OPPORTUNITY OBJECT.
// SET THIS FIELD TO READ ONLY OR CHANGE THE FUNCTIONALITY BELOW TO AVIOD DATA BEING OVERWRITTEN BY MISTAKE...
//STEP 1: Create a list of all Opp IDs we are working on, ie: those created in this lead conversion (or batch conversion)
map <Opportunity,Id> OppsAndContacts = new map <Opportunity,Id>();
for (Lead l : Trigger.new) {
If (l.IsConverted){
Opportunity Opp = [SELECT Id from Opportunity where Id = :l.convertedopportunityid];
OppsAndContacts.put(Opp, l.ConvertedContactID);
System.Debug('>>> adding to map contact: ' + l.convertedcontactid + ' opp: '+ Opp);
}
}
System.Debug('>>> Printing the map: '+ OppsAndContacts);
//STEP 2: Loop through all records in OppsAndContacts, adding the contactID to the Opps
if (!OppsAndContacts.isEmpty()){ //only run if there are actually some newly created Opps
System.Debug('>>> the list of OppsAndContacts is not empty');
for (Opportunity o : OppsAndContacts.keySet()) {
o.Contact__c = OppsAndContacts.get(o);
System.Debug('>>> adding to opp record: ' + o.Id + ' the contact with ID: ' + OppsAndContacts.get(o));
}
}//END IF
}//END TRIGGER
It seems to be having trouble with OppsAndContacts.get(o)
If anyone can help me out I'd greatly appreciate it!