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
brielea1984brielea1984 

Create n Number of Junction Object Records

I'm open to using either a trigger or Flow. I've been trying to do it as a Flow, but I'm getting stuck on getting it to loop through everything and can only get it to create a Demographic for the first Qualification in the list. I'm sure it's something in how to set the Loop and Assign. But I'm more familiar with Apex triggers, so I'm happy to go back to that.

This scenario uses 3 objects: Opportunity, Qualification, and Demographic. 
  • Qualifications have a picklist field called Default_Country__c
  • On Opportunities, the user selects their country from a picklist field Country__c
  • Demographics have a lookup to both Qualification and Opportunity.
I want when an Opportunity is created, for any Qualifications where Default_Country__c matches the Opportunity__c, a Demographic is created. My brain is a bit fried on this so I could use a bit of guidance. 

Thanks!
Best Answer chosen by brielea1984
Bhanu MaheshBhanu Mahesh
Try below code 

Modify
//mapCountryWithQualifications.put(qual.Default_Qualification_Type__c,qual);

to
//mapCountryWithQualifications.put(qual.Default_Qualification_Type__c,lstQual);
 
trigger creatDemoGraphics on Opportunity(after insert){  
  if(trigger.isInsert && trigger.isAfter){  
    Set<String> setCountries = new Set<String>();  
    Map<String,List<Qualification_Library__c>> mapCountryWithQualifications = new Map<String,List<Qualification_Library__c>>();  
    List<Demographic__c> lstDemoGraphicToInsert = new List<Demographic__c>();  
    for(Opportunity opp: trigger.new){  
        if(!String.isBlank(opp.Language_Country__c)){  
            setCountries.add(opp.Language_Country__c);  
        }  
    }  
    if(!setCountries.isEmpty()){  
      for(Qualification_Library__c qual : [SELECT Id,Default_Qualification_Type__c FROM Qualification_Library__c WHERE Default_Qualification_Type__c IN : setCountries]){   
        if(mapCountryWithQualifications != null && mapCountryWithQualifications.get(qual.Default_Qualification_Type__c) != null){  
          mapCountryWithQualifications.get(qual.Default_Qualification_Type__c).add(qual);  
        }  
        else{  
          List<Qualification_Library__c> lstQual = new List<Qualification_Library__c>();  
          lstQual.add(qual);  
          mapCountryWithQualifications.put(qual.Default_Qualification_Type__c,lstQual);  
        }  
      }  
        
      for(Opportunity opp: trigger.new){  
        if(!String.isBlank(opp.Language_Country__c)){  
          if(mapCountryWithQualifications != null && mapCountryWithQualifications.get(opp.Language_Country__c) != null){  
            for(Qualification_Library__c qual : mapCountryWithQualifications.get(opp.Language_Country__c)){  
              Demographic__c demo = new Demographic__c();  
              demo.Opportunity__c = opp.Id;  
              demo.Qualification__c = qual.Id;   
              lstDemoGraphicToInsert.add(demo);  
            }             
          }  
        }  
      }  
        
      if(!lstDemoGraphicToInsert.isEmpty()){  
        insert lstDemoGraphicToInsert;  
      }  
    }  
  }  
}
 
Mark this as "SOLVED" if your query is Answered

Thanks & Regards,
Bhanu Mahesh Gadi

All Answers

Bhanu MaheshBhanu Mahesh
Hi brielea1984;

Try below trigger on opportunity

Please check the API names and add the other required fields in the query and map them while creating the demographic record
 
trigger creatDemoGraphics on Opportunity(after insert){
	if(trigger.issert && trigger.isAfter){
		Set<String> setCountries = new Set<String>();
		Map<String,List<Qualification__c>> mapCountryWithQualifications = new Map<String,List<Qualification__c>>();
		List<Demographic__c> lstDemoGraphicToInsert = new List<Demographic__c>();
		for(Opportunity opp: trigger.new){
				if(!String.isBlank(opp.Country__c)){
						setCountries.add(opp.Country__c);
				}
		}
		if(!setCountries.isEmpty()){
			for(Qualification__c qual : [SELECT Id FROM Qualification__c WHERE Default_Country__c IN : setCountries]){ //Add the required fields in the query
				if(mapCountryWithQualifications != null && mapCountryWithQualifications.get(qual.Default_Country__c) != null){
					mapCountryWithQualifications.get(qual.Default_Country__c).add(qual);
				}
				else{
					List<Qualification__c> lstQual = new List<Qualification__c>();
					lstQual.add(qual);
					mapCountryWithQualifications.put(qual.Default_Country__c,qual)
				}
			}
			
			for(Opportunity opp: trigger.new){
				if(!String.isBlank(opp.Country__c)){
					if(mapCountryWithQualifications != null && mapCountryWithQualifications.get(opp.Country__c) != null){
						for(Qualification__c qual : mapCountryWithQualifications.get(opp.Country__c)){
							Demographic__c demo = new Demographic__c();
							demo.Opportunity__c = opp.Id;
							demo.Qualification__c = qual.Id; //Check the API names and map the other required fields
							lstDemoGraphicToInsert.add(demo);
						}						
					}
				}
			}
			
			if(!lstDemoGraphicToInsert.isEmpty()){
				insert lstDemoGraphicToInsert;
			}
		}
	}
}

