Tuesday, August 14, 2018

Governor Limits Example-Too Many DML Statements :151

Governor Limits Example : Too Many DML Statements :151
(Sunil Sharma)


Example:Code Hitting Governor Limits

System.LimitException :Too Many DML Statements :151



/*Example Code Hitting Governor Limits*/

/*As per the Governor Limits The number of DML operations allowed per a transaction is 150. So, the code should be written in such a way that this limit should not be reached. Below example shows the code with 150+ DML operations*/


public class DMLGovernorLimits {
    
    Public void limittest() {
        Contact c = new Contact();
        c.FirstName = 'TestLimit';
        c.LastName = 'Testing';
        insert c;
        for(integer i= 1; i<150; i++){
            Contact ct = new Contact();
            ct.FirstName = 'TestLimit'+i;
            ct.LastName = 'Testing'+i;
            insert ct;
        }
        Contact ct1 = new Contact();
        ct1.FirstName = 'TestLimitExceed';
        ct1.LastName = 'TestingLimits';
        insert ct1;

    }

}

Now,  execute the class by navigating to Debug ->’Open Execute Anonymous Window’

Enter the following code and click on ‘Execute’ button

DMLGovernorLimits d = new DMLGovernorLimits();
d.limittest();

Here we are writing ‘DML statement’ (insert) inside the ‘for loop’. So, it’s executing 150 times and Total number of times insert will be executed is 152. We are getting the LimitException : Too Many DML Statements :151 due to this.

This can be avoided by using ‘List’ Collection in the above class. Similar operations of an object can be added to a list and perform DML operation on the list. In this way, the above code can be executed without hitting ‘Governors Limit’. Below is the optimized code which doesn’t hit governor limits.

/*Example Code which doesn’t hit governor limits*/

public class DMLGovernorLimit {
    
    Public void limittest() {
        List<Contact> conList = new List<Contact>();
        Contact c = new Contact();
        c.FirstName = 'Sunil';
        c.LastName = 'Sharma';
        conList.add(c);
        for(integer i= 1; i<150; i++){
            Contact ct = new Contact();
            ct.FirstName = 'TestLimit'+i;
            ct.LastName = 'Testing'+i;
            conList.add(ct);
        }
        Contact ct1 = new Contact();
        ct1.FirstName = 'TestLimitNotExceed';
        ct1.LastName = 'TestingLimits';
        conList.add(ct1);
        insert conList;

    }

}



7 comments:

  1. It's so nice article thank you for sharing a valuable content. google cloud online course

    ReplyDelete
    Replies
    1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.

      Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.

      Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

      The Nodejs Projects Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. This concept is a good way to enhance the knowledge.thanks for sharing. please keep it up salesforce Online Training Bangalore

    ReplyDelete
  3. It's so nice article thank you for sharing a valuable content. google cloud online course

    ReplyDelete
  4. Hire Today! The expert Salesforce App Development Company in Delhi, offers BOFC App, Lightning Lead Conversion W/O Opportunity, Code Comparator.

    ReplyDelete
  5. While there are several techniques to please your customers, the best way is to improve your direct relationship with them. Obviously, direct communication is more effective than indirect techniques. However, this is where several businesses tend to fail. Salesforce training in Chennai

    ReplyDelete

Governor Limits Example-Too Many DML Statements :151

Governor Limits Example : Too Many DML Statements :151 (Sunil Sharma) Example:Code Hitting Governor Limits System.LimitExceptio...