Friday, January 27, 2017

Ways to resolve "Collection size xxxx exceeds maximum size of 1000" on VF page

Visualforce pages are not designed to display more than 1000 records in UI. So if you have controller method which returns more than 1000 records and you are displaying the records on UI, then you will receive error "Collection size xxxx exceeds maximum size of 1000"  where xxxx is number of records returned by your controller method.



So now if you have to display more than 1000 records on UI, then you can use below options:

  • Use @ReadOnly annotation on method or on page attribute.

As per Salesforce Documetation:

Normally, queries for a single Visualforce page request may not retrieve more than 50,000 rows. In read-only mode, this limit is relaxed to allow querying up to 1 million rows.

In addition to querying many more rows, the readOnly attribute also increases the maximum number of items in a collection that can be iterated over using components such as <apex:dataTable>, <apex:dataList>, and <apex:repeat>. This limit increased from 1,000 items to 10,000.

  • You can restrict the number of records returned by method and provide pagination option using controller method.

For this you have to write the logic in controller and every time you will send request to server in order to render the page.


You can also jquery datatables and pass the JSON data to it to create table for you and you will get pagination, sorting or filtering of records on client side itself which will be very quick.

So we will pass more than 1000 records as a JSON to VF page and by using jQuery, we can build table.

Below is sample apex class and VF page code:


VF page Output (Displaying more than 50000 records)



If you have to dispaly hyperlink on account name, then modify the account name value before adding it to fieldvalues list at line number 19 in apex class as shown below:
sb.name= '<a href=\'/' + sb.id + '\'  target=\'_blank\'>' + sb.name+ '</a>';



This will display hyper link to account record. In my example code, I am adding new records to list so id are not present. If you are using query to add records in account list, then you can use above code snippet to show hyperlink.



Sunday, January 15, 2017

Customizable Visualforce Component to display Hierarchy Relationship between records for any Object (Account Hierarchy, Case Hierarchy etc.)

It is very common use case to display hierarchy relation between records in tabular form (for example account hierarchy, case hierarchy etc.) In order to achieve this, I have created a reusable visualforce component for this purpose which can be customize as per requirement to display hierarchy in table tree grid.



Below are inputs required for component:
  1. Specify the object name for which you want to display hierarchy.
  2. Specify the parent field API name (used for self relationship).
  3. Specify the API names of fields separated by comma which you want to display in table tree grid.
  4. Specify the columns labels separated by comma (make sure sequence is same as that of API fields name )for table tree grid.
  5. Specify the field API name which will display as hyperlink for record detail page.

You can create new VF page and include this component and specify above inputs and component will display the hierarchy of record starting from top most parent. 

Below is visualforce component and apex class code:


For example to generate the account hierarchy, create a VF page with code :
<apex:page controller="hierarchyComponentController">
<c:hierarchyComponent sObjectAPIName="Account" sParentfieldAPIName="ParentId" ColumnsToDispaly="Name, Type, Industry" RecordLinkfieldAPIName="Name" ColumnsLabels="Account Name, Type, Industry"/>
</apex:page>

Note: Don't forget to append record Id on visualforce page url for which you want to display hierarchy. Below is sample URL:
https://xxxxxxxxx.visual.force.com/apex/accHierarchy?id=0019000001NqoXZ

Hope this will help...

This can be used as base code and can be customize as per your requirements.

Looking forward for everyone's comment and feedback.




Refer Below Links for Salesforce Interview Questions

Saturday, January 7, 2017

Custom Metadata Types and Custom Settings Implementation Tricks

Custom Settings

  • Custom settings helps to create custom data set  and associate the data set to particular user, profile or for all users (org-wide). 
  • There are 2 types of custom setting, List and Hierarchy
  • Same set of data is available to all user if we define List custom setting.
  • In Hierarchy custom setting, you can control data visibility based on logged in user, profile or org-wide. 
  • In Hierarchy custom setting for logged in user, system first check user then profile and then org wide setting in order to return data from hierarchy custom setting.
  • You can control the visibility of custom setting by specifying it as public or protected.
  • If custom setting is marked as protected, the subscriber organization will not be able to access the custom setting. If it is marked as public, then subscriber org can also access it.
  • Once you create custom setting, then you cannot change the type (List to hierarchy or vice versa).
  • Custom setting data is available in application cache which increase performance. 
  • You can access custom setting data using instance methods and can avoid SOQL queries to database.
  • While migrating custom setting to another org, you need to migrate data for custom setting separately.
  • Custom settings do not support relationship fields.
  • Custom setting can be used by formula fields, validation rules, flows, Apex, and the SOAP API.
  • You cannot access List custom setting in Validation rule. Only Hierarchy custom setting can be used.
  • In order to use value from list custom setting in validation rule, create a field in object and populate list custom setting value in custom field through triggers and then refer it in validation rule. Validation rule fires after trigger execution.
  • You can perform CUD (Create, Update, Delete) operation on custom setting in apex.
  • Custom settings are not visible in test class without "SeeAllData" annotation.

Custom Metadata Types
  • Custom metadata are like custom setting but records in custom metadata type considered as metadata
  • You can migrate data present in custom metadata type to different org easily. 
  • This helps to create app configuration data and include it in package. 
  • Custom metadata types support Metadata Relationship. It is still in Beta.
  • Metadata Relationship provides the ability to add relationships from your custom metadata to other things in your app, such as other custom metadata, custom or standard objects and fields, and static resources.
  • You can't perform CUD (Create, Update, Delete) operation on custom metadata type in apex.
  • You can also control the visibility of custom metadata type while adding it in package.
  • Custom metadata type are visible in test class without "SeeAllData" annotation.
Custom metadata types are still in development phase and many new features will come in coming future.


Refer Below Links for Salesforce Interview Questions