Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two rules using same set of RelationshipCriteria fields don't create scheduled items #100

Closed
kushkella opened this issue Dec 12, 2014 · 5 comments
Labels

Comments

@kushkella
Copy link

Hey,

I've trying to setup two rollups from Contact to Account. They both are scheduled, and have the same set of RelationshipCriteriaFields. The only difference is in criteria. Following is the screenshot of two Rollups:

screen shot 2014-12-12 at 11 38 50 pm
screen shot 2014-12-12 at 11 39 04 pm

As seen from the diagrams the two rollups are identical except for the fact one rolls up driver and other fleet_user to two different fields on the Account. The problem is that only one Rollup works. I think scheduled items should be created for both Rollups whenever any Relationship Criteria fields change. And since they have same set of fields, a scheduled item for each Rollup should be created when conditions match. But as far as I've tested only scheduled items for only one rollup are created. I also noticed whichever Rollup was created first had the scheduled items. To verify this, I re-created Rollups with different order, and noticed whichever Rollup was created first had scheduled items.

I also looked at the hadleTrigger function in the RollupService class but everything seemed to be fine. As far as I think the code should be creating two scheduled items, but the debug logs show that only one scheduled item was upserted.

It would be great if you could help me figure this out. Thanks!

PS. I apologize for not being responsive on another issue I created earlier.

@kushkella
Copy link
Author

I've finally being able to pinpoint error in the code. I'm not sure the implications of fix that I'm going to suggest. But I wanted to help in anyway I can. In the updateMasterRollupsTrigger method in RollupService class, you use a Map to build scheduled items to be upserted later. The key for the map is Id of the masterRecord which does not allow scheduled items with same masterRecord and different lookups to live in the map. A simple fix is to use list instead of a map. Something as follows which works in developer organization for my case:

    private static List<SObject> updateMasterRollupsTrigger(List<LookupRollupSummary__c> lookups, Set<Id> masterRecordIds)
    {
        // Process lookups, 
        //    Realtime are added to a list for later LRE context creation and processing, 
        //    Scheduled result in parent Id's being emitted to scheduled item object for later processing
        List<LookupRollupSummary__c> realtimeLookups = new List<LookupRollupSummary__c>();      
        List<LookupRollupSummaryScheduleItems__c> scheduledItems = new List<LookupRollupSummaryScheduleItems__c>(); 
        for(LookupRollupSummary__c lookup : lookups)
        {
            if(lookup.CalculationMode__c == RollupSummaries.CalculationMode.Realtime.name())
            {
                // Filter realtime looks in order to generate LRE contexts below
                realtimeLookups.add(lookup);
            }
            else if(lookup.CalculationMode__c == RollupSummaries.CalculationMode.Scheduled.name())
            {       
                // For scheduled rollups queue the parent Id record for processing
                for (Id parentId : masterRecordIds)
                {
                    LookupRollupSummaryScheduleItems__c scheduledItem = new LookupRollupSummaryScheduleItems__c();
                    scheduledItem.Name = parentId;
                    scheduledItem.LookupRollupSummary__c = lookup.Id;
                    scheduledItem.ParentId__c = parentId;
                    scheduledItem.QualifiedParentID__c = parentId + '#' + lookup.Id; 
                    scheduledItems.add(scheduledItem);
                }                   
            }
        }

        // Add parent Id's to schedule items object
        upsert scheduledItems QualifiedParentID__c;

        // Process each context (parent child relationship) and its associated rollups
        Map<Id, SObject> masterRecords = new Map<Id, SObject>();        
        for(LREngine.Context ctx : createLREngineContexts(realtimeLookups).values())
        {
            // Produce a set of master Id's applicable to this context (parent only)            
            Set<Id> ctxMasterIds = new Set<Id>();
            for(Id masterId : masterRecordIds)
                if(masterId.getSObjectType() == ctx.master)
                    ctxMasterIds.add(masterId);
            // Execute the rollup and process the resulting updated master records
            for(SObject masterRecord : LREngine.rollup(ctx, ctxMasterIds)) 
            {
                // Skip master records without Id's (LREngine can return these where there was 
                //  no related master records to children, for examlpe where a relationship is optional)
                if(masterRecord.Id==null)
                    break;
                // Merge this master record result into a previous one from another rollup ctx?
                SObject existingRecord = masterRecords.get(masterRecord.Id);
                if(existingRecord==null)
                    masterRecords.put(masterRecord.Id, masterRecord);
                else
                    for(LREngine.RollupSummaryField fieldToRoll : ctx.fieldsToRoll)
                        existingRecord.put(fieldToRoll.master.getSObjectField(), 
                            masterRecord.get(fieldToRoll.master.getSObjectField()));
            }           
        }

        // Return distinct set of master records will all rollups from all contexts present
        return masterRecords.values();                  
    }

Also in the scheduled items related list, the column Lookup Rollup Summary Schedule Item Id lists the id of the masterRecord. And same is the case in the detailed view of the scheduled item. Shouldn't this list Id of the scheduled item itself?

@kushkella
Copy link
Author

It would be great if you could release a fix soon. I can't find a clean way to install this without the managed package. I have seen a related issue which mentions a problem with permission sets. I had to remove the those permission sets to install the package. Thanks!

@afawcett afawcett added the bug label Dec 15, 2014
@afawcett
Copy link
Collaborator

Thanks for this, i've seen some other reports of scheduled mode not working consistently, and i think you've nailed the reason here! Well done!

Your code works perfectly, except that i have found that it does not clean up the schedule items reliably elsewhere in the code given this use case. I have written a unit test to reproduce and merged your changes and fixed the other related bug i found.

I've got a couple of other minor fixes i want to get into the next release, if i get enough free time during the week i'll progress, otherwise will likely be another release at the weekend.

@kushkella
Copy link
Author

Thanks! I look forward to the next release.

@afawcett
Copy link
Collaborator

Fixed in v1.15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants