function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anonymous DeveloperAnonymous Developer 

Question about List View

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

Best Answer chosen by Anonymous Developer
SubratSubrat (Salesforce Developers) 
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.

All Answers

SubratSubrat (Salesforce Developers) 
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.
This was selected as the best answer
Anonymous DeveloperAnonymous Developer
Thanks Subrat