1

I have a test class that is failing due to too many SOQL queries. I'm assuming the fix is quite easy, but am struggling with how to bulkify the query. Can someone please assist? Full test class is below.

@isTest
private class clsUpdateAccountFieldsFromUserTable {
static testMethod void  verifyLUXIDSetFromOwner(){

         // Perform our data preparation.

         Profile p = [select id from profile where name='Standard User'];
         User u = new User(alias = 'standt', email='[email protected]',
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id,
            timezonesidkey='America/New_York', username='[email protected]',
            LUX_ID__c ='01234567');

         List<Account> accounts = new List<Account>{};

         for(Integer i = 0; i < 200; i++){
         Account a = new Account(Name = 'Test Account ' + i);
         accounts.add(a);
         }

    // Start the test, this changes governor limit context to
    // that of trigger rather than test.
    test.startTest();

    // Insert the Account records that cause the trigger to execute.
    System.runAs(u){
    insert accounts;
    }

    // Stop the test, this changes limit context back to test from trigger.
    test.stopTest();

    // Query the database for the newly inserted records.
    List<Account> insertedAccounts = [SELECT Name, Owner_LUX_ID__c
    FROM Account
    WHERE Id IN :accounts];

    // Assert that the Description fields contains the proper value now.
    for(Account a : insertedAccounts){
    System.assertEquals(
    '01234567',
    a.Owner_LUX_ID__c);
    }
  }
}
3
  • 1
    You'll need to post the code for your trigger. Nothing really shows from your test class. You may want to reduce the number of accounts to say 5 and see if that doesn't solve your problem. 200 is the max your trigger should be able to handle anyway. So, you first want to find out if it makes it through your test class without failing when just inserting a few records.
    – crmprogdev
    Mar 4, 2015 at 17:50
  • Hello! The issue is my flow that was built with the process builder. And in looking at Ideas just now, it appears to be a large problem for other users as well. I will try changing from 200 to 5 though, and test that way. Thank you for your response!
    – mooreannem
    Mar 4, 2015 at 17:57
  • You should post that as an answer then.
    – Adrian Larson
    Mar 4, 2015 at 18:13

3 Answers 3

1

The issue is my flow that was built with the process builder. And in looking at Ideas just now, it appears to be a large problem for other users as well. salesforce needs to allow the flow in process builder to be "bulkified."

Idea: https://success.salesforce.com/ideaView?id=08730000000DhBlAAK

1
  • Update: Flows in Process Builder are now bulkified. Jan 25, 2019 at 16:19
1

This error occurs when we use SOQL queries in test class of Salesforce. just put LIMIT 1 and try. hope so the error will go off. It had worked for me many a times.

ex: Select Id, Name from User LIMIT 1 Isactive = True;

0

Below code divide in 4 partition and create 4 test method.

     for(Integer i = 0; i < 200; i++){
        Account a = new Account(Name = 'Test Account ' + i);
        accounts.add(a);
     }

Ex:

     for(Integer i = 0; i < 50; i++){
        Account a = new Account(Name = 'Test Account ' + i);
        accounts.add(a);
     }

You must log in to answer this question.

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