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
SFDC9999SFDC9999 

Trigger to create multiple child records with related information, Missing Lookup

Hi ,
i am tryinng to create multiple child records when a parent record is created . The records in child object should be coming from an another relaetd object . Here is my data model

Data model 1: Account (Parent)<<<<<< (Invoices)Financials__c (Child to Account)
Date Model 2 : Account (Master Parent)<<<<<<[Payment Batch] FInancial_Batch__c(Parent)<<<<<<Financial_Batch_Items__c [payment LIne items] (Child) <<<<<< Financilals__C (LookUp on Batch Items)

Use case: Customer paying for the invoices he has open [ excluding already in the process of payment ]

Example: Apple is my account and it has 20 Financials(Invoices) , now i am creating a Financial Batch ( nothing related to invoice at this time ) record and when i save this Fianancial Batch record 20 Financial Batch items should be created. One record for each Financial(Invoice) record Apple account has and the financial(Invoice) record should not be part of another Financial Batch Items .

This is what done already : Trigger finds the related number(20) invoices and creates 20 line items but missing Lookup to the orginal Invoice and not checking if it is referred in another Financial Batch/ Financial Batch Item
 
trigger CreatePaymentLineItems on Financial_Batch__c (after insert) {
          
        List<Financial_Batch_Item__c> PItem = new List<Financial_Batch_Item__c>();
        
        Set<Id> setAccountId = new Set<Id>(); // Set of Id to hold Account id's in Payment
        
        Map<Id,Financials__c> accountInvMap = new Map<Id,Financials__c>();

        
        Map<Id,Integer> mapAccountIdInvoiceCount = new Map<Id,Integer>(); // Map of Id to Integer to hold Account Id and corresponding number of Invoices
        
        for (Financial_Batch__c Pay : trigger.new) 
        {
            setAccountId.add(pay.Account__c); //Here Account__c is the relationship field in Financial_Batch__c which relates to Account
        }
      
       for(Account a : [Select Id,(Select Id ,Type__c from Financials__r where Status__c NOT IN ('Closed', 'Paid')and Financial_External_Key__c like 'Mexico%' Order by Type__c) from account where id IN: setAccountId])
        {
            mapAccountIdInvoiceCount.put(a.Id,a.Financials__r.size()); // Populate map with Account Id and corresponding list of invoice size
        }
        for (Financial_Batch__c Pay : trigger.new) 
        {
            for(Integer i=0; i<mapAccountIdInvoiceCount.get(pay.Account__c);i++)
            {
                Financial_Batch_Item__c PBI = new Financial_Batch_Item__c();
                PBI.Financial_Batch__c = Pay.Id;
                //PBI.Financials__c = accountinvMap.get(inv).id;
                PItem.add(PBI);
            }
        }
        insert PItem;
        }

Thanks