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
AkankshaAkanksha 

Trigger written to create multiple records but is creating a single one

There is a junction object New Sponsorship between 2 Masters Recurring Gift and Child. Also, one of master record Recurring gift is related to Contact through look up. We need to create sponsorship when an opportunity (related to Recurring Gift, New Sponsorship) is created. If the child Id field on Opportunity record contains multiple ids then multiple New Sponsorship records should be created. I have a for loop for the list of Child ids.
The trigger is firing but is creating only a single record rahter than multiple records. Any help would be great !

The code is

trigger CreateNewSponsorship on Opportunity (before insert , before update) {
    RecordType rd = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' AND Name = 'Recurring Donation'];
    
    Set<Id> sponsorIds = new Set<Id>();
    for (Opportunity opp : trigger.new) {
    
        sponsorIds.add(opp.cv__Contact__c);
    }
 // R00NU0000000sKlMMAU represent relation ship between recurring gift and contact object
   Map<Id, Contact> sponsors = new Map<Id, Contact>([SELECT Id, (SELECT cv__Contact__c FROM R00NU0000000sKlMMAU) FROM Contact WHERE Id IN :sponsorIds]); 
        
        Map<Id, New_Sponsorship__c> sponsorships = new Map<Id, New_Sponsorship__c>();
    for (Opportunity opp : trigger.new) {
                System.debug('Inside for loop');
                System.debug('Opportunity Child Id is : '+opp.Child_ID__c);
              //  String ch  =   opp.Child_ID__c.substring(0,15);
              List<String> strChildIds = opp.Child_ID__c.split(',');
                 if(trigger.isInsert && trigger.isBefore){
           if (opp.RecordTypeId == rd.Id && opp.cv__Recurring_Gift__c != null && opp.cv__Contact__c != null && opp.Child_ID__c != null
         && (!sponsorships.containsKey(opp.cv__Recurring_Gift__c) || sponsorships.get(opp.cv__Recurring_Gift__c).Start_Date__c > opp.CloseDate)) {
            Contact sponsor = sponsors.get(opp.cv__Contact__c);
              //start the loop for multiple sponsorship creation
              for( String str : strChildIds){
                sponsorships.put(
                opp.cv__Recurring_Gift__c,
                new New_Sponsorship__c(
                 //   Sponsor__c = opp.cv__Contact__c,
                      Child__c =  [SELECT Id FROM Child__c WHERE Id = :str LIMIT 1 ].Id  ,                    
                      Recurring_Gift_2__c = opp.cv__Recurring_Gift__c,
                    Start_Date__c = opp.CloseDate
               //     Assignment_type__c = sponsor != null && sponsor.New_Sponsorships__r.size() > 0 ? 'Additional' : 'New Enroll'
                )
                );
                }
         }
        }
        if(trigger.isUpdate && trigger.isBefore){
           if (opp.RecordTypeId == rd.Id && opp.cv__Recurring_Gift__c != null && opp.cv__Contact__c != null && opp.Child_ID__c != null && trigger.oldmap.get(opp.id).Child_ID__c==null
         && (!sponsorships.containsKey(opp.cv__Recurring_Gift__c) || sponsorships.get(opp.cv__Recurring_Gift__c).Start_Date__c > opp.CloseDate)) {
            Contact sponsor = sponsors.get(opp.cv__Contact__c);
            //start the loop for multiple sponsorship creation
              for( String str : strChildIds){
                sponsorships.put(
                opp.cv__Recurring_Gift__c,
                new New_Sponsorship__c(
                 //   Sponsor__c = opp.cv__Contact__c,
                      Child__c =  [SELECT Id FROM Child__c WHERE Id = :str LIMIT 1 ].Id  ,                    
                      Recurring_Gift_2__c = opp.cv__Recurring_Gift__c,
                    Start_Date__c = opp.CloseDate
               //     Assignment_type__c = sponsor != null && sponsor.New_Sponsorships__r.size() > 0 ? 'Additional' : 'New Enroll'
                )
                );
                }
         }
        }
        
    }
    
    Map<Id, cv__Recurring_Gift__c> recurringGifts = new Map<Id, cv__Recurring_Gift__c>([SELECT Id, Sponsorship__c FROM cv__Recurring_Gift__c WHERE Id IN :sponsorships.keySet()]);


    insert sponsorships.values();
    
    // update gift sponsorships
  //  List<cv__Recurring_Gift__c> giftsToUpdate = new List<cv__Recurring_Gift__c>();
 //   for (Id giftId : sponsorships.keySet()) {
  //      giftsToUpdate.add(new cv__Recurring_Gift__c(Id = giftId, Sponsorship__c = sponsorships.get(giftId).Id));
 //   }
 //   update giftsToUpdate;
    
    // update donation sponsorships
    for (Opportunity opp : trigger.new) {
        if (sponsorships.containsKey(opp.cv__Recurring_Gift__c)) {
            opp.New_Sponsorship__c = sponsorships.get(opp.cv__Recurring_Gift__c).Id;
        } else if (recurringGifts.containsKey(opp.cv__Recurring_Gift__c)) {
            opp.New_Sponsorship__c = recurringGifts.get(opp.cv__Recurring_Gift__c).Sponsorship__c;
        }
    }
}
sfdcchampionssfdcchampions
Replace strikthrough code with the corrected one.

