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
JoshTonksJoshTonks 

Creating multiple records equal to a quantity field.

Hi I was hoping someone would be able to give me a little advice or point me in the right direction if the information is already documented somewhere. Im looking to build some functionality onto an object where the quantity of a quantity field would define the quantity of records to create. Similar to how payment schedule works on products. So if I specified 10 in quantity box it would create 10 related object records.
Best Answer chosen by JoshTonks
HARSHIL U PARIKHHARSHIL U PARIKH
Hello Josh,

I have thought about it doing from process builder but there are lots of scenarios that I can think of which process builder wouldn't be able to take care but however, Let me offer you a trigger which would do the job. Giev it to your developer and he would modify it based on your objects and requirements a little but the real bulk is alredy WRITTEN!!
Parent Object: Contact
             Fields: Quantity__c
Child Object: Credit__c
             Fields: Contact__c (a lookup relation field to contact)


This is what the trigger does:
Whenever you create a Contact record and provide 5 (or any number) on Quantity__c field then trigger would create 5 (or any number) of records for associatd child object.
(e.g., if I create a contact record named Josh Tonks and provide nothing in Quantity__c field then it will not create any automatic child records.
If I provide 10 inside a Quantity__c field then it will automatically generate 10 Credit__c object record for this contact named Josh Tonk)

1) Let's put NULL or ZERO in Quantity__c field.
          User-added image
2) Now, let's enter 10 inside that Quantity__c field and see the magic of 10 child record creation automatically
   User-added image

Trigger Code:
 
Trigger CreatingAutoRecords on Contact(After Insert, After Update)
{
    
    List<Credit__c> creditRecordsFinalListToInsert = New List<Credit__c>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate)
    {
        For(Contact con : Trigger.New)
        {
            If(con.Quantity__c != null)
            {
                List<Credit__c> fetchingAlreadyExixtsedRecords = [Select Id FROM Credit__c WHERE Contact__c =:con.Id];
                
                If(fetchingAlreadyExixtsedRecords.IsEmpty())
                {
                    // We are only creating a records when there is no Credit__c records exixts.
                    For(Integer I = 0; I < con.Quantity__c; I++)
                    {
                        Credit__c crd = New Credit__c();
                        
                        crd.contact__c = con.Id;
                        crd.Name       = 'Sample Name' + I;
                        creditRecordsFinalListToInsert.add(crd);
                    }
                }
                
            }
            
            try{
                If(!creditRecordsFinalListToInsert.IsEmpty()){
                    insert creditRecordsFinalListToInsert;
                }
            }
            Catch(Exception e){
                System.debug('The thrown exception for CreatingAutoRecords is:: ' + e.getMessage());
            }
        }
    }
    
    
}

You or your developer person can definitely modify the trigger in a way which works best for your org requirements. But as far as the auto creation goes, it has been taken cared by trigger.

Hope this helps and if it answers your question then please mark it as best answer since it will help other users in community with the similar query!

All Answers

HARSHIL U PARIKHHARSHIL U PARIKH
Yes this would be possibe in my opinion.

Let's say you have a Parent object name ObjA and Child object name ObjB and there is a field name Quantity__c on ObjA.
You can write a trigger (or may be a process builder) which would create a 10 records of child object ObjB when Quantity__c on ObjA is = 10.

There are some things you may need to take care of which is what to do when Quantity__c changes from 10 to 50? Do trigger creates another 50 records? Or even Quantity__c changes after it has been inserted?

lets us know the requirements in depth and we will see if that can be achieved via process builder or an Apex Trigger.

Hope this helps!
JoshTonksJoshTonks
Hi, Ill explain it in a little more detail. We do monthly health checks with a customer always on the same day of the month. If the customer signs up for 12 months for instance we would have a quantity of 12. When the customer signs up we send them a health check schedule which at the moment is a manual process done in word and it done by one person it's a lot of work for them.

I am looking to do something similar to the payment schedule on products on opportunities but for health checks instead. Would you know of any documentation out there that would get me on the right track.

If it can be done via process builder then thats good as I can document and explain it to the other guy I work a long side. Im studying to get on with my development stuff at the moment and have some apex knowledge. Let me know if you think it can be done in process builder and ill have a go at getting it done.
HARSHIL U PARIKHHARSHIL U PARIKH
Hello Josh,

