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
Nihar SharmaNihar Sharma 

How to merge same account with multiple contact in apex class?

I am inserting Invoice based on my attendees object and inserting Invoice Line item based on that Invoice that is already created.

so, My question is if the user will select multiple attendees, many of those might be at the same account, so we’ll make only one invoice for each account, but there will be an Invoice Line Item for each attendee.

This account is selected as per the contact of attendees record.

Please suggest me how can i do this.

Here is my code :
Public PageReference save(){

        invList = new List<Invoice__c>();
        invLineList = new List<Invoice_Line__c>();
        List<Attendee__c> attendeeUpdate = new List<Attendee__c>();

        Map<String,Attendee__c> invoiceAttendee = new Map<String,Attendee__c>();

        for(Attendee__c a : atList){

            Invoice__c inv = new Invoice__c();

            inv.Billed_Through__c = a.Function__r.Billed_Through__c;
            inv.Invoice_Memo__c = a.Function__r.Name;
            inv.Customer__c = a.Contact__c;
            inv.Invoice_Date__c = date.today();
            inv.AtndID__c = a.Id;
            invList.add(inv);
            invoiceAttendee.put(a.Id,a);
        }

        insert invList;

        for(Invoice__c inv : invList){
            if(invoiceAttendee.get(inv.AtndID__c) != null){
                invoiceAttendee.get(inv.AtndID__c).Invoice__c = inv.Id;
            }
        }

        update invoiceAttendee.values();

        for(Invoice__c i : invList){

            invLine = new Invoice_Line__c();
            invLine.Line_Description__c = ' Attendance of ' + invoiceAttendee.get(i.AtndID__c).Contact__r.Name  + ' at '+  invoiceAttendee.get(i.AtndID__c).Function__r.Name;
            invLine.Line_Total__c =  invoiceAttendee.get(i.AtndID__c).Attendee_Charge__c;
            invLine.invoice__c = i.Id;
            invLineList.add(invLine);
            if(invIDs == null){
                invIDs = i.id;
            }else{
                invIDs += ',' + i.id;  
            }

        }

        insert invLineList;
Best Answer chosen by Nihar Sharma
Nihar SharmaNihar Sharma
Hi All,

I solved this using the following code :

A MAP to capture whether an invoice has already been created for that account.
Map<Id, Invoice__c> accountToInvoice = new Map<Id, Invoice__c>();
        
        for(Attendee__c a : atList){
           Invoice__c inv = accountToInvoice.get(a.Contact__r.AccountId);
            if(inv != null)
                continue;
                        
            inv = new Invoice__c();
            
            inv.Billed_Through__c = a.Function__r.Billed_Through__c;
            inv.Invoice_Memo__c = a.Function__r.Name;
            inv.Customer__c = a.Contact__c;
            inv.Invoice_Date__c = date.today();
            inv.AtndID__c = a.Id;
            invList.add(inv);
            invoiceAttendee.put(a.Id,a);
            accountToInvoice.put(a.Contact__r.AccountId, inv);

            
        }
        insert invList;
For invoice line, just loop through attendee list instead of the invoice list.
for(Attendee__c a : atList) {
            Invoice__c inv = accountToInvoice.get(a.Contact__r.AccountId);
            invLine = new Invoice_Line__c();
            invLine.Line_Description__c = ' Attendance of ' + a.Contact__r.Name  + ' at '+  a.Function__r.Name;
            invLine.Line_Total__c =  a.Attendee_Charge__c;
            invLine.invoice__c = inv.Id;
            invLineList.add(invLine);
            if(invIDs == null){
                invIDs = inv.id;
            }else{
                invIDs += ',' + inv.id;  
            }
        }  
        insert invLineList;

Regards,
Nihar

All Answers

Abhishek BansalAbhishek Bansal
Hi Nihar,

I have one query related to your requirement.
  1. In line no.15 you are adding Contact__c from attendee in your invoice as customer So my question is If you want to create a Sigle Invoice for a Single Account than what should be the value of customer__c in that Invoice as there can be multiple contacts associated with an Account. For Eg : Attendee 1 = Contact 1 = Account A,  Attendee 2 = Contact 2 = Account 1.  In this case according to you only a single Invoice should be created than what should be the value of Customer__c in that Invoice  Contact 1 or Contact 2 ?
Please provide me the information on the above query.

Regards,
Abhishek.
Nihar SharmaNihar Sharma
Hi Abhishek,

Thanks for the reply but when user select multiple attendees from the layout (using checkbox) it will take first which is in priority.
In other word First come First Serve.

Actually i solved this issue Using MAP.

So, Thank you so much.

Regards,
Nihar
Abhishek BansalAbhishek Bansal
Hi Nihar,

Thanks for the information and please close this question in order to avoid confusion and save time of others.
In future if you find a solution for any question than please mark it as SOLVED so that others will not spend their time in finding a solution for you.

Regards,
Abhishek.
Nihar SharmaNihar Sharma
Can i post my solution of my own question ?

so it can be helpful for others.
Abhishek BansalAbhishek Bansal
Yes Nihar, If no one has replied to your question and in the mean time you yourself find a sloution than imeediately either remove your question or post the solution so that it would also be helpful to others.
You can also select your own Answer as Best Answer.
Nihar SharmaNihar Sharma
Hi All,

I solved this using the following code :

A MAP to capture whether an invoice has already been created for that account.
Map<Id, Invoice__c> accountToInvoice = new Map<Id, Invoice__c>();
        
        for(Attendee__c a : atList){
           Invoice__c inv = accountToInvoice.get(a.Contact__r.AccountId);
            if(inv != null)
                continue;
                        
            inv = new Invoice__c();
            
            inv.Billed_Through__c = a.Function__r.Billed_Through__c;
            inv.Invoice_Memo__c = a.Function__r.Name;
            inv.Customer__c = a.Contact__c;
            inv.Invoice_Date__c = date.today();
            inv.AtndID__c = a.Id;
            invList.add(inv);
            invoiceAttendee.put(a.Id,a);
            accountToInvoice.put(a.Contact__r.AccountId, inv);

            
        }
        insert invList;
For invoice line, just loop through attendee list instead of the invoice list.
for(Attendee__c a : atList) {
            Invoice__c inv = accountToInvoice.get(a.Contact__r.AccountId);
            invLine = new Invoice_Line__c();
            invLine.Line_Description__c = ' Attendance of ' + a.Contact__r.Name  + ' at '+  a.Function__r.Name;
            invLine.Line_Total__c =  a.Attendee_Charge__c;
            invLine.invoice__c = inv.Id;
            invLineList.add(invLine);
            if(invIDs == null){
                invIDs = inv.id;
            }else{
                invIDs += ',' + inv.id;  
            }
        }  
        insert invLineList;

Regards,
Nihar
This was selected as the best answer