I've created an apex class that simply generates random data. we have created a sort of "checklist", similar to the todomvc project, which has checklist items and checklist assignees (contacts that are assigned to those todos). This class will generate both todos and randomly assign a list of contacts to the various tasks.

My question is, why am I getting hit by SOQL limits? generating 15 tasks puts the SOQL count up to 94/100. It doesn't seem to matter how many users I assign, so that isn't really upping the count. There should only be 5 SOQL hits (there is one not shown here in grabbing csv data from a static resource). All of the hits come when I upsert(tasks). I couldn't find anything about upserts affecting soql limits. Do they?

Can you help me figure out where the SOQL hits are coming from?

Thank you!

public class dataGen {

    private static list<map<string,string>> appData {get;set;}

    public dataGen(){
        //get app data
        appData = getMockData();

    public void generateTasks(string oppId, integer numTasks){

        //get users
        list<contact> users = new list<contact>();
        Contact user1 = new Contact();
        Contact user2 = new Contact();
        Contact user3 = new Contact();

        user1 = [SELECT id FROM Contact where name like 'user1'];
        user2 = [SELECT id FROM Contact where name like 'user2'];
        user3 = [SELECT id FROM Contact where name like 'user3'];


        //create upload tasks
        list<task__c> tasks = createTasks(numTasks, oppId);

        }catch(DmlException e){
        list<Task_Contact__c> c = assignUsers(users, tasks);
        }catch(DmlException e){
        system.debug('tasks: '+tasks);
        system.debug('task_contacts: '+c);

    private static list<task__c> createTasks(integer numTasks, string oppId){
        list<task__c> tasks = new list<task__c>();

        for(integer i=0; i<numTasks; i++){
            integer rndIdx = Integer.valueOf(math.floor(math.random()*appData.size()));
            map<string,string> mapRecord = appData.get(rndIdx);
            task__c task = new task__c();

            task.comments__c          = mapRecord.get('comments');  //text
            task.Opportunity__c       = oppId;                      //lookup(id)
            task.Subject__c           = mapRecord.get('subject');   //text


        return tasks;

    private static list<Task_Contact__c> assignUsers(list<contact> users, list<task__c> tasks){
        list<Task_Contact__c> res = new list<Task_Contact__c>();
        integer numUsers = users.size();

        for(task__C t:tasks){
            integer howManyAssignees = Integer.valueOf(math.rint(math.random()*numUsers));
            list<contact> assignees = users.clone();
            list<contact> assigned = new list<contact>();
            for(integer i = 0; i<howManyAssignees; i++){
                integer rndIdx = Integer.valueOf(math.floor(math.random()*assignees.size()));
                contact chosenAssignee = assignees.get(rndIdx);
            for(contact user:assigned){
                Task_Contact__c tc = new Task_Contact__c();
                tc.Task__c = t.Id;
                tc.Contact__c = user.Id;
        return res;

    //gets csv data from static resource mockAppdata
    private static list<map<string, string>> getMockData(){
        //... gets mock data from static csv

  • 2
    quick suggestion....adding Limits.getQueries() debug statement at different parts of the code might help others to get it more quickly.
    – sam_s
    Jan 25, 2016 at 21:59
  • 2
    You would need to monitor Task__c and Task_Contact__c triggers code to figure out. As @sam_s told, use Limits.getQueries() in different parts of the code like before trigger, after trigger etc.
    – Avinash
    Jan 25, 2016 at 22:10
  • This is very good advice. I believe it actually is one of the triggers that has not been bulkified. Thank you both, I was unaware of limits.getQueries()
    – ZAR
    Jan 25, 2016 at 23:18
  • System.debug() to see how many times generateTasks is getting called
    – EricSSH
    Jan 25, 2016 at 23:21

1 Answer 1


Set "PROFILING" to "FINEST" in your TraceFlag (Developer Console > Debug > Change Log Levels...), and it'll tell you where all those calls are coming from. Open the Developer Console, click on Debug, then Change Log Levels, then Click Add/Change on the General Trace Settings for You section, then create a new debug level with all values set to FINEST, then click the row to activate it, click Done, then change the expiration time to the future (it'll turn green), then click Done again.

In your logs, you'll see a CUMULATIVE_PROFILING_BEGIN area:

16:40:35.003 (1003929731)|CUMULATIVE_PROFILING_BEGIN
16:40:35.003 (1003929731)|CUMULATIVE_PROFILING|SOQL operations|
Class.Triggers_Test.test: line 6, column 1: [SELECT Id FROM RecordType WHERE SObjectType = 'Account' AND DeveloperName = 'Project']: executed 1 time in 12 ms
// More queries would be listed here

16:40:35.003 (1003929731)|CUMULATIVE_PROFILING|No profiling information for SOSL operations
// Contains SOSL, if any

16:40:35.003 (1003929731)|CUMULATIVE_PROFILING|DML operations|
Class.Triggers_Test.test: line 7, column 1: Insert: Account: executed 1 time in 146 ms
// More DML operations here...

16:40:35.003 (1003929731)|CUMULATIVE_PROFILING|method invocations|
External entry point: static testMethod void test2(): executed 1 time in 204 ms
// More function calls here; you can use this info to optimize your code

16:40:35.003 (1003929731)|CUMULATIVE_PROFILING_END

You must log in to answer this question.

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