+ Start a Discussion
Rajasekhar TRajasekhar T 

How to pass two same lookup records to one set <Address__c> parameter in for loop?

I have two classes. in the first class account object having two lookup fields like "ship from" and "ship to". but these lookup object is one that is Address__c object. these lookup fields have different records.
totalrecords.totalamount(Address__C.Ship_To__c, Address__C.Ship_From__c, contact.type);
my requirement is how to pass these two object values to second class in this first set object parameter.
testcal(Set<Address__c>AddressSet, string type)
like below method
public static Decimal testcal(Set<Address__c>AddressSet, string type){ }
please check below my code
list<Account> someAddress = [SELECT ID, Ship_To__c, Ship_From__c FROM Account WHERE Id = :someID]; 

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

 for(account acc:someAddress ){ 

addressIds.add(acc.Ship_To__c); 
addressIds.add(acc.Ship_From__c); 

} 
Set<Addresss__c> addresses = new Set<Addresss__c>([SELECT Id, Other_Fields__c FROM Addresss__c WHERE ID IN :addressIds]);

 for(account acc1:newvalues()){

 TestCal(addresses, Contact.Type);

 }
i had written like this but it is not passing particular account to particlur address. i got list of address in set but account for loop how to pass particular address to particular account. 
 
Nagendra Prasad ChNagendra Prasad Ch
Hi Rajshekar,
You have to use Map for this.
Map<Id,Set<Address__c>> addressMap = new Map<Id,Set<Address__c>>();

 
list<Account> someAddress = [SELECT ID, Ship_To__c, Ship_From__c FROM Account WHERE Id = :someID]; 

Set<Id> addressIds = new Set<Id>();
Map<Id,Set<Address__c>> addressMap = new Map<Id,Set<Address__c>>();

 for(account acc:someAddress ){
//Clear the map for every iteration 
addressIds.clear();
addressIds.add(acc.Ship_To__c); 
addressIds.add(acc.Ship_From__c);
//Add addresses to map as Acc id as key
addressMap.put(acc.Id,addressIds); 

} 
Set<Addresss__c> addresses = new Set<Addresss__c>([SELECT Id, Other_Fields__c FROM Addresss__c WHERE ID IN :addressIds]);

 for(account acc1:newvalues()){
// Get the addresses from map by passing Account Id
 TestCal(addressMap.get(acc.Id), Contact.Type);

 }

I edited code in note pad, there might be minor typos.

Thanks,
Nagendra Prasad Ch.
Nagendra Prasad ChNagendra Prasad Ch
Rajashekar,
Here is the proper code. Above code has some typos.
 
list<Account> someAddress = [SELECT ID, Ship_To__c, Ship_From__c FROM Account WHERE Id = :someID]; 

Set<Id> addressIds = new Set<Id>();
//Define map
Map<Id,Set<Address__c>> addressMap = new Map<Id,Set<Address__c>>();

 for(account acc:someAddress ){
//Clear the map for every iteration 
addressIds.clear();
addressIds.add(acc.Ship_To__c); 
addressIds.add(acc.Ship_From__c);
//Add addresses to map as Acc id as key
addressMap.put(acc.Id,addressIds);
} 

Set<Addresss__c> addresses = new Set<Addresss__c>([SELECT Id, Other_Fields__c FROM Addresss__c WHERE ID IN :addressIds]);

 for(account acc1:newvalues()){
// Get the addresses from map by passing Account Id
 TestCal(addressMap.get(acc.Id), Contact.Type);
 }

Thanks,
Nagendra Prasad Ch​​​​​​​
Rajasekhar TRajasekhar T

Hi Ch Nagendra Prasad,
Thanks for reply. i got solution.

list<Account> someAddress = [SELECT ID, Ship_To__c, Ship_From__c FROM Account WHERE Id = :someID];
Set<Id> addressIds = new Set<Id>();
Map<Id, Id> mapAccountToShipToId = new Map<Id, Id>();
Map<Id, Id> mapAccountToShipFromId = new Map<Id, Id>();
for(account acc : someAddress ){ 
	mapAccountToShipToId.put(acc.Id, acc.Ship_To__c);
	mapAccountToShipFromId.put(acc.Id, acc.Ship_From__c);
}
addressIds.addAll(mapAccountToShipToId.values());
addressIds.addAll(mapAccountToShipFromId.values());

Map<Id, Addresss__c> mapAddressIdToAddress = new Map<Id, Addresss__c>();
for(Addresss__c objAddress : [SELECT Id, Other_Fields__c FROM Addresss__c WHERE ID IN :addressIds]){
	mapAddressIdToAddress.put(objAddress.id, objAddress);
}

Set<Addresss__c> accountAddresses = new Set<Addresss__c> ();
for(account acc1 : newvalues()){
	if(mapAccountToShipToId.containsKey(acc1.id) && mapAddressIdToAddress.containsKey(mapAccountToShipToId.get(acc1.id))){
		accountAddresses.add(mapAddressIdToAddress.get(mapAccountToShipToId.get(acc1.id)));
	}
	if(mapAccountToShipFromId.containsKey(acc1.id) && mapAddressIdToAddress.containsKey(mapAccountToShipFromId.get(acc1.id))){
		accountAddresses.add(mapAddressIdToAddress.get(mapAccountToShipFromId.get(acc1.id)));
	}
	TestCal(accountAddresses, Contact.Type); 
	accountAddresses.clear();
}