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

create new object to summarise records

Hi all

Currently I have an integration which creates records for invoice items, but only at line item level. There's no parent object which shows all related lines and gives overall totals etc.

Is it possible to retrospectively create a new record which consolidates all records with the same invoice number?
I mean you can do some work-arounds where let's say you have 50 Items records and they all have differenet Total__c in it and then you can sum-up all of these total into the first record.

1) Create a field named Grand_Total__c on a Object. (This field will have available on all invoice items records)
2) Find out or pick the record number on which you would like to have all total summed up.
3) Create a validation rule which says when record number is NOT 12358062 and there is a value on Grand_Total__c field then throw an error.
(This way only the record who has number as 12358062 would be able to receive value in Grand_Total__c field)

4) Fun Part:
Create trigger which would summup all the totals__c from all records and populate Grand_Total__c on that choosen record (12358062  one)

Hope it helps!
Glyn Anderson 3Glyn Anderson 3
1) Create your parent object, which I'll call "Invoice_Summary__c".  No need to add any fields yet, except maybe "Invoice_Number__c"
2) Add a Lookup field (not Master-Detail) to Invoice_Summary__c on the child object, which I'm calling "Invoice_Line_Item__c".  I'll call the Lookup field, "Invoice_Summary__c".
3) Run the code below in the Execute Anonymous window of Developer Console.
4) Change the data type of the "Invoice_Summary__c" Lookup field to Master-Detail.
5) Add Rollup Summary Formula fields to the Invoice_Summary__c object to compute the totals you need.
(Of course, you can use whatever names you want...)
Note that this code assumes that you have fewer than 10,000 Invoice_Line_Item__c records and that triggers, etc, won't hit limits when this runs.
(Disclaimer: This code is untested and may contain typos.  Try it in a sandbox and/or use "LIMIT 1" on the query to make sure it works correctly.)

List<Invoice_Line_Item__c> lineItems = [SELECT Id, Invoice_Number__c, Invoice_Summary__c FROM Invoice_Line_Item__c];
Map<String,List<Invoice_Line_Items__c>> lineItemsByInvoiceNumber = new Map<String,List<Invoice_Line_Items__c>>();
Map<String,List<Invoice_Summary__c>> summariesByInvoiceNumber = new Map<String,Invoice_Summary__c>();
for ( Invoice_Line_Item__c item : lineItems )
    if ( ! lineItemsByInvoiceNumber.containsKey( item.Invoice_Number__c ) )
        lineItemsByInvoiceNumber.put( item.Invoice_Number__c, new List<Invoice_Line_Items__c>() );
        summariesByInvoiceNumber.put( item.Invoice_Number__c, new Invoice_Summary__c( Invoice_Number__c = invoiceNumber ) );
    lineItemsByInvoiceNumber.get( item.Invoice_Number__c ).add( item );
insert summariesByInvoiceNumber.values();
for ( Invoice_Line_Item__c item : lineItems )
    item.Invoice_Summary__c = summariesByInvoiceNumber.get( item.Invoice_Number__c ).Id;
update lineItems;
Glyn Anderson 3Glyn Anderson 3
Did any of these answers solve your problem?  If so, please mark the question as "Solved".  If not, let us know.  If you solved it yourself another way, please post your solution.  Thanks!