+ Start a Discussion
J DoveJ Dove 

De-bulkify Apex class that triggers Process Builder flow

I'm trying to execute an Apex method through the anonymous execution via Developer Console. The class basically mimics navigating to a V-force page and clicking a button. However, I'm trying to get out of hitting the too many SOQL queries error that frequents me when doing bulk operations with the Process Builder.

I know this isn't a best practice. It's just a temporary solution to get me out of having to click a button 30 times. The irony is that the amount of time it's taken me to solve the issue, I could've clicked the button 60 times. But, the principle, man! 

 My code is thus:
public class ManualScorecardSend { 
    public void sendScorecard() { 
        Rare_Campaign__c [] cResults = [SELECT Id FROM Campaign__c where Status__c = 'Active' AND Program__r.Name IN ('SomeProgram')]; 

        For (Campaign__c c: cResults) { 
            SetupNewScoreCardExtension setupnsc = new SetupNewScoreCardExtension(new ApexPages.StandardController(c)); 
            setupnsc.SendScorecard(); 
        }
    } 
} 

ManualScorecardSend mss = new ManualScorecardSend(); 
mss.sendScorecard();


Every time I execute this code I get the 'too many SOQL' queries error from Process Builder. But if I go to the V-force page for each Id and kick off the process manually, it works fine.

Could it be that even though it's not a bulkified operation the flows are getting kicked off too quickly and thus hitting limits? I thought about adding a sleep method to slow things down a bit. 

Also, I tested this limiting the query to just one result and it worked fine. It's only when I try it with all resuls (~ 60 records total) that I get errors.

Please let me know if you have any questions or need more information.
Himanshu ParasharHimanshu Parashar
Hi J,

Yes too many soql issue is there with your code because your code is not bulkified I guess, can you post your code for SetupNewScoreCardExtension?
It seems like that there you again have a SOQL and as an end result unknowingly that is coming inside for loop at line 7


Thanks,
Himanshu