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

Cannot disable DLRS in Apex #889

Closed
delan-flywheel opened this issue Dec 12, 2019 · 13 comments
Closed

Cannot disable DLRS in Apex #889

delan-flywheel opened this issue Dec 12, 2019 · 13 comments

Comments

@delan-flywheel
Copy link

Is there a way to disable (set dlrs__Active__c to false) our rollups for the duration of a specific trigger and then re-enable them at the end?

We have built a process that makes small edits to a large number of records, and we have run into CPU timeout issues partially because there are a lot of rollup fields involved.

We deduced that rollup fields were a contributing factor. This was confirmed when we manually disabled the rollups and tried to run the process. It worked, and then we re-enabled the rollups and clicked 'Calculate'.

We would like to automate this manual disabling, enabling, and one-time calculation of the lookups. Our hunch was to add code disabling the lookup fields (setting dlrs_Active__c to false) to start the process, and then at the end of the process reactivating them and then manually calculating the rollups once, now that the thousands of records have been changed.

This hasn't worked because DML operations are not permitted on dlrs__LookupRollupSummary2__mdt, where our lookup information seems to be. We tried to work with the dlrs.CustomMetadataService methods to see if there was promise there, but we have run into a problem of 'Type is not visible: dlrs.CustomMetadataService'.

Any insight on how we should approach this problem would be greatly appreciated. Is there a method we are missing?

Thanks,

@afawcett
Copy link
Collaborator

This is a good enhancement to the tools public API. Meanwhile you can customize the DLRS trigger that was generate by the tool and add an "if" statement referencing a static member variable your code controls to condition the call in the generated trigger to the RollupService.

@delan-flywheel
Copy link
Author

Good to hear!

The solution we engineered was to use Custom Settings. We created an org-wide checkbox called DLRS_Switch__c with a default value of false. We also added a flag to the child trigger to check that this value was false before executing, like so:

trigger dlrs_Custom_ObjectTrigger on Custom_Object__c
    (before delete, before insert, before update, after delete, after insert, after undelete, after update)
{
    DLRS_Switch__c triggerSwitch = DLRS_Switch__c.getInstance();
    
    // if the switch is off, update records
    if(triggerSwitch.Trigger_Switch__c == false) {
        dlrs.RollupService.triggerHandler(Custom_Object__c.SObjectType);
    }
}

Then, at the beginning of our process, we set the value of the checkbox to true. We run the process, then set the switch back to false, and end the process by calculating all lookups using the runJobToCalculate method.

@afawcett
Copy link
Collaborator

afawcett commented Jan 2, 2020

@delan-flywheel thanks so much for sharing meanwhile!

@renatoliveira
Copy link
Contributor

Kevin O'Hara's sfdc trigger framework has a bypass API, but for triggers. I imagine that it would be very simple to implement a similar functionality.

@afawcett
Copy link
Collaborator

afawcett commented Apr 3, 2020

Thanks @renatoliveira PR merged!

@afawcett
Copy link
Collaborator

afawcett commented Apr 6, 2020

Fixed in v2.12

@afawcett afawcett closed this as completed Apr 6, 2020
@mkdjns
Copy link

mkdjns commented May 5, 2021

I posted this comment on the merged pull request, but on reflection realize that might not have been the best place. So-

Is there documentation on usage for this feature?

@renatoliveira
Copy link
Contributor

I posted this comment on the merged pull request, but on reflection realize that might not have been the best place. So-

Is there documentation on usage for this feature?

If you are talking about the Apex API, you can just use RollupService's static methods:

  1. isBypassed
  2. bypass
  3. clearBypass
  4. clearAllBypasses

They are supposed to be used in the middle of your code, turning on/off rollups dynamically. For example, if you want to update a child record but don't want to update the parent's rollups.

@mkdjns
Copy link

mkdjns commented May 7, 2021

That's the pointer I needed. I also forgot about name spaces, which is another reason I was getting errors.

Thanks!

@stephenpstanley
Copy link

stephenpstanley commented Oct 28, 2022

@mkdjns you've obviously got the bypass handler working, can you give more details how?

I've called dlrs.RollupService.bypass(name) and passed in "names" using the following fields from the custom metadata settings in dlrs__LookupRollupSummary2__mdt

  • Label
  • MasterLabel
  • DeveloperName
  • Id
    but I get exactly the same results as if I pass in the string 'XYZ'

I've also looked in dlrs__LookupRollupSummary__c to see if I can grab the Name field from there, but there don't appear to be any records in that object

All that happens is that it adds the string passed in to a set and a call to dlrs.RollupService.isBypassed() just tells you that the name passed in is in the set. It doesn't tell you if the rollup has actually been bypassed.

You mention forgetting namespaces, is that where I'm failing?

@aheber
Copy link
Contributor

aheber commented Oct 28, 2022

@stephenpstanley, maybe this will help.

You should be able to use the DeveloperName assuming you're data stored as the Custom Metadata records. If it is stored as the custom object then you'll need to provide the UniqueName__c value.

Here is where it is checked,

if (BypassHandler.isBypassed(lookup.UniqueName)) {

and here is where the value is retrieved from the metadata.

@stephenpstanley
Copy link

Thanks - they are definitely stored in the custom metadata setting, but I'm still getting Too Many SOQL 101 errors even with all the rollups bypassed as dlrs and some triggers are doing too many queries in my test class. Dlrs is looking to see which are active and running 50+ queries even with the rollup meta data disabled :-(

@sjurgis
Copy link

sjurgis commented Sep 26, 2024

Yep doesn't work for me either. Compiles and runs, but I see in logs DLRS is still running. Using same names as I use to schedule jobs using Apex.

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

No branches or pull requests

7 participants