I am trying to populate a Contact lookup field (Sourced_By__c) on the Opportunity with the Converted Contact upon Lead Conversion but am not having any luck. I do not get an error but it also does not populate the field. Here is my trigger below, can anyone help?
Trigger Lead on Lead (before update) {
if (Trigger.isBefore) {
if (Trigger.isUpdate) {
Set<Id> setLeadIds = new Set<Id>();
Set<Id> setConvertedOppIds = new Set<Id>();
Set<Id> setConvertedContIds = new Set<Id>();
for (Lead ld : Trigger.new) {
// Find all converted Leads with Opportunitiy and add ConvertedOpportunityId to setConvertedOppIds
if (ld.isConverted && ld.ConvertedOpportunityId != null
&& Trigger.oldMap.get(ld.Id).ConvertedOpportunityId != ld.ConvertedOpportunityId)
setConvertedOppIds.add(ld.ConvertedOpportunityId);
// Find all converted Leads with Contacts and add ConvertedContactId to setConvertedContIds
if (ld.isConverted && ld.ConvertedContactId != null
&& Trigger.oldMap.get(ld.Id).ConvertedContactId != ld.ConvertedContactId)
setConvertedContIds.add(ld.ConvertedContactId);
}
if (!setConvertedOppIds.isEmpty()) {
List<Opportunity> lstOpp = new List<Opportunity>();
for(Opportunity opps:[select Id,AccountId,CampaignId from Opportunity where Id in :setConvertedOppIds])
List<Contact> lstCont = new List<Contact>();
for(Contact Conts:[select Id,AccountId from Contact where Id in :setConvertedContIds])
if (lstOpp.size() > 0) {
for (Opportunity opp : lstOpp) {
Opportunity otm = new Opportunity(
//OpportunityId = opps.Id,
Sourced_By__c = Conts.Id);
lstOpp.add(otm);
}
if (!lstOpp.isEmpty()) {Update lstOpp;
}
}
}
}
}
}