NOTE: I have not compiled it. Please check for compilation errors. And let me know if you have any issues.

Mark this as "SOLVED" if your question is answered.


Thanks & Regards,
Bhanu Mahesh Gadi
brielea1984brielea1984
Thank you so much for helping! When putting in the correct integration names (I shortened everything for the summary), I received this error:

User-added image

 
brielea1984brielea1984

whoops, didn't realize it was so small:

Compile Error: Incompatible value type Qualification_Library__c for Map<String, List<Qualification_Library__c>> at line 19 column 11.
trigger creatDemoGraphics on Opportunity(after insert){  
  if(trigger.isInsert && trigger.isAfter){  
    Set<String> setCountries = new Set<String>();  
    Map<String,List<Qualification_Library__c>> mapCountryWithQualifications = new Map<String,List<Qualification_Library__c>>();  
    List<Demographic__c> lstDemoGraphicToInsert = new List<Demographic__c>();  
    for(Opportunity opp: trigger.new){  
        if(!String.isBlank(opp.Language_Country__c)){  
            setCountries.add(opp.Language_Country__c);  
        }  
    }  
    if(!setCountries.isEmpty()){  
      for(Qualification_Library__c qual : [SELECT Id,Default_Qualification_Type__c FROM Qualification_Library__c WHERE Default_Qualification_Type__c IN : setCountries]){   
        if(mapCountryWithQualifications != null && mapCountryWithQualifications.get(qual.Default_Qualification_Type__c) != null){  
          mapCountryWithQualifications.get(qual.Default_Qualification_Type__c).add(qual);  
        }  
        else{  
          List<Qualification_Library__c> lstQual = new List<Qualification_Library__c>();  
          lstQual.add(qual);  
          //mapCountryWithQualifications.put(qual.Default_Qualification_Type__c,qual);  
        }  
      }  
        
      for(Opportunity opp: trigger.new){  
        if(!String.isBlank(opp.Language_Country__c)){  
          if(mapCountryWithQualifications != null && mapCountryWithQualifications.get(opp.Language_Country__c) != null){  
            for(Qualification_Library__c qual : mapCountryWithQualifications.get(opp.Language_Country__c)){  
              Demographic__c demo = new Demographic__c();  
              demo.Opportunity__c = opp.Id;  
              demo.Qualification__c = qual.Id;   
              lstDemoGraphicToInsert.add(demo);  
            }             
          }  
        }  
      }  
        
      if(!lstDemoGraphicToInsert.isEmpty()){  
        insert lstDemoGraphicToInsert;  
      }  
    }  
  }  
}

 
Bhanu MaheshBhanu Mahesh
Try below code 

Modify
//mapCountryWithQualifications.put(qual.Default_Qualification_Type__c,qual);

to
//mapCountryWithQualifications.put(qual.Default_Qualification_Type__c,lstQual);
 
trigger creatDemoGraphics on Opportunity(after insert){  
  if(trigger.isInsert && trigger.isAfter){  
    Set<String> setCountries = new Set<String>();  
    Map<String,List<Qualification_Library__c>> mapCountryWithQualifications = new Map<String,List<Qualification_Library__c>>();  
    List<Demographic__c> lstDemoGraphicToInsert = new List<Demographic__c>();  
    for(Opportunity opp: trigger.new){  
        if(!String.isBlank(opp.Language_Country__c)){  
            setCountries.add(opp.Language_Country__c);  
        }  
    }  
    if(!setCountries.isEmpty()){  
      for(Qualification_Library__c qual : [SELECT Id,Default_Qualification_Type__c FROM Qualification_Library__c WHERE Default_Qualification_Type__c IN : setCountries]){   
        if(mapCountryWithQualifications != null && mapCountryWithQualifications.get(qual.Default_Qualification_Type__c) != null){  
          mapCountryWithQualifications.get(qual.Default_Qualification_Type__c).add(qual);  
        }  
        else{  
          List<Qualification_Library__c> lstQual = new List<Qualification_Library__c>();  
          lstQual.add(qual);  
          mapCountryWithQualifications.put(qual.Default_Qualification_Type__c,lstQual);  
        }  
      }  
        
      for(Opportunity opp: trigger.new){  
        if(!String.isBlank(opp.Language_Country__c)){  
          if(mapCountryWithQualifications != null && mapCountryWithQualifications.get(opp.Language_Country__c) != null){  
            for(Qualification_Library__c qual : mapCountryWithQualifications.get(opp.Language_Country__c)){  
              Demographic__c demo = new Demographic__c();  
              demo.Opportunity__c = opp.Id;  
              demo.Qualification__c = qual.Id;   
              lstDemoGraphicToInsert.add(demo);  
            }             
          }  
        }  
      }  
        
      if(!lstDemoGraphicToInsert.isEmpty()){  
        insert lstDemoGraphicToInsert;  
      }  
    }  
  }  
}
 
Mark this as "SOLVED" if your query is Answered

Thanks & Regards,
Bhanu Mahesh Gadi
This was selected as the best answer
brielea1984brielea1984
That worked!!!! 

Thank you thank you thank you so much! I knew all of the pieces that needed to go into the trigger, but just couldn't get the mess in my head to figure out what needed to be written.

Thanks!