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
manuel.johnsonmanuel.johnson 

Concatenate String Within For Loop

I am trying to create a trigger that will update an opportunity field called "Tinderbox_Sites__c" after a child object called "Site__c" is inserted with a list of all the child Site names. I have created the below trigger and helper class method but for some reason, when there are multiple child sites, the string will just overwrite instead of concatenate the site names. For example,

 

Opportunity A has two child Site__c objects:

- Site 1

- Site 2

 

The "Tinderbox_Sites__c" opportunity field will only display

"Site 2<br />"

 

instead of the expected concatenated string of sites

"Site 1<br />Site 2<br />"

 

. Can anyone help me figure out why the string is overwriting instead of concatenating? Thanks!

 

 

trigger SiteTrigger on Site__c (after insert) {
    
  SiteTriggerHelper helper = new SiteTriggerHelper();

  if(Trigger.isAfter && Trigger.isInsert){
    helper.updateTinderboxSites(Trigger.new);
  }
}

 

 

public void updateTinderboxSites(List<Site__c> sites){

  Set<Id> opportunityIds = new Set<Id>();
  for(Site__c s: sites){
    opportunityIds.add(s.Opportunity__c);
  }
  List<Opportunity> opps = new List<Opportunity>([select Id, Tinderbox_Sites__c from Opportunity where Id in : opportunityIds]);
  List<Opportunity> oppsToUpdate = new List<Opportunity>();
  for(Opportunity o: opps){
    String html = '';
    for(Site__c oppSite: sites){
      html += oppSite.Name+'<br />';
    }
    o.Tinderbox_Sites__c = html;
    oppsToUpdate.add(o);
  }
  update oppsToUpdate;
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
MaxPowerForceMaxPowerForce

Trigger.new probably only contains one of the sites.  You need to get the rest of them from the database and map them to the opportunites.  Try this

 

 

public void updateTinderboxSites(List<Site__c> sites){
 
  Set<Id> opportunityIds = new Set<Id>();
  for(Site__c s: sites){
    opportunityIds.add(s.Opportunity__c);
  }

 
  // Get all of the sites
  List<Site__c> sitesFromQuery = [select Id, Name, Opportunity__c from Site__c where Opportunity__c in :OpportunityIds]);

  // Create a map of the Opportunities to a list of Sites
  Map<Opportunity, List<Site__c>> OpSiteMap = new Map<Opportunity, List<Site__c>>();  
 
  List<Opportunity> opps = new List<Opportunity>([select Id, Tinderbox_Sites__c from Opportunity where Id in : opportunityIds]);
  List<Opportunity> oppsToUpdate = new List<Opportunity>();



  // Loop to map opportunities to a list of sites
  for (site__c site : sitesFromQuery){
    if (OpSiteMap.containsKey(s.Opportunity__c) {
        OpSiteMap.get(s.Opportunity__c).add(site);
    }
        else {
            List<Site__c> siteList = new List<Site__c>();
            siteList.add(site);
            opSiteMap.put(site.Opportunity, siteList);
    }
 }


  for(Opportunity o: opps){
    String html = '';
    List<Site__c> sList = oppSiteMap.get(o);    
    for(Site__c oppSite: sList){        (            
            html += oppSite.Name+'<br />';
        }
    o.Tinderbox_Sites__c = html;
    oppsToUpdate.add(o);
  }
  update oppsToUpdate;
}

 

All Answers

MaxPowerForceMaxPowerForce

Trigger.new probably only contains one of the sites.  You need to get the rest of them from the database and map them to the opportunites.  Try this

 

 

public void updateTinderboxSites(List<Site__c> sites){
 
  Set<Id> opportunityIds = new Set<Id>();
  for(Site__c s: sites){
    opportunityIds.add(s.Opportunity__c);
  }

 
  // Get all of the sites
  List<Site__c> sitesFromQuery = [select Id, Name, Opportunity__c from Site__c where Opportunity__c in :OpportunityIds]);

  // Create a map of the Opportunities to a list of Sites
  Map<Opportunity, List<Site__c>> OpSiteMap = new Map<Opportunity, List<Site__c>>();  
 
  List<Opportunity> opps = new List<Opportunity>([select Id, Tinderbox_Sites__c from Opportunity where Id in : opportunityIds]);
  List<Opportunity> oppsToUpdate = new List<Opportunity>();



  // Loop to map opportunities to a list of sites
  for (site__c site : sitesFromQuery){
    if (OpSiteMap.containsKey(s.Opportunity__c) {
        OpSiteMap.get(s.Opportunity__c).add(site);
    }
        else {
            List<Site__c> siteList = new List<Site__c>();
            siteList.add(site);
            opSiteMap.put(site.Opportunity, siteList);
    }
 }


  for(Opportunity o: opps){
    String html = '';
    List<Site__c> sList = oppSiteMap.get(o);    
    for(Site__c oppSite: sList){        (            
            html += oppSite.Name+'<br />';
        }
    o.Tinderbox_Sites__c = html;
    oppsToUpdate.add(o);
  }
  update oppsToUpdate;
}

 

This was selected as the best answer
asish1989asish1989

This is the simplest trigger for your requirement 

trigger SiteTrigger on Site__c (before insert) {
	List<Opportunity> listofOpportunitys = new List<Opportunity>();
	for(Site__c s :Trigger.new){
		Opportunity oppt = new Opportunity(id = 's.Opportunity__c', Tinderbox_Sites__c = s.Name);
		listofOpportunitys.add(oppt);
		
	}
	
	if(!listofOpportunitys.isEmpty()){
		upsert listofOpportunitys;
	}
}