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
BARJRDBARJRD 

Bulk-insert intersection table records

I have two custom objects - Payments and OppPayments. 

 

Payments captures information about the payment of one or more opportunities (e.g., Payment Method (Cash, Check, Credit Card), etc.).

OppPayments is the intersection object joining Payments to Opportunities (containing OpportunityId and PaymentId).  For example, there could be more than one payment on an opportunity (half paid in cash; half paid in check) OR more than one opportunity paid for on a single check.

 

I am trying to figure out how to automatically insert both Payments and OppPayments from a list of Opportunities displayed on a VF page.  I can insert the opportunity list and can generate a Payments list (note that the user has selected a payment method which will apply to all opportunities on the page) which I can also insert.

 

How can I bulk insert OppPayments?  I need to insert Opportunities and Payments first (so I have those Ids). How can I link the Opportunities just inserted with the corresponding Payments just inserted.

 

Thanks!!!

Barb

mtbclimbermtbclimber

You should be able to author a save method in Apex that performs the insertion in the correct sequence.  It might help to see some of your code and/or markup as it's hard to tell how you are capturing the information from the user - either in sequence or in one step.  One thing to note: whenever you insert data via Apex we automatically update the objects with their respective IDs so, for example this test should convey what I mean:

 

 

@IsTest
private class inserttests {

    static testmethod void insertOpp() {
    
        Opportunity o = new Opp(CloseDate = Date.Today(), StageName = 'Closed - Won')
        Database.insert(o);
        System.assert(o.id != null);

    }
}

 Hope that helps. If not, include more detail around your scenario.

 

BARJRDBARJRD

Thanks for your reply.  All is fine when I insert only one Opportunity, Payment, and OppPayment  However, I have a LIST of Opportunities that have not yet been inserted.  In this particular scenario, there will need to be one Payment record and one OppPayment record created for each Opportunity in the opportunity list.

 

I could create a procedure that will loop through the opportunity list and:

1) insert an Opportunity

2) create/insert a Payment

3) create/insert an OppPayment (using the ids just created in steps 1 & 2)

4) redo steps 1-3 for each opp in the opportunity list

 

I was hoping to minimize I/O and perform the following (somehow):

1)  insert the Opportunity list

2) create/insert the Payment list

3) create/insert the OppPayment list --- (**this is where I'm stuck)

 

I don't really have any code yet --- just trying to work out the logic first....

 

Thanks!
Barb

 

mtbclimbermtbclimber

certainly doing things in bulk (lists) becomes more challenging but the same approach applies, You need to insert the opps and payments in bulk first, keep track of which belong to which and then insert the opppayments.   You can simplify management of the association by creating a struct, an apex class that will have two properties - one for the opp and one for the payment.   You'll then have to reconstruct the list of each separately in the save routine to perform the DML statements but the association will be maintaind.

 

Hope that makes sense.

BARJRDBARJRD

Thanks, Andrew.  I'm still a little fuzzy on how to accomplish this --- I can create a class with the opp and the payment as properties, but I'm not sure how to update the class with the opp/payment ids once the opp/payment lists are inserted.  Is there any sample code that you can point me to?   Thank you very much!   Barb

mtbclimbermtbclimber

I was out of the office for the last week. are you still stuck on this?

BARJRDBARJRD

Yes.... I put it on the back burner. Would you happen to have any examples?