+ Start a Discussion
Micky MMicky M 

MAP Help

Hi all does anyone know how i can convert this select to a map:

 

for(Recurring_Service_Invoice__crsi : ClonedInvoices)

{

   for(Recurring_Service_Line_Item__ccli : [SelectUnit_Price__c, Unit_Cost__c, Start_Date__c Service__cRecurring_Service_Line_Item_ID__c, Recurring_Service_Invoice__c, Quantity__c, Notes__c, Next_Invoice_Due_Date__c, Name, Monthly_Charging_Metric__c, Invoice_Total__c, Invoice_Cost__c, Initial_Quantity__c, Id, End_Date__c, Customer_PO_Number__c, Category__c, Billing_Period__c, Asset__cFrom Recurring_Service_Line_Item__c where Recurring_Service_Invoice__c = :rsi.OldInvoiceId__c])

 

I need to replace this with a map so im not doing loads of soql calls but im stumped how to do it. 

 

Thanks

ShamilShamil

Hopefully I understood your question. My pseudo-code solution is:

 

Set<String> oldInvoicedIds = new Set<String>();
for(Recurring_Service_Invoice__c rsi : ClonedInvoices){
	oldInvoicedIds.add(rsi.OldInvoiceId__c);
}

List<Recurring_Service_Line_Item__c>  = [SelectUnit_Price__c, Unit_Cost__c, Start_Date__c Service__c, Recurring_Service_Line_Item_ID__c, Recurring_Service_Invoice__c, Quantity__c, Notes__c, Next_Invoice_Due_Date__c, Name, Monthly_Charging_Metric__c, Invoice_Total__c, Invoice_Cost__c, Initial_Quantity__c, Id, End_Date__c, Customer_PO_Number__c, Category__c, Billing_Period__c, Asset__cFrom Recurring_Service_Line_Item__c where Recurring_Service_Invoice__c in :oldInvoicedIds];

Map<String,Recurring_Service_Line_Item__c> oldInvoiceIdToRSLI = new Map<String,Recurring_Service_Line_Item__c>();
for(Recurring_Service_Line_Item__c rsli : ClonedInvoices){
	//Recurring_Service_Invoice__c MUST be unique!
	oldInvoiceIdToRSLI.put(rsli.Recurring_Service_Invoice__c,rsli);
}

//use the oldInvoiceIdToRSLI map to get Recurring_Service_Line_Item__c object via Recurring_Service_Invoice__c field's value

 

Key assumption here is that Recurring_Service_Invoice__c value is unique.