0

I wrote a really simple trigger on Contact After Insert which isn't working as expected. The expected behaviour would be to delete contacts created from Leads converted that had the field Account_to_be_converted_in__c populated.

public class TriggerContactDeleteFarmingContacts implements Triggers.Handler {
    public void handle () {
        List<id> ContactsToDelete = new List<id> ();
        Map<id,Contact> Mapcontacts= new Map<id,Contact> ();
        for(Contact C: (List <Contact>) Trigger.new) {
            Mapcontacts.put(C.id,C);
        }
        System.debug('Mapcontacts: ' + MapContacts);
        for(Lead L: [SELECT Account_to_be_converted_in__c,ConvertedContactid FROM Lead WHERE isConverted = True AND ConvertedContactid in: MapContacts.keySet()]) {
            if(MapContacts.keyset().contains(L.ConvertedContactid) && !String.isBlank(L.Account_to_be_converted_in__c)) {
                ContactsToDelete.add(L.ConvertedContactId);
            }   
        }
        Database.delete(ContactsToDelete);
    }
}

For some reason I still don't see the query isn't returning any row, but if I do it on query editor it returns the expected results.

EDIT: (Using on Lead After update Trigger)

public class TriggerLeadDeleteContactonConvert implements Triggers.Handler {
    public void handle () {
        Map <id,Lead> MapOldLeads = (Map<id,Lead>) Trigger.oldMap;
        List <id> contactsToDelete = new List <id> ();
        for(Lead L:(List <Lead>) Trigger.new) {
            if(L.isConverted != MapOldLeads.get(L.id).isConverted && !String.isBlank(L.Account_to_be_converted_in__c)) {
                contactsToDelete.add(L.ConvertedContactId);
            }
        }
        Database.delete(contactsToDelete);
    }
}

If I try the code above I'm getting this error :

ENTITY_IS_DELETED

So the conversion isn't possible to be achieved.

EDIT 2: (Using Trigger on Lead After Update without using Id but List, still getting same error)

public void handle () {
    Map <id,Lead> MapOldLeads = (Map<id,Lead>) Trigger.oldMap;
    List <Contact> contactsToDelete = new List <Contact> ();
    for(Lead L:(List <Lead>) Trigger.new) {            
        if(L.isConverted != MapOldLeads.get(L.id).isConverted && !String.isBlank(L.Account_to_be_converted_in__c)) {
            contactsToDelete.add(new Contact(id=L.ConvertedContactId));
        }
    }
    delete contactsToDelete;
}
9
  • Is Really necessary the isConverted = true clause? I didn't had it there originally , but put it since I didn't knew what else to try. I know probably the mistake i'm making it's very simple but I just can't see where i'm messing things up on that query. Commented May 5, 2016 at 10:00
  • try to remove istrue clause. I don't think it will affect but can solve your problem. Commented May 5, 2016 at 10:02
  • The original query didn't had it there and still returned 0 rows. There isn't the problem but I will deleted since it didn't help to solve it neither and seems redundant. Commented May 5, 2016 at 10:03
  • 1
    also check when contact created till that time your lead is updated or not. I think it not updated. Commented May 5, 2016 at 10:04
  • 1
    So then I should switch to after update on Lead. When isConverted changes from false to true and do the same behaviour but querying for the contact related. But anyway to me at least sounds counterintuitive that you create a Contact before locking the lead. I dunno if it's done this way due to some internal implications but at least to me as I said doesn't sound logical at all. If someone could explain why does this work that way would be interesting. I'll try that changes and provide feedback about how it went. Thanks. Commented May 5, 2016 at 10:08

1 Answer 1

1

This is due to when this contact is converted you lead is not converted thats why you are not getting any records. Due to this you need to use Lead afterupdate trigger.

Lead conversion order of execution

Account Before                 (Fires based on lead settings)
Account After
Contact Before                 (Fires based on lead settings)
Contact After
Opportunity Before             (Fires based on lead settings)
Opportunity After              (OCR's not available)
Lead Before
Lead After                     (OCR's available)

Reference 1

7
  • It's still not working even moving the trigger to Lead after update. Commented May 5, 2016 at 10:30
  • first check you are getting value in L.ConvertedContactId. Also you can't delete like this create a contact instance first and store them in list and then perform delete operation Commented May 5, 2016 at 10:33
  • The value of ConvertedContactId isn't null and even if I change the code to perform the delete with Contact Objects with that id's i'm still getting the same error. Commented May 5, 2016 at 10:38
  • @AlexanderAeonsTorn can you please share your updated code. Commented May 5, 2016 at 12:00
  • 1
    @Alex @future would be easier to maintain and should work just fine...
    – Adrian Larson
    Commented May 5, 2016 at 15:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .