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
vinod fnuvinod fnu 

how to get fields from a list and compare it to the list other custom object

i have a scenario .I have a custom object party  having fields 'related contact' and 'related  companies'.
These fields are lookup to Contact Object and companies object.
each party record will have a related contact and related companies value.
I have a custom object named  invoices .
For all companies we will have 1 invoices record.
on invoices object  i want the list of all contacts in fields user 1 ,user 2 ,user 3
example
Record PARTY -1            james  jo                  acme ltd
            Party -2               marc                          acme ltd
          party 3                   lily                             pvd ltd
      party  4                      james jo                      amx ltd
     Party 5                      silly                            amx ltd

for record  invoice with name acme ltd.
user 1=james jo (auto poulated by apex trigger or formulae)
user 2 = marc (auto poulated by apex trigger or formulae)

for record  invoice with name amx ltd.
user 1=james jo (auto poulated by apex trigger or formulae)
user 2 = silly (auto poulated by apex trigger or formulae)
 
Natarajan _Periyasamy__cNatarajan _Periyasamy__c
Hey Vinod,

With the help of Map & Set, it's easily achievable.

Map<Id, List<Id>> accountIdToPartyContactIds = new Map<Id, List<Id>>(); //Populate the values from the party object to the map defined and Make sure you are removing the duplicates (List may contain duplicates)
//Id (Key) = Account Id; List<Id> (Values) = Contains all the Contact Id's from the party that are related to the specific account;

for (Invoice this|nv : invoiceList) {
      if(accountIdToPartyContactIds.get(thisInv.Account__c) != null) {
            thisInv.User1__c = thisInv.User1__c == null && accountIdToPartyContactIds.get(thisInv.Account__c).size() > 0 ?  accountIdToPartyContactIds.get(thisInv.Account__c)[0] : thisInv.User1__c;
            accountIdToPartyContactIds.get(thisInv.Account__c).remove(thisInv.User1__c); //Remove the Mapped Contact Id from set

           thisInv.User2__c = thisInv.User2__c == null && accountIdToPartyContactIds.get(thisInv.Account__c).size() > 0 ?  accountIdToPartyContactIds.get(thisInv.Account__c)[0] : thisInv.User2__c;
            accountIdToPartyContactIds.get(thisInv.Account__c).remove(thisInv.User2__c); //Remove the Mapped Contact Id from set
      }
}

//Above is just a sample code, but the logic should work
vinod fnuvinod fnu
one account will be in the party records list multiple times.so if we remove the account duplicate .we will miss the contact assigned to those account on the .For example 

Party1    jole        actimes
party2  merr       ptmt
party 3   jole      ltmt
part 4    aunt     ltmt


if i prepare a list and remo0ve duplicates.I will miss out jole as a seprate for ltmt will not be in the added .however we will have ltmt in the list since it will come for aunt.
 
Natarajan _Periyasamy__cNatarajan _Periyasamy__c
That's not correct. It's one list againt each key in the map. So you need to remove the duplicates in each list. 

I hope this helps!!
Natarajan _Periyasamy__cNatarajan _Periyasamy__c
Also, you don't have to remove the duplicates, instead while adding the values (Id) to the list you can check whether you have the value (Id) already present in the list or not. If not present, you can add it. I hope this is clear to you.