4

I wrote one for loop and perform insert,update operation inside the for loop, it is working clearly but i need to perform that operation outside the for loop so is the better way to do it ?

for(Exhibition_Line_Item__c ELI : [Select Item__c, Line_Total__c,Invoice_Line__c, Line_Description__c,Exhibitor__c from Exhibition_Line_Item__c where Exhibitor__c in : exIdList]){

            invLine = new Invoice_Line__c();
            if(ELI.Line_Description__c != null){
                invLine.Line_Description__c = ELI.Item__c +' : '+ ELI.Line_Description__c;
            }else{
                invLine.Line_Description__c = ELI.Item__c;
            }
            invLine.Line_Total__c =  ELI.Line_Total__c;
            invLine.invoice__c = invoiceExhibitor.get(ELI.Exhibitor__c).Invoice__c;

            insert invLine;

            Invoice_Line__c inLI = new Invoice_Line__c();
            inLI = [select Id, Name from Invoice_Line__c where id =: invLine.Id];
            ELI.Invoice_Line__c = inLI.Id;

            update ELI;

            if(invIDs == null){
                invIDs = invoiceExhibitor.get(ELI.Exhibitor__c).Invoice__c;
            }else{
                invIDs += ',' + invoiceExhibitor.get(ELI.Exhibitor__c).Invoice__c;  
            }
}
3

Create one List called exLineList

Create one List called invLineList

exLineList = new List<Exhibition_Line_Item__c>();
invLineList= new List<Invoice_Line__c>();     

        for(Exhibition_Line_Item__c ELI : [Select Item__c, Line_Total__c,Invoice_Line__c, Line_Description__c,Exhibitor__c from Exhibition_Line_Item__c where Exhibitor__c in : exIdList]){

            invLine = new Invoice_Line__c();
            if(ELI.Line_Description__c != null){
                invLine.Line_Description__c = ELI.Item__c +' : '+ ELI.Line_Description__c;
            }else{
                invLine.Line_Description__c = ELI.Item__c;
            }
            invLine.Line_Total__c =  ELI.Line_Total__c;
            invLine.invoice__c = invoiceExhibitor.get(ELI.Exhibitor__c).Invoice__c;
            invLineList.add(invLine);
            exLineList.add(ELI);
            if(invIDs == null){
                invIDs = invoiceExhibitor.get(ELI.Exhibitor__c).Invoice__c;
            }else{
                invIDs += ',' + invoiceExhibitor.get(ELI.Exhibitor__c).Invoice__c;  
            }

        }

        insert invLineList;

        if(invLineList.size() > 0){
            for(integer i = 0 ; i < invLineList.size() ; i++ )
            {
                exLineList.get(i).Invoice_Line__c = invLineList.get(i).Id;
            }
            update exLineList;
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.