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
james1986james1986 

inserting master-detail records using apex

I'm new to salesforce.com, and looking for some guidance.

 

I need a way to insert a master record, and then its related detail records.

 

I initially though of using the following pseudo-code:

do

     insert master record

     do

           insert detail record and set foriegn key = recently inserted master record

      loop until end of detail records

loop until end of master records

 

This won't work because I will exceed governor limits. Any adivce on how best to proceed?

Best Answer chosen by Admin (Salesforce Developers) 
Cory CowgillCory Cowgill

You would need to bulkify this.

 

Example:

 

Account is Master Record.

Account_Fund__c is Child Record.

Relationship is Master - Detail.

 

List<Account> accounts = new List<Account>();

for(Integer x = 0; x < 10; x++)

{

    Account testAccount = UnitTestFactory.buildTestAccount();

    accounts.add(testAccount);

}

insert accounts; //Uses ONE DML

 

List<Account_Fund__c> accountFunds = new List<Account_Fund__c>();

for(Account currAcct : accounts)

{

for(Integer y = 0; y < 10; y++)

{

     Account_Fund__c testFund = UnitTestFactory.buildTestFund(currAcct.Id);

     accountFunds.add(testFund);

}

}

insert accountFunds; // Use ONE DML Statement to insert all your Accounts.

 

 

All Answers

Cory CowgillCory Cowgill

You would need to bulkify this.

 

Example:

 

Account is Master Record.

Account_Fund__c is Child Record.

Relationship is Master - Detail.

 

List<Account> accounts = new List<Account>();

for(Integer x = 0; x < 10; x++)

{

    Account testAccount = UnitTestFactory.buildTestAccount();

    accounts.add(testAccount);

}

insert accounts; //Uses ONE DML

 

List<Account_Fund__c> accountFunds = new List<Account_Fund__c>();

for(Account currAcct : accounts)

{

for(Integer y = 0; y < 10; y++)

{

     Account_Fund__c testFund = UnitTestFactory.buildTestFund(currAcct.Id);

     accountFunds.add(testFund);

}

}

insert accountFunds; // Use ONE DML Statement to insert all your Accounts.

 

 

This was selected as the best answer
james1986james1986

Thanks Cory! Will give this a try...

james1986james1986

hi Cory,

I think I've basically implemented this, but have run into another problem - this might be better suited for another posting, but there goes:

 

The whole point of this is to automate the entry of recurring monthly donations for a non-profit. Each donation is split up among multiple funds the donor chooses. Pledges will be stored in one object, donations in another. Each pledge and each donation has multiple allocations (hence master-detail structure).

 

While the monthly_giving_allocation and allocations objects are identical (except for the fact that they reference different master objects), the monthly_giving and and donations objects are somewhat different. The donations object has a payment date which is obviously a date, while monthly_giving has a day of month variable which is just an integer. Additionally, donation records have a check box for "posted" as well as a "deposit_batch" # (both are for accounting purposes).

 

I keep gettinging Error: Compile Error: Incompatible element type SOBJECT:Monthly_Gift__c for collection of SOBJECT:Donation__c at line 22 column 9, and I assume its because the columns in the 2 table don't line up perfectly.

 

Is there a way I can SELECT from the monthly_giving object + add a few values for donation records? I'm not sure how to modify the donations list/collection.

 

Any help would be much appreciated,

James

 

----

 

public class updatepledges
{


    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
       
        public updatepledges(ApexPages.StandardController controller)
        {
            //initialize the stanrdard controller
            this.controller = controller;
            
   

 


    List<Donation__c> donations = new List<Donation__c>();
    for (Monthly_Gift__c mg: [SELECT Account__c, Contact__c, PaymentType__c FROM Monthly_Gift__c])
    {
        Monthly_Gift__c Newmg = mg.clone(false);
        donations.add(Newmg);
    }
    insert donations; //Uses ONE DML

    List<Allocation__c> allocations = new List<Allocation__c>();
    for (Donation__c currdon : donations)  
    {
        for(Monthly_Gift_Allocations__C mga: [SELECT Amount__c, Monthly_Gift__c, Program__c, Stream__c FROM Monthly_Gift_Allocations__c])
        {  
            Monthly_Gift_Allocations__c Newmga = mga.clone(false);
            allocations.add(Newmga);
        }
    }
    insert allocations; // Use ONE DML Statement to insert all your Accounts.
}
}

Cory CowgillCory Cowgill

    List<Donation__c> donations = new List<Donation__c>();
    for (Monthly_Gift__c mg: [SELECT Account__c, Contact__c, PaymentType__c FROM Monthly_Gift__c])
    {
        Monthly_Gift__c Newmg = mg.clone(false);
        donations.add(Newmg);
    }

 

You have a list of Donation__c objects, but you are adding an object of type Monthly_Gift__c. You should be adding an object of type Donation__c to that List<>. That is why you are getting a Compile Error.

 

In respect to your other questions, you can retrieve children objects for parent objects in a single query. Take a look at the documentation online, but the jist of it is this:

 

Account a = [Select Id, Name, (Select Id, Name from Account.Contacts) from Account where Id = :inputId];

List<Contact> accountContacts = a.Contacts;

 

You do a sub-select and you give the name of the relationship. Often times for custom objects its Parent_Object__c.Custom_Object__r.

 

 

 

james1986james1986

The difficulty is that I NEED to copy the data from monthly_gifts__c to donations__c; copying data within donations__c isn't really an option.

 

Since this is a new problem, I've started a seperate message: 

http://boards.developerforce.com/t5/Apex-Code-Development/copying-data-from-object1-to-object2/td-p/238271

 

Thanks for your help!

DeepabaliDeepabali

Hi,

Thanks.I was searching something else which I have got from this reply.I was looking for syntax or hoe to get the child object value.I knew the SOQL.But the storing of child data at list is something I was looking for.

 

Thanks.

Kellan ScheiberKellan Scheiber
This worked great. Thank you!