0

I have two custom objects with Master Detail relationship. I need to be able to bulk upsert these records simultaneously. For example:

If the master records don't exist, create them. Otherwise, create them. If detail records don't exist for these master records, create them. Otherwise, update them.

The master object has only two fields: One text field and one checkbox. The detail object has only one additional field that is a checkbox.

In other words, master records and their related detail records may or may not exist. I need to be able to create either single master records or multiple master records (along with their detail records).

I tried to do some research and found this.

This solution is using an external field and that seems to be the right way to go. However, I don't understand how to bulkify the code.

Any help you can provide is appreciated. Let me know if you need more information.

Thank you.

  • Do you want to do this with a file upload? if so, would each line of that file contain data of both the parent and the child?. is the text field that you have on both objects the way to uniquely identify if the record exist? Please put some object names and field names so it is clearer and we can give you a more understandable solution – Sergio Alcocer May 6 at 18:37
  • No, this is not a file upload. This will be done in apex. Here are the object name: Master Record: Waitlist Fields: Product Number: String Waitlisted: Checkbox Detail Record: Waitlist Customer (this is a junction record) Fields: Product Number: String Customer Number: String Waitlisted: Checkbox – Kalpesh May 6 at 19:23
  • What i mean is how is it initiated? VF controller, batch, etc. the solution depends on if you control the initiating part or not (if its a controller (VF, aura, etc), you have some more control than if its starting on a trigger directly) . What is the lookup field between the Waitlist customer and the waitlist? Are all customers created already?, etc. – Sergio Alcocer May 6 at 19:39
  • It will be initiated by user input in a lightning web component. Users will be able to add customers on a waitlist for one or more products – Kalpesh May 6 at 19:45
  • so you will always set the checkbox to true? – Sergio Alcocer May 6 at 19:55
0

There are many ways to meet the requirement, the one below is "query-free", let me know if you cannot do as below and I would post the version with queries

A simplified version (without error control, nor partial commits, nor duplicate records, etc.) would be

In Master Record, (WaitList__c for this example)

  • set the Product Number as Unique External Id (ProductNumber__c)

In second master, (Customer__c for this example)

  • set the Customer Number as Unique External Id (CustomerNumber__c)

In Detail Record, (WaitListCustomer__c for this example)

  • create field (ExternalId__c for this example) that is unique External id and would be the representation of the junction between customer and product. this will contain the {customer}-{product}

Diagram

In your code, lets assume you receive a List<Wrapper> where wrapper is

class Wrapper {
    String Product;
    String Customer; 
} 

Then in your controller you could do

Set<WaitList__c> waitListToUpsert = new Set<WaitList__c>();
Set<WaitListCustomer__c> waitListCustomerToUpsert = new Set<WaitListCustomer__c>();

for (Wrapper forW : yourWrapperList) {
    waitListToUpsert.add(
        new WaitList__c(
            ProductNumber__c = forW.Product, 
            Waitlisted__c = true
        )
    );
    waitListCustomerToUpsert.add(
        new WaitListCustomer__c(
            WaitListLookUp__r = new WaitList__c(
                ProductNumber__c = forW.Product
            ),
            CustomerLookUp__r = new Customer__c(
                CustomerNumber__c = forW.Customer
            ),
            Waitlisted__c = true,
            ExternalId__c = forW.Customer + '-' + forW.Product
        )
    )     
} 
upsert new List<WaitList__c>(waitListToUpsert) ProductNumber__c;
upsert new List<WaitListCustomer__c>(waitListCustomerToUpsert) ExternalId__c;

You might need a trigger (if you can create this records elsewhere) / validation rule (if they are only creatable by your component) to make sure that ExternalId__c is always kept properly

| improve this answer | |
  • Thank you Sergio. I will try this today and test it with various possibilities. I will post here if I need the query version. I will post it either later this evening or early tomorrow morning. Thank you for your help with this issue. – Kalpesh May 6 at 20:20
  • Thank you Sergio - I tested this and it is working perfectly! Thank you so much! – Kalpesh May 6 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.