1

Here Amount_c is the formula field and Commission_Total__c is the parent object field where the sum needs to be updated while changing the amount in child object.but the trigger is not firing..please give me solution

trigger totalCommissionAmountTrigger on Commission__c (after insert, after update) 
{
    public Map <ID, List<Commission__c >> mapIdList = NEW   Map <ID, List <Commission__c >> ();
    public List <Commission__c > commissionList = NEW List<Commission__c >();
    public List <AVTRRT__Placement__c> placementList = NEW List <AVTRRT__Placement__c>();
    Set <ID> placementId = NEW Set<ID>();   
    if( (Trigger.isInsert) || (Trigger.isUpdate) )
    {
        System.debug('**************');
        for (Commission__c cms: Trigger.New)
        {
            if (cms.Placement__r.Id != null)
            {
                placementId.add(cms.Placement__r.Id);
                system.debug('-----'+placementId);
            }
        }      
        commissionList = [ SELECT 
                             Spread__c ,Commission__c, Amount__c, Placement__r.Id
                           FROM 
                            Commission__c 
                           WHERE    
                            Placement__r.Id IN:placementId];
        for (Commission__c commission:commissionList)
        {
            if (commission.Amount__c !=null)
            {

                mapIdList.put(commission.Placement__r.Id,commissionList);


            }
         }

        placementList = [SELECT Commission_Total__c FROM AVTRRT__Placement__c WHERE Id IN:placementId];
        List <AVTRRT__Placement__c> updatePlacementList = NEW List <AVTRRT__Placement__c>();
        Decimal total = 0;
        for(AVTRRT__Placement__c p:placementList)
        {


               for(Commission__c c:mapIdList.get(p.Id))
               {                     
                     System.debug('////////////'+c.Amount__c); 
                     if (c.Amount__c!=null) 
                     {

                         total += c.Amount__c;
                     }

               }
               p.Commission_Total__c  = total;                  
               system.debug('--------'+p.Commission_Total__c);
               updatePlacementList.add(p);
         }
        update updatePlacementList;

     }
}
  • What do you mean by "trigger is not firing"? Is it that the above trigger doesn't get invoked when a Commission__c record is inserted or updated? Or is it that the trigger is executed but doesn't function as you expect? – joe May 24 '16 at 14:45
  • 2
    Triggers only execute if the underlying Sobject is affected by a DML event; changes to formula fields are not DML events – cropredy May 24 '16 at 14:54
  • Can you explain your problem a bit briefly.You want your trigger to execute only whenever formula field is updated. @sivanee – Pavan tej May 24 '16 at 15:13
  • yeah...the trigger should fire whenever the formula field gets updated.@Pavan tej – sivanee May 25 '16 at 6:14
1

You can have a rollup field and a workflow to update another amount field when rolled up amount is being changed. In this case you can have a trigger to kick in to do what you want it to do on a parent record.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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