Skip to main content
So the structure I have is this:

Record of Custom Object Type A has a related list of multiple records of Custom Object Type B.

And this is what I'm looking to do:

Object Type B has a Currency field, and Object Type A has a field that is inteded to be the Sum of that Currency field for all records in the related list. Whenever any record of Object Type B is updated, I need the Sum field on Object Type A to calculate the new sum. (Essentially it is a rollup sum, but I cannot use a rollup field because these do not have a Master-Detail relationship.)

Example:

Initial State:

  • Object Type A Record SumField = 100

    • Related ObjectB Record 1 CurrencyField = 50
    • Related ObjectB Record 2 CurrencyField = 30
    • Related ObjectB Record 3 CurrencyField = 20

User Action:

  • Related ObjectB Record 2 CurrencyField is changed from 30 to 80

Desired End State:

  • Object Type A Record SumField = 150
    • Related ObjectB Record 1 CurrencyField = 50
    • Related ObjectB Record 2 CurrencyField = 80
    • Related ObjectB Record 3 CurrencyField = 20
16 Antworten
  1. 22. Juli 2016, 18:19
    As noted, a rollup summary cannot be used because these objects do not have a Master-Detail relationship.

    But, I did find an answer elsewhere and have translated it into a generic form for anyone else who needs it:

    trigger TRIGGERNAME on Child_Object__c (after insert, after delete, after update) {

    if(Trigger.isInsert){

    try {

    for (Child_Object__c co : Trigger.new){

    Parent_Object__c po = [SELECT Id, Sum_Field__c FROM Parent_Object__c WHERE Id = :co.Parent_Lookup__c];

    List<Child_Object__c> l_co = [SELECT Id, Amount__c FROM Child_Object__c WHERE Parent_Lookup__c = :po.Id];

    for(Child_Object__c am_co : l_co) {

    amount += am_co.Amount__c;

    }

    po.Sum_Field__c = amount;

    update po;

    }

    } catch (Exception e) {

    System.debug(e);

    }

    }

    if(Trigger.isAfter) {

    if(Trigger.isUpdate){

    try {

    for (Child_Object__c co : Trigger.old){

    Parent_Object__c po = [SELECT Id, Sum_Field__c FROM Parent_Object__c WHERE Id = :co.Parent_Lookup__c];

    List<Child_Object__c> l_co = [SELECT Id, Amount__c FROM Child_Object__c WHERE Parent_Lookup__c = :po.Id];

    for(Child_Object__c am_co : l_co) {

    amount += am_co.Amount__c;

    }

    po.Sum_Field__c = amount;

    update po;

    }

    } catch (Exception e) {

    System.debug(e);

    }

    }

    if(Trigger.isDelete){

    try {

    for (Child_Object__c co : Trigger.old){

    Parent_Object__c po = [SELECT Id, Sum_Field__c FROM Parent_Object__c WHERE Id = :co.Parent_Lookup__c];

    List<Child_Object__c> l_co = [SELECT Id, Amount__c FROM Child_Object__c WHERE Parent_Lookup__c = :po.Id];

    for(Child_Object__c am_co : l_co) {

    amount += am_co.Amount__c;

    }

    po.Sum_Field__c = amount;

    update po;

    }

    } catch (Exception e) {

    System.debug(e);

    }

    }

    }

    }

    **The __c portion of the names is left off if working with a Standard Object/Field