美文网首页Salesforce 开发笔记
System.LimitException:Too many S

System.LimitException:Too many S

作者: 古月的小七 | 来源:发表于2020-07-23 16:17 被阅读0次

    Test Class 在运行过程中,有时会碰到 Query 101的 Error, 这篇文章会提醒我们在代码过程中需要规避的问题以及碰到这种问题的时候的解决方案。
    Here are some tips that will improve your code and boost your stock on the Salesforce job market!

    1. Avoid using Seealldata=true if possible. It's better to just create these records from scratch in your Test Class. This is not only a good Salesforce practice, but also a good coding practice in general. Plus, you'll save on SOQL queries.(尽量避免使用 seeAlldata = ture, 这个基本属于严禁使用的)

    2. Combine your SOQL queries - for example, you could have done this instead:
      List<RecordType> recordTypes = [SELECT Id FROM RecordType WHERE Name IN ('Commercial Deal Sheet', 'Residential Rental Deal Sheet', 'Residential Sale Deal Sheet', 'Referral Deal Sheet')];
      (对你的Query 语句来说,最好拼接你的SOQL,不要每条单独查询)

    3. Combine your DML statements by using update on a list instead of individual records. All SOQL queries from your triggers will fire with each update statement, which is causing you to hit your limit. It looks like in this case you might need to use each update call, so check out tip #4(在准备测试数据的过程中,要尽量使用LIst来进行DML操作,不要对单独对每条数据进行操作)

    1. Separate your test class into multiple test classes. This is the easiest way to get by governor limits in test classes - just split your test class in half and make it into two separate test classes. You'll get double the governor limits!(如果还是在单个文件中达到了 salesforce Limitation,可以把一个测试类分成多个)

    5. Use Test.startTest() and Test.stopTest(). You can do all your queries in the beginning of your class, then do all your updates inside Test.startTest() and Test.stopTest(). You get a fresh set of governor limits inside these too!(Test.startTest() 和 Test.stopTest() 方法之间的LImit是单独计算的,所以可以在测试方法中加入这两个方法来规避Limitation)

    1. 还有一个方法可以规避101 Error, 就是在setup 方法里面加入 Test.startTest() 和 Test.stopTest() 方法,如下:
            /*
            * Avoid SOQL 101 error
            */
            Test.startTest();
            System.runAs(loggedInUser){
                OpportunityLineItem opl = TestDataFactory.createOpportunityLineItem(oppty);
                opl.Product_Network_Node__c = '1974711';
                opl.Fee_Arrangement__c = '12667269';
                insert opl;
                }
            Test.stopTest();
    

    "Any code that executes after the call to startTest and before stopTest is assigned a new set of governor limits."
    http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_test.htm

    相关文章

      网友评论

        本文标题:System.LimitException:Too many S

        本文链接:https://www.haomeiwen.com/subject/eyrvwhtx.html