-2

I have a custom field in Contact Object named Inactive Contact.The custom field is a checkbox. I have a custom object site and Create a junction object called Site Contacts ( created a lookup field to site,contact) Added few records in Site Contact Condition: Whenever a contact is updated to inactive =true, then the associated junction object records should get deleted. For that, I have written code. It is throwing an error

Attached the code enter image description here

1
  • 1
    Please edit your post to include the code inline. Do not post screenshots.
    – David Reed
    Nov 25, 2019 at 14:15

2 Answers 2

3

I think you do not need the "after insert" event but just the "after update". This code below should do the job:

trigger DeleteContact on Contact (after update) {
    Set<Id> idContactWhereInactiveEqualsTrue = new Set<Id>();
    for(Id key : Trigger.newMap.keySet()){
        if(Trigger.newMap.get(key).In_Active_del__c == true){
            idContactWhereInactiveEqualsTrue.add(key);
        }
    }

    if(idContactWhereInactiveEqualsTrue.size() > 0){
        List<Contact_Site__c> contactSiteToDelete = [SELECT Id FROM Contact_Site__c WHERE contact__c IN :idContactWhereInactiveEqualsTrue];
        delete contactSiteToDelete;
    }

}

Update below:

trigger DeleteContact on Contact (after update) {
    Set<Id> idContactWhereInactiveEqualsTrue = new Set<Id>();
    for(Contact cont : Trigger.new){
        if(cont.In_Active_del__c == true){
            idContactWhereInactiveEqualsTrue.add(cont.Id);
        }
    }

    if(idContactWhereInactiveEqualsTrue.size() > 0){
        List<Contact_Site__c> contactSiteToDelete = [SELECT Id FROM Contact_Site__c WHERE contact__c IN :idContactWhereInactiveEqualsTrue];
        delete contactSiteToDelete;
    }

}

As @Novarg said, it is always better to put all the logic of the trigger in a handler class.

6
  • -1. Best practice is to keep the trigger without logic and perform everything in a handler. And on a side note: personally I would rewrite the loop to. for (Contact c : Trigger.new) and if (c.In_Active_del__c).
    – Novarg
    Nov 25, 2019 at 13:49
  • @Novarg Yeah it's true, it was just to offer him a code that works. I guess he already knows it.
    – dibocor
    Nov 25, 2019 at 13:58
  • Why do you want to rewrite the loop?
    – dibocor
    Nov 25, 2019 at 13:59
  • it's easier to read and maintain in the future. Also, the performance will be slightly better. I just did a quick test with similar code and for (Contact c : Trigger.new) performs better than looping over the ids and then getting a record out of the map. I only did 5 tests with each(insert of 1000 records, so trigger runs 5 times, no query or other DML), but best times were 3.173s vs 4.553s. Worst times were closer to each other: 5.051s vs 5.588s.
    – Novarg
    Nov 25, 2019 at 14:26
  • ah ok I see, Thanks @Novarg. I've just updated my answer
    – dibocor
    Nov 25, 2019 at 14:49
1

There is no build-in field called contactid in Contact object. Try Id instead.

Try

con.contact__c = c.id
3
  • Still it is not working. I got an error saying "Method does not exist or incorrect signature: void add(List<Contact_Site__c>) from the type Contact_Site__c" Nov 25, 2019 at 9:51
  • Can you paste your code here rather the screenshot? Nov 25, 2019 at 10:53
  • Definitely, I have some system issue right at that moment. I tried to copy the code from the mobile I wasn't working. So I have taken screenshot of it. In the future definitely I will paste the code. Thank you @noor Nov 26, 2019 at 5:40

You must log in to answer this question.

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