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
Akis AthanasiadisAkis Athanasiadis 

Create multiple Records

I have an object named Contract_Renewals__c which is related to Contracts (Master Detail)
Contracts are also related (sipmle lookup) with Contract_Renewals__c.
In contracts I have a checkbox named Renew__c.
What i want is if the Renew__c=True then to create new records of Contract Renewals.
If i have 5 contract renewals, then 5 should be created.
Any help please?
Khan AnasKhan Anas (Salesforce Developers) 
Hi Akis,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. I am using Account and Contact objects to create multiple contacts if a checkbox is checked on Account. If there are 5 Contacts associated with Account then it will create 5 more Contacts on that Account. Kindly modify the code as per your requirement.
 
trigger CreateMultipleContacts on Account (after insert, after update) {
    
    List<Contact> contactFinalListToInsert = New List<Contact>();
    
    if(Trigger.isInsert || Trigger.isUpdate){
        for(Account acc : Trigger.New) {
            if(acc.Checkbox__c == true) {
                Integer fetchingAlreadyExistedRecords = [SELECT count() FROM Contact WHERE AccountId =:acc.Id];
                
                if(fetchingAlreadyExistedRecords!=null) {
                    // We are only creating a records when there is atleast one Contact record exists.
                    for(Integer i=0; i<fetchingAlreadyExistedRecords; i++) {
                        Contact con = new Contact();
                        
                        con.AccountId = acc.Id;
                        con.LastName = 'Sample Name ' + i;
                        contactFinalListToInsert.add(con);
                    }
                }
            }
            
            try{
                if(!contactFinalListToInsert.IsEmpty()){
                    INSERT contactFinalListToInsert;
                }
            }
            catch(Exception e){
                System.debug('The thrown exception for CreatingAutoRecords is:: ' + e.getMessage());
            }
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Akis AthanasiadisAkis Athanasiadis
This seems to be a nice approach. One question here. The contract.id or contractid is not found. do you have any idea what i should use here?
Khan AnasKhan Anas (Salesforce Developers) 
Please check the field API name and use it accordingly.

User-added image

Regards,
Khan Anas
Akis AthanasiadisAkis Athanasiadis
Continuing this approach i may say here the following. The contract renewal records have a specific field which is called Mutli_Year__c and it is a check box.
So what i want is among the 5 records of contract renewals, if i have 3 records without this checkbox checked, then i create one record with the summary of the amount of those records and 2 records from the other two that had the checkbox checked
Akis AthanasiadisAkis Athanasiadis
Take a look at the code plz
 
trigger CreateMultipleContacts on Contract  (before insert, after insert, before update, after update) {
    
    List<Contract> contractFinalListToInsert = New List<Contract>();
    
    if(Trigger.isInsert || Trigger.isUpdate){
        for(Contract  c : Trigger.New) {
            if(c.Renew__c == true) {
                Integer fetchingAlreadyExistedRecords = [SELECT count() FROM Contract_Renewal__c /*WHERE c.id=:Contract__c*/
                ];
                
                if(fetchingAlreadyExistedRecords!=null) {
                    // We are only creating a records when there is atleast one Contact record exists.
                    for(Integer i=0; i<fetchingAlreadyExistedRecords; i++) {
                        Contract_Renewal__c con = new Contract_Renewal__c();
                        
                        con.Contract__c = c.Id;
                        /*con.LastName = 'Sample Name ' + i;
                        contactFinalListToInsert.add(con);*/
                    }
                }
            }
            
            try{
                if(!contractFinalListToInsert.IsEmpty()){
                    INSERT contractFinalListToInsert;
                }
            }
            catch(Exception e){
                System.debug('The thrown exception for CreatingAutoRecords is:: ' + e.getMessage());
            }
        }
    }
}
I have done this.For some reason i get an error on the [select count()] line that is why i have commented this out.
Maybe that is why when i check renew in contracts and I save, no records are created. can u please help me?
 
Khan AnasKhan Anas (Salesforce Developers) 
Use this:

SELECT count() FROM Contract_Renewal__c WHERE Contract__c=:c.Id]
Akis AthanasiadisAkis Athanasiadis
i should mention that there exists a summary field in contract which summarizes the amounts of contract renewals per specific characteristics
Akis AthanasiadisAkis Athanasiadis
nothing happens
Akis AthanasiadisAkis Athanasiadis
i have tried several things here but unfortunately the records are not created