You need to sign in to do that
Don't have an account?
Nihar 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 :
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;
I solved this using the following code :
A MAP to capture whether an invoice has already been created for that account. For invoice line, just loop through attendee list instead of the invoice list.
Regards,
Nihar
All Answers
I have one query related to your requirement.
- 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.
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
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.
so it can be helpful for others.
You can also select your own Answer as Best Answer.
I solved this using the following code :
A MAP to capture whether an invoice has already been created for that account. For invoice line, just loop through attendee list instead of the invoice list.
Regards,
Nihar