TransWikia.com

Invalid type: Schema.SObject

Salesforce Asked by Lalitha Rallabandi on December 14, 2021

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.

One Answer

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);
            }
        }

}

Answered by Noor A Shuvo on December 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP