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
Molson94Molson94 

Comparing Two Lists to Create a Third List

Morning Everyone,

Thank you in advance for the insights to this problem.
I posted a related question here: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AxSXIA0
Where i thought i would be able to get the list of IDs through 1 query. After going back and forth all week i have come to the realization that i cannot query at once since the relationships do not align.

But I do think i can query 2 lists through Product__c and do some comparing to create the third list for emailing. Problem is I cannot find the right code to do it.

List 1: List of Promotion Id and related Product Id (based on criteria of the promotion)
List 2: List of Product Id and related Product Contact Id (based on criteria of the Product Contact Role ' Manager' or 'Rep')

I would like to do a comparison between List 1 & 2.
If the Product ID in L2 exists in L1, add the Promotion ID and Product Contact ID together in L3.

So List 3 would be: Promotion ID | Product Contact ID

Thoughts on the best practice to tackle this?

Thanks!

 
Best Answer chosen by Molson94
Swayam  AroraSwayam Arora
Hi,

Below is the one way of doing this:-

List<Promotion__c> L1 = [Select Id, (Select Id From Product__c) from Promotion__c where Status__c = 'Approved'];
List<Product_Contacts__c> L2 = [Select Id, Product__c (Select Id, Email From Contact) from Product_Contacts__c where Role = 'Manager' Or Role = 'Rep'];

for(Promotion__c p1 : L1) {
    for(Product__c p2 : p1.Product__c) {
        for(Product_Contacts__c p3 : L2) {
            if(p3.Product__c == p2.Id) {
                for(Contact c : p3.Contact) {
                    send email to these contacts
                }
            }
        }
    }
}

I will try to think for better solution using Maps (if exists).

Please close the thread marking this answer as Best Answer if it really helped. Closing the thread help others finding the correct answer.

 

All Answers

Swayam  AroraSwayam Arora
Hi,

Below is the one way of doing this:-

List<Promotion__c> L1 = [Select Id, (Select Id From Product__c) from Promotion__c where Status__c = 'Approved'];
List<Product_Contacts__c> L2 = [Select Id, Product__c (Select Id, Email From Contact) from Product_Contacts__c where Role = 'Manager' Or Role = 'Rep'];

for(Promotion__c p1 : L1) {
    for(Product__c p2 : p1.Product__c) {
        for(Product_Contacts__c p3 : L2) {
            if(p3.Product__c == p2.Id) {
                for(Contact c : p3.Contact) {
                    send email to these contacts
                }
            }
        }
    }
}

I will try to think for better solution using Maps (if exists).

Please close the thread marking this answer as Best Answer if it really helped. Closing the thread help others finding the correct answer.

 
This was selected as the best answer
Molson94Molson94
Thank you for your reply Swayam!
This helped and got me pointed in the right direction.

I did however come across a new error that i posted if you are able to advise:

https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Apex_Code_Development&criteria=OPENQUESTIONS&id=906F0000000AzXFIA0

Thanks again!