You need to sign in to do that
Don't have an account?
![sfdc_newbie17 sfdc_newbie17](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
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?
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?
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!
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.)
<pre>
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;
</pre>