//start the loop for multiple sponsorship creation
              for( String str : strChildIds){
                sponsorships.put(
                opp.cv__Recurring_Gift__c,
                new New_Sponsorship__c(
                 //   Sponsor__c = opp.cv__Contact__c,
                      Child__c =  [SELECT Id FROM Child__c WHERE Id = :str LIMIT 1 ].Id  ,                    
                      Recurring_Gift_2__c = opp.cv__Recurring_Gift__c,
                    Start_Date__c = opp.CloseDate
               //     Assignment_type__c = sponsor != null && sponsor.New_Sponsorships__r.size() > 0 ? 'Additional' : 'New Enroll'
                )
                );
                }




for( String str : strChildIds){
    
    if(sponsorships.containsKey(opp.cv__Recurring_Gift__c))
        sponsorships.get(opp.cv__Recurring_Gift__c).add(new New_Sponsorship__c(Child__c =  [SELECT Id FROM Child__c WHERE Id = :str LIMIT 1 ].Id , Recurring_Gift_2__c = opp.cv__Recurring_Gift__c,    Start_Date__c = opp.CloseDate))
    else 
        sponsorships.put(
                opp.cv__Recurring_Gift__c,
                new New_Sponsorship__c(
                 //   Sponsor__c = opp.cv__Contact__c,
                      Child__c =  [SELECT Id FROM Child__c WHERE Id = :str LIMIT 1 ].Id  ,                    
                      Recurring_Gift_2__c = opp.cv__Recurring_Gift__c,
                    Start_Date__c = opp.CloseDate
               //     Assignment_type__c = sponsor != null && sponsor.New_Sponsorships__r.size() > 0 ? 'Additional' : 'New Enroll'
                )
                );
}


Let me know if this works for you.
AkankshaAkanksha
I got my code running and creating multiple records as follows. Thanks sfdcchampions but when i replaced my code with your code it was throwing error like method doesn not exist for [New_Sponsorship__c].add(New_Sponsorship__c).

Now I need to check duplicates opportunity if sponsorship already exists for same Recurring gift and Child Ids combination.
any help for checking the child id(break the string into individual ids) of opportunity already present in another opportunity for same recurring gift.


