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
Ronaldo CostaRonaldo Costa 

awesome roll-up summary trigger for opportunity products

Hello all, I have the following code to roll-up summary products name to an opportunity text-long field, I need a summary of purchased products separated by commas.
Everything runs good with no errors, however I get no results, nothing happens, can you please take a look and help me?

Trigger:
 
trigger OppProductsTrigger on OpportunityLineItem (after delete, after insert, after update) {

  // fires after both insert and update
  if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isAfter){

    // find the ids of all opps that were affected
    Set<Id> oppIds = new Set<Id>();
    for (OpportunityLineItem ar : [select Id, name, OpportunityId from OpportunityLineItem 
      where Id IN :Trigger.newMap.keySet()])
      oppIds.add(ar.OpportunityId);

    // process the opps  
    OppProductsTriggerHandler.ProcessProductsAsync(oppIds);


  // fires when records are deleted. may want to do undelete also?
  } else if(Trigger.isDelete && Trigger.isAfter){

    // find the ids of all opps that were affected
    Set<Id> oppIds = new Set<Id>();
    for (ID id : Trigger.oldMap.keySet())
      oppIds.add(Trigger.oldMap.get(id).OpportunityId);

    // process the opps
    OppProductsTriggerHandler.ProcessProductsAsync(oppIds);

  }

}

Class:
 
public with sharing class OppProductsTriggerHandler {

  @future 
  public static void ProcessProductsAsync(Set<ID> oppIds){

    // holds a map of the opp id and comma separated products to build
    Map<Id, String> oppProductMap = new Map<Id, String>();

    // get ALL of the products for all affected opps so we can build
    List<OpportunityLineItem> oppProducts = [select id, name, OpportunityId 
      from OpportunityLineItem 
      where OpportunityId IN :oppIds order by Name];

    for (OpportunityLineItem ar : oppProducts) {
      if (!oppProductMap.containsKey(ar.OpportunityId)) {
        // if the key (opp) doesn't exist, add it with product name
        oppProductMap.put(ar.OpportunityId,ar.Name);
      } else {
        // if the key (opp) already exist, add ", product-name"
        oppProductMap.put(ar.OpportunityId,oppProductMap.get(ar.OpportunityId) + 
          ', ' + ar.Name);
      }
    }

    // get the opps that were affected
    List<Opportunity> opps = [select id from Opportunity where Id IN :oppIds];

    // add the comma separated list of regions
    for (Opportunity a : opps)
      a.Products_Purchased__c = oppProductMap.get(a.id);

    // update the opps
    update opps;

  }  

}


Thank you, thank you!
Ron
 
Ronaldo CostaRonaldo Costa
Anyone please?