You need to sign in to do that
Don't have an account?

Need help in Nested Loop Optimization . Hw can i implement this by map
for(Apttus_Billing__Invoice__c invoice:invoiceObjectList1) // loop over invoices { List<InvoiceLineItems> listToStoreLineItems = new List<InvoiceLineItems>(); APTS_InvoiceJournalWrapper invoiceJournWrapper= new APTS_InvoiceJournalWrapper(); countHeader=0; count=count+1; Decimal Sum=0; invoiceJournWrapper.id=String.valueof(invoice.id); invoiceJournWrapper.HeaderKey=String.valueof(count); invoiceJournWrapper.ZCurrency=(invoice.CurrencyIsoCode!=null?invoice.CurrencyIsoCode:'USD'); //invoiceJournWrapper.AccountingJournal='null'; invoiceJournWrapper.AccountingJournalID=String.valueOf(invoice.Name + '_' +myDate); invoiceJournWrapper.JournalNumber=String.valueOf(invoice.Name + '_' +myDate); invoiceJournWrapper.ExternalReferenceID=String.valueOf(invoice.Name); invoiceJournWrapper.AccountingDate=String.valueof(Date.valueof(invoice.Apttus_Billing__InvoiceDate__c)); //------------------- list<Apttus_Billing__InvoiceLineItem__c> invlineitems=[Select id,Apttus_Billing__AssetLineItemId__r.Apttus_Config2__ProductId__r.ProductCode,Apttus_Billing__AssetLineItemId__r.Apttus_Config2__OptionId__r.ProductCode,Apttus_Billing__InvoiceId__r.Name,Apttus_Billing__ProductId__r.Name,Apttus_Billing__InvoiceId__r.CurrencyIsoCode, Apttus_Billing__Type__c,Apttus_Billing__InvoiceId__c,Name,Apttus_Billing__Amount__c,Apttus_Billing__SoldToAccountId__r.EQ_ClientRef__c,Apttus_Billing__LocationId__c, Apttus_Billing__ProductId__r.EQ_FeeCode__c,Apttus_Billing__BillToAccountId__r.EQ_ClientRef__c,Apttus_Billing__ProductId__r.ProductCode,Apttus_Billing__InvoiceId__r.EQ_TotalInvoiceAmount__c from Apttus_Billing__InvoiceLineItem__c where Apttus_Billing__InvoiceId__c=:invoice.id and Apttus_Billing__Type__c='Contracted' and Apttus_Billing__Amount__c!=0]; //---- // //------------- InvoiceLineItems FirstInvItems= new InvoiceLineItems(); countHeader=countHeader+1; FirstInvItems.LineKey=string.valueof(countHeader);
Consider to split some "iterative tasks" in an helper (static) class, since your Rest service might grows in future, developers (you too ofc) may encounter difficulties to maintain that. For example, that part in which lineItems are collected and then modified can be delegated to the helper class to enhance code readability.
Further little optization can be done storing fixed numbers/strings in costants but I guess that there are not any improvements that significately reduce the execution time, I mean the optimization I will want to see here is in code readability first. Hope I helped you to solve your cancerns!
All Answers
I modifyed the query for readability, please check if it's correct before run the code.
First of all keep your filter in a String/Id list in order to minimize the records access. To keep track of your record use a second list of type Apttus_Billing_Invoice__c and wrap them in a map:
Then you will able to do a consistent filter in the WHERE clause:
So even if your loop scans 10000 records the number of query performed during the script execution is always one. This is the simplest "optimization" you can do, but actually your code does not respect salesforce best practices.
Tell me if you need further help
Paul
I have already tried this one. Please see my code:
Expected output should be:
But I am not getting the line items in correct order. It should be like : Invoice1 - All ites line items, Invoice2- All its line items .....
I suspect that the bahavior of the keyset() method (returning a Set<> and not a List, that means the elements belonging the container are not ordered, check this here : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm#apex_System_Map_keySet ) could change the order of your records.
What you can do is, before using keys in keyset(), to put your records from the keyset() to a List, that means you can obtain an ordered data structured since the List class do it for you.
The easiest way to fill the list is made using tha .addAll() method.
Let me know if this solve the issue.
Could you please review this code and let me know if more optimization can be done. This is working correctly.
Consider to split some "iterative tasks" in an helper (static) class, since your Rest service might grows in future, developers (you too ofc) may encounter difficulties to maintain that. For example, that part in which lineItems are collected and then modified can be delegated to the helper class to enhance code readability.
Further little optization can be done storing fixed numbers/strings in costants but I guess that there are not any improvements that significately reduce the execution time, I mean the optimization I will want to see here is in code readability first. Hope I helped you to solve your cancerns!