-2

My requirement is, whenever the checkbox is checked on Contact Object then records in the related junction object(Custom Object) should gets deleted automatically. For that here is the code:

public class DeleteContactsFromRSites {

    @InvocableMethod
    public static void SiteContactDel(List<Id> ContactIds) {

        System.debug('list of contacts ' + ContactIds);

        List<Site_Contact__c> resultList = [SELECT Id, Contact__r.name, 
                                                    , Contact__r.id
                                                    , Contact_r.In_Active__c 
                                             FROM Site_Contact_c 
                                             Where Contact__r.id IN :ContactIds 
                                                And Contact_r.In_Active__c = true];
        System.debug('the list of record to delete' + resultList);

        try{
            if (!resultList.isEmpty()) {
                delete resultList;

                for(Integer i=0;i<resultList.size();i++)
                    System.debug('deleted list of contacts ' + resultList[i].id + '  name is ' + resultList[i].Contact__r.Name);
            }
        }
        catch(Exception e){
            System.debug('Deletion of records failed:' + e);
        }
    }
}

Please Let me know where exactly the error is.

6
  • Please include code in your post using the {} option so that it is formatted and readable.
    – Jigar Shah
    Nov 26, 2019 at 5:24
  • 1
    Can you please share the exact error message that you are encountering. Moreover, this can be easily achieved via an after update Apex trigger. Is there a specific reason why you are using a Process builder to initiate the record deletion?
    – Jigar Shah
    Nov 26, 2019 at 5:33
  • I already achieved it with the trigger. I am looking for the other possibilities of doing Nov 26, 2019 at 5:48
  • 1
    Please share the complete error message that you encounter.
    – Jigar Shah
    Nov 26, 2019 at 5:50
  • Error Message: Contact__r.name, Contact__r.id, Contact_r.In_Active_del__c FROM ^ ERROR at Row:1:Column:44 Didn't understand relationship 'Contact_r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. @jigar Nov 26, 2019 at 5:59

1 Answer 1

0

Contact_r should be Contact__r, that is, instead of single underscore (_), you should use double underscore (__).

Therefore, the code will be

public class DeleteContactsFromRSites {
    @InvocableMethod
    public static void SiteContactDel(List < Id > ContactIds) {
        system.debug('list of contacts ' + ContactIds);

        List < Site_Contact__c > resultList = [SELECT id, Contact__r.name, Contact__r.id, Contact__r.In_Active__c FROM Site_Contact_c 
                                               where Contact__r.id IN: ContactIds and Contact__r.In_Active__c = true];
        system.debug('the list of record to delete' + resultList);

        try{
            if (!resultList.isEmpty()) {
            delete resultList;
            for(Integer i=0;i<resultList.size();i++)
            System.debug('deleted list of contacts ' + resultList[i].id + '  name is ' + resultList[i].Contact__r.Name);
        }
        }
        catch(Exception e){
        System.debug('Deletion of records failed:' + e);
            }
        }

}

You must log in to answer this question.

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