+ Start a Discussion
Terri HenryTerri Henry 

help with a simple trigger

Hi All,

I need to create a trigger that "rolls up" the field Amount__c from a custom object Sessions to a custom object called Session_Invoice to a field called Invoice_Amount - I can't use a rollup summary field unfortunately as it's just a lookup relationship.

I'm quite new to Salesforce and Coding, so struggling where to start!

Any help would be much appreciated
 
trigger SessionAmountRollup on Session__c (after insert, after update, after delete, after undelete) {
  
}

 
Best Answer chosen by Terri Henry
JeffreyStevensJeffreyStevens
You could try this code inside of your existing trigger...
 
// Get Session_Invoice IDs 
set<id> sessionInvoiceIds = new set<id>();
for(Session__c s :trigger.new) {
  sessionInvoiceIds.add(s.session_Invoice__c);
}

// Get those Session_Invoices and the Sessions
list<Session_Invoice__c> sessionInvoicesToUpdate = new list<Session_Invoice__c>(
  [SELECT id,Invoice_Amount__c,
                  (SELECT id,Amount__c FROM Sessions)
    FROM Session_Invoice__c
    WHERE id IN :sessionInvoiceIds
  ];
);

// Process Session Invoices and Sessions
For(Session_Invoice__c si :sessionInvoicesToUpdate) {
  decimal amountTotal = 0.00;
  for(Session__c s :si.Sessions) {
    amountTotal += s.Amount__c;
  }
  Invoice_Amount__c = amountTotal;
}

// Update Session Invoices
if(sessionInvoiceToUpdate.size()>0) {
  update sessionInvoicesToUpdate;
}

Not sure yet how to handle after delete and after un-delete  - trigger.new isn't populated on after delete - so you'd need to use trigger.oldmap.values() - but I think that gives you a good idea of how to do it.  That should "re-calculate" the entire roll-up after any Session changes.

All Answers

JeffreyStevensJeffreyStevens
Not always that simple.  You could use the App Rollup Helper - it might be just what you need.

If you still want trigger code - let me know.


 
samsafin42samsafin42
Rollup Helper is great - the free version limits the number you can use, but if this is the only one you need, then it should work for you.
Terri HenryTerri Henry
Hi both,

Thanks for your responses - I have actually checked out Rollup Helper - which looks great, however the free version does not roll up in realtime (minimum refresh time is one hour).

Haven't been able to get sign off on the subscription $2k+ p/a

 
JeffreyStevensJeffreyStevens
You could try this code inside of your existing trigger...
 
// Get Session_Invoice IDs 
set<id> sessionInvoiceIds = new set<id>();
for(Session__c s :trigger.new) {
  sessionInvoiceIds.add(s.session_Invoice__c);
}

// Get those Session_Invoices and the Sessions
list<Session_Invoice__c> sessionInvoicesToUpdate = new list<Session_Invoice__c>(
  [SELECT id,Invoice_Amount__c,
                  (SELECT id,Amount__c FROM Sessions)
    FROM Session_Invoice__c
    WHERE id IN :sessionInvoiceIds
  ];
);

// Process Session Invoices and Sessions
For(Session_Invoice__c si :sessionInvoicesToUpdate) {
  decimal amountTotal = 0.00;
  for(Session__c s :si.Sessions) {
    amountTotal += s.Amount__c;
  }
  Invoice_Amount__c = amountTotal;
}

// Update Session Invoices
if(sessionInvoiceToUpdate.size()>0) {
  update sessionInvoicesToUpdate;
}

Not sure yet how to handle after delete and after un-delete  - trigger.new isn't populated on after delete - so you'd need to use trigger.oldmap.values() - but I think that gives you a good idea of how to do it.  That should "re-calculate" the entire roll-up after any Session changes.
This was selected as the best answer
Terri HenryTerri Henry
Thanks Jeffrey!