trigger CreateNewSponsorship on Opportunity (before insert , before update) {
    RecordType rd = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' AND Name = 'Recurring Donation'];
   
    Set<Id> sponsorIds = new Set<Id>();
    for (Opportunity opp : trigger.new) {
   
        sponsorIds.add(opp.cv__Contact__c);
    }
    Map<Id, Contact> sponsors = new Map<Id, Contact>([SELECT Id, (SELECT cv__Contact__c FROM R00NU0000000sKlMMAU) FROM Contact WHERE Id IN :sponsorIds]);
       
        Map<Id, New_Sponsorship__c> sponsorships = new Map<Id, New_Sponsorship__c>();
    for (Opportunity opp : trigger.new) {
                System.debug('Inside for loop');
                System.debug('Opportunity Child Id is : '+opp.Child_ID__c);
              //  String ch  =   opp.Child_ID__c.substring(0,15);
              List<String> strChildIds = opp.Child_ID__c.split(',');
                 if(trigger.isInsert && trigger.isBefore){
           if (opp.RecordTypeId == rd.Id && opp.cv__Recurring_Gift__c != null && opp.cv__Contact__c != null && opp.Child_ID__c != null
         && (!sponsorships.containsKey(opp.cv__Recurring_Gift__c) || sponsorships.get(opp.cv__Recurring_Gift__c).Start_Date__c > opp.CloseDate)) {
            Contact sponsor = sponsors.get(opp.cv__Contact__c);
              //start the loop for multiple sponsorship creation
              for( String str : strChildIds){
              System.debug('Inside for loop : Child Id : '+str);
                sponsorships.put(
                opp.cv__Recurring_Gift__c,
                new New_Sponsorship__c(
                 //   Sponsor__c = opp.cv__Contact__c,
                      Child__c =  [SELECT Id FROM Child__c WHERE Id = :str LIMIT 1 ].Id  ,                   
                      Recurring_Gift_2__c = opp.cv__Recurring_Gift__c,
                    Start_Date__c = opp.CloseDate
               //     Assignment_type__c = sponsor != null && sponsor.New_Sponsorships__r.size() > 0 ? 'Additional' : 'New Enroll'
                )
                );
               insert sponsorships.values();
                }
         }
        }
        if(trigger.isUpdate && trigger.isBefore){
           if (opp.RecordTypeId == rd.Id && opp.cv__Recurring_Gift__c != null && opp.cv__Contact__c != null && opp.Child_ID__c != null && trigger.oldmap.get(opp.id).Child_ID__c==null
         && (!sponsorships.containsKey(opp.cv__Recurring_Gift__c) || sponsorships.get(opp.cv__Recurring_Gift__c).Start_Date__c > opp.CloseDate)) {
            Contact sponsor = sponsors.get(opp.cv__Contact__c);
            //start the loop for multiple sponsorship creation
              for( String str : strChildIds){
                sponsorships.put(
                opp.cv__Recurring_Gift__c,
                new New_Sponsorship__c(
                 //   Sponsor__c = opp.cv__Contact__c,
                      Child__c =  [SELECT Id FROM Child__c WHERE Id = :str LIMIT 1 ].Id  ,                   
                      Recurring_Gift_2__c = opp.cv__Recurring_Gift__c,
                    Start_Date__c = opp.CloseDate
               //     Assignment_type__c = sponsor != null && sponsor.New_Sponsorships__r.size() > 0 ? 'Additional' : 'New Enroll'
                )
                );
              insert sponsorships.values();  
                }
         }
        }
       
    }
   
    Map<Id, cv__Recurring_Gift__c> recurringGifts = new Map<Id, cv__Recurring_Gift__c>([SELECT Id, Sponsorship__c FROM cv__Recurring_Gift__c WHERE Id IN :sponsorships.keySet()]);


   // insert sponsorships.values();
   
    // update gift sponsorships
  //  List<cv__Recurring_Gift__c> giftsToUpdate = new List<cv__Recurring_Gift__c>();
 //   for (Id giftId : sponsorships.keySet()) {
  //      giftsToUpdate.add(new cv__Recurring_Gift__c(Id = giftId, Sponsorship__c = sponsorships.get(giftId).Id));
 //   }
 //   update giftsToUpdate;
   
    // update donation sponsorships
    for (Opportunity opp : trigger.new) {
        if (sponsorships.containsKey(opp.cv__Recurring_Gift__c)) {
            opp.New_Sponsorship__c = sponsorships.get(opp.cv__Recurring_Gift__c).Id;
        } else if (recurringGifts.containsKey(opp.cv__Recurring_Gift__c)) {
            opp.New_Sponsorship__c = recurringGifts.get(opp.cv__Recurring_Gift__c).Sponsorship__c;
        }
    }
}