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
degmodegmo 

Comparing list and produce a final list

Hello All,
I have one big list of accounts as follows:
List<Account> lstMain = [SELECT Id FROM Account WHERE balance__c > 1000000];

I have several other smaller lists as follows:
List<Account> lstVip = [SELECT Id FROM Account Where vip__c = true];

List<Account>lstSpecial = [SELECT Id FROM Account where special__c = true];

List<Account>lstNearMe = [SELECT Id FROM Account where state =:currentState AND status = 'valid'];

What I need to do is produce a final list.  The final list has to start with 'lstMain' and remove from 'lstMain' those that aren't in 'lstVip' or 'lstSpecial' or 'lstNearMe'.  What is the most efficient way to accomplish this?  Should I use a map instead of a list?
ANUTEJANUTEJ (Salesforce Developers) 
Hi Degmo,

As mentioned you can simply use a map<string,list<Account>> where the string is lstMain/lstVip/lstSpecial/lstNearMe and list values is the respective list of records that are obtained from the above soql queries.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
MichelCRMichelCR
Hi Degmo,

You should try to make that logic within the same query so you don't have to manipulate those lists.
 
List<Account> finalList = [SELECT Id FROM Account WHERE balance__c > 1000000
AND (vip__c = true
OR special__c = true
OR Status = 'valid')];

I hope this helps.