0

I have a custom object (Apttus__APTS_Agreement__c) where I want to roll-up child records to it's parent record. Since this is on the same object, it cannot be a rollup summary field and has to be a trigger. I have started writing a trigger but could use some assistance to complete it. Its fairly simple what it needs to do, which is to roll-up agreement records that have status not equal to approved and has a parent record associated to it or not null. I will post the trigger and class in the answer below. Appreciate any assistance, thanks.

2 Answers 2

0

//Trigger trigger ApttusChildRollup on Apttus__APTS_Agreement__c (after update,after insert) {

if (TriggerContextUtility.isFirstRun()) {

System.debug (logginglevel.DEBUG, '@@@@@@@@ Step 1');

//Loop through all the Agreements that caused the trigger and add their associated IDs to a Set

set<Id> AgreementIds = new set<Id>();

for (integer i=0;i<trigger.size;i++)
{
    if((Trigger.isInsert || Trigger.isUpdate)  && trigger.new[i].Apttus__Parent_Agreement__c!= null
       && trigger.new[i].Pricing_Approval_Status__c <> 'Approved')
    {
        AgreementIds.add(trigger.new[i].ID);
    }


}
RollupChildAgreements.ChildAgreements(AgreementIds);
}    

}

//Class public without sharing class RollupChildAgreements {

public static void ChildAgreements(set<Id> AgreementIds)

{ list AgreementsTobeUpdated = new list(); list ParentAgreements = [select Pricing_Approval_Status__c from Apttus__APTS_Agreement__c where Id in :Agreementids];

System.debug('Method called with:@@@@@@@' + agreementids +'Step 1');

integer i=0;     

For (Apttus__APTS_Agreement__c agmt : ParentAgreements){          

        //agmt.Apttus__Parent_Agreement__c == ParentAgreements.Id    

                i = i+1; 

                System.debug('Method called with:@@@@@@@' + agreementids +'Step 2');

    agmt.Total_Child_Agreement__c = i;
    AgreementsTobeUpdated.add(agmt);
}        
 if(!AgreementsTobeUpdated.isEmpty())
 {
     update AgreementsTobeUpdated;
 }

} }

2
  • Hi @sfdev i will prefer to use below apps to reduce my development time: appexchange.salesforce.com/…
    – C0DEPirate
    Oct 23, 2015 at 7:16
  • Thanks Nick. I installed but the helper is not going to work in my case.
    – sfdev09
    Oct 23, 2015 at 14:39
0

What you could do is use Andrew Fawcett's Declarative Lookup Rollup Summaries, a tool providing the Rollup Summary functionality (usually available only on Master-Detail relationships) on Lookup relationships as well.

After installing the package, you should be able to define a new Lookup Rollup Summary. In the Lookup Relationship details, as a Parent Object specify Apttus__APTS_Agreement__c, Child Object Apttus__APTS_Agreement__c again, and the Relationship Field between them (based on information from your trigger) Apttus__Parent_Agreement__c. In the Rollup Details section, you should choose Count as the Aggregate Operation, Id as the Field to Aggregate, and finally the Total_Child_Agreement__c as the Aggregate Result Field, ie. as the field where you would like to write the aggregate result values.

Your setup should look something like this: Lookup Rollup Summary setup for Child record count

After successful setup, you should deploy the trigger auto generated by the DLRS tool, using Manage Child Trigger -> Deploy buttons. After the trigger has been deployed, you should mark your DLRS as Active, save again and that should be it.

For any newly created/edited child agreements, the aggregate count will be properly summarized up to the parent. In order to update the agreements created before the DLRS was set-up, click the Calculate button, which will start a batch job updating the values on all of the records.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .