• Paweł Woźniak
  • NEWBIE
  • 20 Points
  • Member since 2014
  • Developer
  • Fibratus

  • Chatter
    Feed
  • 0
    Best Answers
  • 2
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 4
    Replies
Hi.

Happy about finally delivering option to get recordId by developer name, see relase info https://releasenotes.docs.salesforce.com/en-us/summer18/release-notes/rn_apex_developer_name.htm I wanted to use it. It works perfectly on Anonymous execution, however if used in apex class it is not possible to save due to method does not exists error.
Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('PersonAccount').getRecordTypeId();
System.debug(devRecordTypeId);
In anonymous execution it returns a value in debug. Using in apex class gives an error:
Method does not exist or incorrect signature: void getRecordTypeInfosByDeveloperName() from the type Schema.DescribeSObjectResult
Do we have SF peoples here? Can you please fix it.
 
I've gone through every step successfully but am banging my head against the wall with step 8. I have more than adequate code coverage but continue to get this error message:
User-added image
I have gone back and rewritten OrderTests twice according to the requirements but can't get past this error. Anyone else had any luck with this?

Here's my code for reference:
@isTest (SeeAllData=false)
private class OrderTests {
    
    static void SetupTestData() {
	    TestDataFactory.InsertTestData(5);
    }
 
    @isTest static void OrderExtension_UnitTest() {
        PageReference pageRef = Page.OrderEdit;
        Test.setCurrentPage(pageRef);
        SetupTestData();
        ApexPages.StandardController stdcontroller = new ApexPages.StandardController(TestDataFactory.orders[0]);        
        OrderExtension ext = new OrderExtension(stdcontroller);        
       	System.assertEquals(Constants.DEFAULT_ROWS, ext.orderItemList.size());
        ext.OnFieldChange();
        ext.SelectFamily();
        ext.Save();
        ext.First();
        ext.Next();
        ext.Previous();
        ext.Last();
        ext.GetHasPrevious();
        ext.GetHasNext();
        ext.GetTotalPages();
        ext.GetPageNumber();
        List<SelectOption> options = ext.GetFamilyOptions();
    }
    
@isTest
public static void OrderUpdate_UnitTest(){
    setupTestData();
    
   
    Test.startTest();
    
    List<Order> orders = TestDataFactory.orders;
    for (Order o : orders){
        o.Status = Constants.ACTIVATED_ORDER_STATUS;
    }
    List<Product2> oldProducts = TestDataFactory.products;
    Set<Id> productIds = new Set<Id>();
    for (Product2 oldProd : oldProducts){
        productIds.add(oldProd.Id);
    }
    oldProducts = [SELECT Id, Quantity_Ordered__c FROM Product2 WHERE ID IN :productIds];
    Map<Id, Integer> quantities = new Map<Id, Integer>();
    for (OrderItem oi : TestDataFactory.orderItems){
        Integer quantity = 0;
        List<PricebookEntry> pricebookentries = TestDataFactory.pbes;
        for (PricebookEntry pbe : pricebookentries){
            if (oi.PricebookEntryId == pbe.Id){                
                if (quantities.containsKey(pbe.Product2Id)){
                    quantity = quantities.get(pbe.Product2Id);
                }
                quantity += (Integer)oi.Quantity;
                quantities.put(pbe.Product2Id, quantity);
                break;
            }
        }
    }
   
    update orders;
    Map<Id, Product2> currentProducts = new Map<Id, Product2>([Select Id, Quantity_Ordered__c FROM Product2 WHERE Id IN :productIds]);
  
    for (Product2 prod : oldProducts){
      
        TestDataFactory.VerifyQuantityOrdered(prod, currentProducts.get(prod.Id), quantities.get(prod.Id));
  }
  Test.stopTest();
}
}

 
Our production deployments using the (ant) force migration tool have been consistently failing recently, after executing some percentage of our tests, with the error:

System.LimitException: Your runAllTests request is using too many DB resources.

All of our tests are @isTest(seeAllData=false) and we recreate all test data from scratch using factory classes at the beginning of each test method.

We contacted Salesforce support, and after a multiple calls, different people, and a lot of time, they told us that in addition to the per-transaction governor limits, there are undocumented limits that are shared amongst all executed code during the course of a runAllTests request:
  • The DML statement limit is 350,000
  • The SOQL statement limit is 450,000
Salesforce suggested that we use the @testSetup annotation to setup data in our tests (we have not been using that annotation), and this would help us in avoiding hitting the above limits. I wrote a small unit test to verify that this would actually help with limits, but the unit test suggests that the DML statements accrued in a @testSetup annotated method do indeed count in each testmethod.
@isTest(seeAllData=false)
public class TestSalesforceTestSetupDMLCount {

    @testSetup static void setup(){
        // Assert that no DMLs have occurred yet
        system.assertEquals(0, Limits.getDMLStatements());
        
        // Perform 4 DMLs
        for(Integer i=0; i<4; i++){
            insert new Account(Name='Test' + i);
        }
        
        // Assert that 4 DMLs have been performed
        system.assertEquals(4, Limits.getDMLStatements());
    }
    
    public static testmethod void testDmlCount1(){
		// THIS ASSERTION FAILS
        system.assertNotEquals(4, Limits.getDMLStatements());
    }
    
    public static testmethod void testDmlCount2(){
        // THIS ASSERTION FAILS
        system.assertNotEquals(4, Limits.getDMLStatements());
    }
}

The 2 testmethods fail because Limits.getDMLStatements() returns 4. The question is, regarding the runAllTests DML statement limit of 350,000, does the above test contribute 4 DML statements or does it contribute 12 DML statements (4 for setup(), 4 for testDmlCount1(), 4 for testDmlCount2())?

This is obviously something that only Salesforce can answer, but I at least want this issue documented somewhere, and will provide updates as we get answers from Salesforce.

Hi. 
I want to replace some specific words in string with anothers using pattern and matcher. Example: I want format nicely SOQL query so:
[ SELECT Id, Name FROM Account WHERE Name =  'John' ]
should be changed to:
[SELECT
    Id, Name
FROM
    Account
WHERE 
    Name = 'John']

The code:
 

String convertInput = '[ SELECT Id, Name FROM Account WHERE Name =  \'John\' ]';
String convertOutput = '';
String regExp = '(SELECT|FROM|WHERE)';

Pattern p = Pattern.compile(regExp);      
Matcher m = p.matcher(convertInput);
    while (m.find() == true) {          
        convertOutput = m.replaceFirst('\n' + m.group(1) + '\n');

    } 
System.debug(convertOutput);

Gives error: System.LimitException: Regex too complicated

Where code:
String convertInput = '[ SELECT Id, Name FROM Account WHERE Name =  \'John\' ]';
String convertOutput = '';
String regExp = '(SELECT|FROM|WHERE)';

Pattern p = Pattern.compile(regExp);      
Matcher m = p.matcher(convertInput);
    while (m.find() == true) {          
        convertOutput = m.replaceAll('\n' + m.group(1) + '\n');
    } 
System.debug(convertOutput);

Gives output:
SELECT
Id, Name
SELECT
Account
SELECT
Name = 'John' ]

So I am confused what is so complicated in replaceFirst?
Do you have any suggestions how to solve that?