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
sam_Adminsam_Admin 

Trigger help for mapping values

We have "Location" field on Opportunity with picklist values and we have "Opp_Location__c" field on the account, this trigger displays the values of "Location" field into "Opp_Location__c" field, how to modify the trigger so i can map the same values and not display it more than once? for ex i have picklist value "Hospital" and if the account has 10 opps with same hospital location then i want to display it only one time 


trigger RollupLocation on Opportunity (after insert, after update) { 
  Map<ID, Account > parentOpps = new Map<ID, Account>(); 
  Set<Id> listIds = new Set<Id>();

  for (Opportunity childObj : Trigger.new) {
    listIds.add(childObj.AccountId);
  }

  parentOpps = new Map<Id, Account>([SELECT id, Opp_Location__c ,
                                        (SELECT ID, Location_Type__c  FROM Opportunities) 
                                    FROM Account WHERE ID IN :listIds]);

  List<Account> lstAccount = new List<Account>();

  for(Account acct:parentOpps.values())
  {  
      List<String> strType = new List<String>();
      for(Opportunity opty:acct.Opportunities)
      {
          strType.add(opty.Location_Type__c);
      }
      acct.Opp_Location__c = String.join(strType,',');

  }

  update parentOpps.values();
}
Best Answer chosen by sam_Admin
Damon ShawDamon Shaw
Hi Sam, 

I guess I should have tested that haha, 

try this, put the values into a Set, then loop over the set and buld up your string
trigger RollupLocation on Opportunity (after insert, after update) { 
  


  Map<ID, Account > parentOpps = new Map<ID, Account>(); 
  Set<Id> listIds = new Set<Id>();

  for (Opportunity childObj : Trigger.new) {
    listIds.add(childObj.AccountId);
  }

  parentOpps = new Map<Id, Account>([SELECT id, Opp_Location__c ,
                                        (SELECT ID, Location_Type__c  FROM Opportunities) 
                                    FROM Account WHERE ID IN :listIds]);

  List<Account> lstAccount = new List<Account>();

  for(Account acct:parentOpps.values())
  {  
      Set<String> strType = new Set<String>();
      for(Opportunity opty:acct.Opportunities)
      {
          strType.add(opty.Location_Type__c);
      }
      
      String oppLocation = '';
      for(String s :strType)
      {
        oppLocation += (oppLocation == '' ? '' : ',' )+s;
      }
      acct.Opp_Location__c = oppLocation;

  }

  update parentOpps.values();
}

 

All Answers

Damon ShawDamon Shaw
Hi Sam,

If you change the List on line 17 (List<String> strType = new List<String>();) to a Set it should fix the issue, sets only contain 1 instance of a value, so if you put 'Hospital' into Set<String> strType 3 times strType would = {'Hospital'}

hope this helps
sam_Adminsam_Admin
When i changed it to  List<Account> lstAccount = new List<Account>();, i got error "Method does not exist or incorrect signature: void join(Set<String>, String) from the type String at line 30 column 37"
Damon ShawDamon Shaw
Hi Sam, 

I guess I should have tested that haha, 

try this, put the values into a Set, then loop over the set and buld up your string
trigger RollupLocation on Opportunity (after insert, after update) { 
  


  Map<ID, Account > parentOpps = new Map<ID, Account>(); 
  Set<Id> listIds = new Set<Id>();

  for (Opportunity childObj : Trigger.new) {
    listIds.add(childObj.AccountId);
  }

  parentOpps = new Map<Id, Account>([SELECT id, Opp_Location__c ,
                                        (SELECT ID, Location_Type__c  FROM Opportunities) 
                                    FROM Account WHERE ID IN :listIds]);

  List<Account> lstAccount = new List<Account>();

  for(Account acct:parentOpps.values())
  {  
      Set<String> strType = new Set<String>();
      for(Opportunity opty:acct.Opportunities)
      {
          strType.add(opty.Location_Type__c);
      }
      
      String oppLocation = '';
      for(String s :strType)
      {
        oppLocation += (oppLocation == '' ? '' : ',' )+s;
      }
      acct.Opp_Location__c = oppLocation;

  }

  update parentOpps.values();
}

 
This was selected as the best answer
sam_Adminsam_Admin
Works fantastic, thanks for the help