Skip to main content

Hello Good day everyone I have a question about the list 

 

I have a custom object application that has two lookup fields for accounts and has multiple related contacts on each account I want to filter all the records on the custom objects to only show the records to the contacts related to the accounts is this possible to implement if so can anyone help me?

 

Thanks in advance

2 réponses
  1. 12 mai 2023, 07:59
    Hello ,

    Yes, it is possible to implement this filtering on your custom object records. Here is an example approach you could use:

    Start by querying the related contacts for the accounts , You can use a SOQL query to retrieve the Contact records related to your Account records.

    Once you have the related contacts, you can create a Set or List to store the Contact Ids.

    Next, you can use a SOQL query on your custom object to filter the records based on the Contact Ids you retrieved .

    Please try with below code :

    // Query the related Contacts for the Account records

    List<Contact> relatedContacts = [SELECT Id FROM Contact WHERE AccountId IN (SELECT Account_c FROM Custom_Object_c)];

    // Create a Set to store the Contact Ids

    Set<Id> contactIds = new Set<Id>();

    for(Contact c : relatedContacts) {

        contactIds.add(c.Id);

    }

    // Use the Set of Contact Ids to filter the Custom_Object__c records

    List<Custom_Object_c> filteredRecords = [SELECT Id, Name, Field1c, Field2c FROM Custom_Objectc WHERE Contact_c IN :contactIds];

    If this helps , please mark this as Best Answer !

    Thank you.