Bulk data operations with Apex

,

The basic flow of data manipulation operations for Apex is illustrated in the figure below.

Figure 188. Data manipulation flow


Data is retrieved from an object in the Force Platform database with a query statement. The array of records is modified through the action of your Apex code, and then the array is sent back to the object through a data manipulation command.

These actions are performed in bulk on the Force Platform; however, there are limits to how many records can be affected by each stage of this operation. Since records are normally retrieved into a Force Platform collection, the number of records that can be retrieved is limited by the maximum size of a collection, which is 1,000 items. This limit means that queries retrieving records into a collection cannot retrieve more than 1,000 rows.

There is an easy way around this limitation. Instead of retrieving records into a collection, you can use a FOR SOQL loop, which was discussed in the previous chapter. The FOR SOQL loop iterates through the results of a query without holding those results in a declared object, so the 1,000-item limitation is removed.

There are other limits on Force Platform data interactions that are imposed by the platform itself. The Force Platform has limits on the number of records that can be read or written, in order to prevent any application or organization using too many resources and jeopardizing the overall performance of the platform.

The specific governor limits for different actions are detailed in the Apex Language Reference documentation, but there a few limits that you should be conscious of when creating Apex triggers. The first limit is on the number of data interactions in a trigger. You cannot have more than 20 queries or 20 DML statements in one trigger. Because of this limitation, you should not normally use either of these statements within a loop, since a larger number of loop iterations, which can occur naturally, depending on application context, can result in an error.

There is also a limit on both the number of records that can be retrieved in a query and the number of records that can be affected by a DML statement; however, these limits are based on the number of records on which the trigger is operating. The limits are 1,000 records retrieved and 100 records written per record received.

If a trigger is called when only one record is being affected, the trigger can only retrieve 1,000 records or write 100 records. If the same trigger is called when 10 records are being affected, the limits rise to 10,000 records read and 1,000 records written.

Regardless of the number of records passed to the trigger, there is a maximum of 10,000 rows read by the trigger. These limits are not onerous for the large majority of triggers, but being aware of these potential limitations will help you design your triggers to avoid potential runtime issues.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.51.241