I'm pretty new to apex and trying to create a trigger that will update a custom object field when a lead is converted. I think I'm close with the trigger, but I'm getting an error that my list "lstHandoff" doesn't exist. Any ideas?
trigger MoveHandoffOnLeadConvert on Lead (after update) {
if (Trigger.isAfter) {
if (Trigger.isUpdate) {
Set<ID> leadToContact = new Set<ID>();
for (Lead ld : Trigger.new) {
if(ld.isConverted && trigger.oldMap.get(ld.id).isConverted) continue; //only process newly converted leads
if (ld.ConvertedContactId != null){
leadToContact.put(ld.ConvertedContactID);
}
}
if (!leadToContact.isEmpty()) {
List<Handoff__c> lstHandoff = new List<Handoff__c>();
for(Handoff__c handoffs:[select Id,Contact__c from Handoff__c where Lead__c in : leadToContact.keySet()]){
if(leadToContact.containsKey(handoffs.id)){
handoffs.Contact__c = leadToContact.get(handoffs.id);
lstHandoff.add(handoffs);
}
}
}
if (!lstHandoff.isEmpty()) {
Update lstHandoff;
}
}
}
}
isAfter
andisUpdate
are irrelevant; the test for empty before the update lstHandoff is not required, if the list is empty, the DML statement becomes a NOP and doesn't count against DML calls