Deprecated: Optional parameter $list declared before required parameter $is_script is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 21

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 45

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 104

Deprecated: Optional parameter $expire declared before required parameter $path is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_functions.php on line 54

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/megamenu.php on line 155

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/mobilemenu.php on line 147

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 583

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 606

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":856,"date":"2017-07-27T06:15:45","date_gmt":"2017-07-27T06:15:45","guid":{"rendered":"http:\/\/www.salesforcenextgen.com\/?p=856"},"modified":"2020-12-28T19:45:18","modified_gmt":"2020-12-28T19:45:18","slug":"playing-sobject-collections","status":"publish","type":"post","link":"https:\/\/salesforcenextgen.com\/playing-sobject-collections\/","title":{"rendered":"Playing with sObject Collections"},"content":{"rendered":"

Playing with sObject Collections<\/strong><\/p>\n

I cannot imagine a single day of apex developer where he\/she does not use sObject collection in the development. They are the core part of Apex coding when it comes to writing efficient code. sObject collections help in minimizing the probability to hit the governor limit.<\/p>\n

First, in the collections come List \u2013 or list of sObject <\/strong><\/p>\n

A list can be of any data type but in this post, we are only dealing with sObject or custom objects. Lists are used when we want to process data in bulk. As we all the transaction limits of Apex popularly known as Governor Limit.<\/p>\n

List can be used with DML Operations, they can store the data returned by a SOQL Query. A list can be created by using the below code :<\/p>\n

List<Account> accountList = new List<Account>();<\/p>\n

List<abc__c> custObjList__c = new List< abc__c >();<\/p>\n

List can also be populated using a SOQL query while declaring it as below:<\/p>\n

List<Account> accountList = [select Id, name from account limit 10];<\/p>\n

Elements or new record can be added to a created list and once a record is added to the list, it gets indexed.<\/p>\n

List<Account> accountList = new List<Account>();<\/p>\n

Account A1 = new Account(Name = \u201cTest Account\u201d);<\/p>\n

accountList.add(A1);<\/p>\n

Account A2 = accountList.get(0);<\/p>\n

Records can be processed in bulk if we pass the list variable to the DML Operation as below:<\/p>\n

Insert accountList;<\/p>\n

Sorting the List of sObject<\/strong><\/p>\n

To sort a list of sObject we can make use of a predefined method list.sort.<\/p>\n

Default order of sort is as below:-<\/p>\n

    \n
  1. First in the order of label<\/li>\n
  2. Then on name field if applicable<\/li>\n
  3. Then standard field in alphabetical order except id and name field<\/li>\n
  4. Lastly on custom field in alphabetical order<\/li>\n<\/ol>\n

    For instance, assume two records have a similar name and indistinguishable standard fields, and there are two custom fields, FieldA and FieldB, the value of FieldA is utilized first to sort.<\/p>\n

    For custom sort order kindly have a look at the below link :<\/p>\n

    Custom Sort Order<\/a><\/p>\n

    Next Comes Sets in the Collection<\/strong><\/p>\n

    Sets only allow unique entity in its confinement, elements added in sets do not get indexed. Set have method like contains or contains all. It is used to determine the uniqueness of sObject.<\/p>\n

    Ex: Set<Account> accountSet =\u00a0new\u00a0Set<Account>{a1, a2};<\/p>\n

    Finally Map of sObject<\/strong><\/p>\n

    Map is the collection of key value pair, and data can be retrieved with the help of the key. It can hold sObject in both key as well as value.<\/p>\n

    Similar to List map can also be populated using SOQL<\/p>\n

    Ex: Map<ID, Account> mapOfAccount =\u00a0new\u00a0Map<ID, Account>([SELECT\u00a0Id, \u00a0fieldA__c\u00a0FROM\u00a0Account])<\/p>\n

    Use case of map can be assumed as the in memory join of tables or relation of two tables.<\/p>\n

    Next:\u00a0<\/span>Custom settings<\/span><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

    Playing with sObject Collections I cannot imagine a single day of apex developer where he\/she does not use sObject collection in the development. They are the core part of Apex coding when it comes to writing efficient code. sObject collections help in minimizing the probability to hit the governor limit. First, in the collections come …
    Continue reading Playing with sObject Collections<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72],"tags":[40,61,74,45,75,38,42,55,51,52],"_links":{"self":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/856"}],"collection":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/comments?post=856"}],"version-history":[{"count":3,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/856\/revisions"}],"predecessor-version":[{"id":2212,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/856\/revisions\/2212"}],"wp:attachment":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/media?parent=856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/categories?post=856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/tags?post=856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}