You need to sign in to do that
Don't have an account?

Trigger to create multiple child records when a parent is created
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 (Child)
Date Model 2 : Account (Master Parent)<<<<<<Payment(Parent)<<<<<<Payment Line Items (Child) <<<<<< Invoices
Example Salesforce is my account and it has 20 Invoices , now i am creating a Payment ( notthing related to invoice at this time ) record and when i save this payment record 20 payment line items should be created. One record for each invoice salesforce account has .
I can create a child to a parent automatically but not sure how to create mutiple child records based on the Invoices object .
Here is the start with able to create single child record
Thanks,
Akhil
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 (Child)
Date Model 2 : Account (Master Parent)<<<<<<Payment(Parent)<<<<<<Payment Line Items (Child) <<<<<< Invoices
Example Salesforce is my account and it has 20 Invoices , now i am creating a Payment ( notthing related to invoice at this time ) record and when i save this payment record 20 payment line items should be created. One record for each invoice salesforce account has .
I can create a child to a parent automatically but not sure how to create mutiple child records based on the Invoices object .
Here is the start with able to create single child record
trigger CreatePaymentLineItems on Payment__c (after insert) { List<Payment_Item__c> PItem = new List<Payment_Item__c>(); for (Payment__c Pay : trigger.new) { Payment_Item__c PBI = new Payment_Item__c(); PBI.Payment__c = Pay.Id; // PBI.Financial__c = Pay.; PItem.add(PBI); } insert PItem; }
Thanks,
Akhil
All Answers
Thanks for the reply
I am getting an error at
for(Account a : [Select Id,(Select Id from Invoices) where id =: setAccountId]) ---
and
mapAccountIdInvoiceCount.put(a.Id,a.Invoices.size()); // Populate map with Account Id and corresponding list of invoice size -- No field invoices . Here i beleive we are refernceing Custom child object Invoices__C Right?
Let me know if this is solved or you need any help. You can check the cofrrect syntax and field names before using them in query.
Thanks
I still could not get this to working, I checked the names and i am using the correct object names .
Thanks
Invalid field FInancials__C on account
Replace line no 11 with this line
for(Account a : [Select Id,(Select Id from Financials__c) where id IN: setAccountId])
let me knwo if it works.
Thanks
Error: Compile Error: unexpected token: where at line 11 column 65
I tried differnt syntaxes for the IN condition and still having issues
This worked . Thanks sandeep . I have one more question . So when i am adding all invoices(Financials) to the Financial Line item i want to exclude the Invoices which are alreadt created for other Financial Line item . So bascially before we added all invoices for the customer now we have to limit it. Sorry if i am asking for too much
Thanks
Akhil.
Thanks for the solution . I am one step away from completion. Trigger autpmaticaly created 20 records only missing peice is the Lookup Feild getting populated. I want those 20 Invoices filled in the Lookup so that they can access the invoice right from there
Hi I have a question that kind of expands on this question:
I have workshops and individual sessions for that workshop in Salesforce. When a workshop is created I want the sessions to be created based on the information entered on the workshop record. I understand how to do this if the workshop meets just one day a week (see code below) but sometimes these workshops meet two or three times a week and the particular days are stored in a multi picklist. The user can select which days they meet, the start date, and the end date.
I cannot for the life of me figure out how to create ongoing records for sessions occuring on, say, Mondays AND Thursdays AND Wednesdays or any other combo of days. Can anyone help?