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
SF7SF7 

Trigger not populating Lookup field on child object

Hi ,

I am Auto creating multiple child records based on other objects . When a Financial Batch  record is created i need to create multiple child records (line items) based on some other related object.  This is the relationship of objects . Account--- Financial_Batch ---- Financial_ Line_Items 
trigger CCMTY_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,Id> accountInvMap = new Map<Id,Id>();

        
        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(Financials__c Inv:[Select Id ,Account__c,Type__c from Financials__c where Status__c NOT IN ('Closed', 'Paid')and Financial_External_Key__c like 'Mexico%' and Account__c IN:SetAccountid Order by Type__c ])
        {
        accountInvMap.put(Inv.id,Inv.Parent_Financials__C);

        }
       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(i).id;
                PItem.add(PBI);
            }
        }
        insert PItem;
        }

User-added image


Every thing is working as intended . When i have 20 Financials on the Account then 20 new Financial Batch items are created as child records to Financila batch when Financial Batch is created . Only issue is i am unable to pupluate Financils__cid on financial Batch item Lookup field