I have thought about it doing from process builder but there are lots of scenarios that I can think of which process builder wouldn't be able to take care but however, Let me offer you a trigger which would do the job. Giev it to your developer and he would modify it based on your objects and requirements a little but the real bulk is alredy WRITTEN!!
Parent Object: Contact
             Fields: Quantity__c
Child Object: Credit__c
             Fields: Contact__c (a lookup relation field to contact)


This is what the trigger does:
Whenever you create a Contact record and provide 5 (or any number) on Quantity__c field then trigger would create 5 (or any number) of records for associatd child object.
(e.g., if I create a contact record named Josh Tonks and provide nothing in Quantity__c field then it will not create any automatic child records.
If I provide 10 inside a Quantity__c field then it will automatically generate 10 Credit__c object record for this contact named Josh Tonk)

1) Let's put NULL or ZERO in Quantity__c field.
          User-added image
2) Now, let's enter 10 inside that Quantity__c field and see the magic of 10 child record creation automatically
   User-added image

Trigger Code:
 
Trigger CreatingAutoRecords on Contact(After Insert, After Update)
{
    
    List<Credit__c> creditRecordsFinalListToInsert = New List<Credit__c>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate)
    {
        For(Contact con : Trigger.New)
        {
            If(con.Quantity__c != null)
            {
                List<Credit__c> fetchingAlreadyExixtsedRecords = [Select Id FROM Credit__c WHERE Contact__c =:con.Id];
                
                If(fetchingAlreadyExixtsedRecords.IsEmpty())
                {
                    // We are only creating a records when there is no Credit__c records exixts.
                    For(Integer I = 0; I < con.Quantity__c; I++)
                    {
                        Credit__c crd = New Credit__c();
                        
                        crd.contact__c = con.Id;
                        crd.Name       = 'Sample Name' + I;
                        creditRecordsFinalListToInsert.add(crd);
                    }
                }
                
            }
            
            try{
                If(!creditRecordsFinalListToInsert.IsEmpty()){
                    insert creditRecordsFinalListToInsert;
                }
            }
            Catch(Exception e){
                System.debug('The thrown exception for CreatingAutoRecords is:: ' + e.getMessage());
            }
        }
    }
    
    
}

You or your developer person can definitely modify the trigger in a way which works best for your org requirements. But as far as the auto creation goes, it has been taken cared by trigger.

Hope this helps and if it answers your question then please mark it as best answer since it will help other users in community with the similar query!
This was selected as the best answer
JoshTonksJoshTonks
I really appreciate this thank you very much. We don't have a developer so its going to be myself modifying it. You have really helped me out.
Alex LeplumeyAlex Leplumey

Hi,

this solution is great! I have a question though.

I need to create a multiple custom child object records and I'm thinking about using the trigger above.

My question is when you update the quantity from the parent object, is there a way to delete all the associated child object and create new ones, and this without deleting other child objects (different child object that the one I wish to create).

Thanks a lot!
Alex

BobPBobP
Good morning HARSHIL ,
I like your solution to create mutiple records. I was wondering if the same code can be used to create multiple records at once with different record types.  

Say i have two qauntity fields Record Type 1 Qauntity and Record Type 2 Qauntity on an opportunity object. The user select the qauintities for Record Type 1 then the Record Type 2 after save the trigger created the custom object records  Can this be accomplished with adjusting your code below?  I do know some developing but have been stuck on how to acheive this for multiple record types. Any help would be greatly appreciated. 
Mildred Morales 19Mildred Morales 19
Just wondering can this be accomplish using process builder and flow instead of an Apex Trigger?
 
Chelsea Hirschton 9Chelsea Hirschton 9
@Mildred Morales—It looks like it can. Check out this other post I found. https://success.salesforce.com/answers?id=9063A000000lQcOQAU

I'm trying to do this myself currently and running into some challenges, more that I just don't know flow well enough.  
Mildred Morales 19Mildred Morales 19
I actually posted the same questions on the Traiblazer Community and was able to accomplished what and wanted and more :-) This is the post https://success.salesforce.com/answers?id=9063A000000lQcO 
dpardpar
@HARSHIL PARIKH AKA:Govind 
Can we do update and delete the same thing with bulky safe