0

I have picklist field "Location" on opportunity object and text field "Type" on Account object. I want to copy the picklist values from all opportunities related to the account and display it on "Type" field on account separated by comma. I tried using process builder but didn't work, tried using below trigger but not working, any help?

trigger UpdateAmount on Opportunity (after insert, after update) { Map parentOpps = new Map(); Set listIds = new Set();

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

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

List lstAccount = new List();

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

}

update parentOpps.values(); }

2

The code you have shared will not at all compile.

  • use close brace here for (Opportunity childObj : Trigger.new)
  • use childObj.AccountId instead of childObj.Account to access AccountId
  • When you use relationship query be sure to use Opportunities instead of Opportunity.
  • You should put Opportunity's Opp_Location__c into a List. Then use String.join() to add those with comma delimited.
  • And finally update accounts.

Trigger

trigger UpdateAmount 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, Location_Type__c ,
                                        (SELECT ID, Opp_Location__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 oppty:acct.Opportunities)
      {
          strType.add(oppty.Opp_Location__c);
      }
      acct.Location_Type__c = String.join(strType,',');

  }

  update parentOpps.values();
}
  • Sorry pretty new to apex, is it possible to update the code? – Admin_BA Apr 27 '17 at 22:29
  • I have given the approach, feel free to change according to your need – Santanu Boral Apr 27 '17 at 22:30
  • May not apply to this situation, but if multiple opportunities on the same account are likely to have the same value in Opp_Location__c, then you can dedupe them by making strType a Set instead of a List and then setting acct.Location_Type__c = String.join(new List<String>(strType),','); – Sequoyah Apr 27 '17 at 23:07
  • I get error Compile Error: Invalid field Opp_Location__c for SObject Opportunity at line 20 column 23 – Admin_BA Apr 28 '17 at 18:43
  • 1
    @Santanu, this is gold. It works brilliantly. – rmarq423 Dec 13 '18 at 14:37

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.