Category Archives: Salesforce Corner

Salesforce Apex: Namespace and Field Name in AggregateResult

In Salesforce SOQL, we can use the aggregate function to summarize data in query. When an aggregate function is used, Salesforce will return the result as AggregateResult object and we can retrieve the value by calling AggregateResult.get(‘field name’). For example:

for(AggregateResult ar : [select count(Id), Business_Type__c from Account group by Business_Type__c]){
	system.debug(ar.get('expr0'));		//get the value of count(id)
	system.debug(ar.get('Business_Type__c'));	//get the grouped by field value
}

However, things get a little bit tricky when you have a namespace in the Salesforce instance and you have custom field in the SOQL. The sample query will still work, but you will get an error saying “Invalid aggregate result field” when you are trying to retrive the value of custom field.

To overcome this, you can choose to:

Option 1

Prepend the custom field with a namespace, for instance,

system.debug(ar.get('namespace__Business_Type__c'));	//get the grouped by field value

Option 2

Use alias in the SOQL to represent a custom field in query result.

for(AggregateResult ar : [select count(Id), Business_Type__c businesstype from Account group by Business_Type__c]){
	...
	system.debug(ar.get('businesstype'));	//get the grouped by field value
}

Personally, I would prefer to use Option 2 as I don’t have to hard code the namespace in all the SOQL.

Salesforce: Prevent Duplicate Record By Name

Scenario: An automation to prevent a record with the same name from being created twice.

Problem: Object name is a standard text field and you won’t be able to make it unique.

Solution #1: Salesforce Duplicate Management  Feature. Refer to this blog here for more information.

Solution #2: Validation rule with Vlookup function.
Example: 
Prevent Dup

Please take note that you will need to use $ObjectType in your Vlookup formula. $ObjectType is to tell the formula that it needs to refer to an object in that organisation. Here is how you can select your object from $ObjectType (Only available for custom objects):

SObject
For more details on how to use the Vlookup function, click here.

Salesforce Visualforce Page workaround for apex:pageBlockSection not collapsible within apex:repeat

Visualforce provides us with many tools and components to quickly setup a page with high degree of similarity in style with Salesforce UI look and feel.

One of such is the pageBlockSection, which as the name hint is often used to create a section within the Visualforce Page that we intend to display, ex:

<apex:pageBlockSection title="Test Section">
...
</apex:pageBlockSection>

Much like the way Salesforce own UI pageBlockSection behave, this section can be toggled between a collapsed state where it only display the title of the section, ex:

2015-06-23 12_08_02-salesforce.com - Developer Edition

or unfolded into it’s deployed state where it display the content of the section as well as the title..

2015-06-23 12_07_42-salesforce.com - Developer Edition

These are standard capability of the pageBlockSection itself, but as of the time of this writing a problem exist when we attempt to use pageBlockSection while enclosing it within a repeat component, ex:

<apex:repeat>
	<apex:pageBlockSection title="Test Section">
	...
	</apex:pageBlockSection>
</apex:repeat>

The above code will break the collapsible feature of pageBlockSection, locking it in unfolded state.
A workaround to this problem is to include a dummy pageBlockSection prior to the repeat component, this dummy pageBlockSection can be hidden and it will remain functional, so to fix our example above:

<apex:pageBlockSection title="Title" collapsible="true" rendered="false"/>
<apex:repeat>
	<apex:pageBlockSection title="Test Section">
	...
	</apex:pageBlockSection>
</apex:repeat>

Salesforce Visualforce Page View State

Have you ever encountered the “Maximum view state size limit” error on the Visualforce page that you developed even though you only capture very little information (e.g, like 3 input fields)?

Maximum view state size limit (135KB) exceeded. Actual view state size for this page was xxx.xxKB

The way that the view state works is that it is not only holding the value of the field that you capture on the page, but it also holds the value of any accessible objects/variables that you have declared in the controller/extension. For example, if you have an object list (e.g, List<Account>) and the list is holding a lot of records, you might encounter the view state size limit error even though you are not displaying result in the page.

Salesforce has a good explanation on Visualforce View State that gives you a good start if you are troubleshooting the issue. As a good developer, you should also follow the best practice guide to develop a performance optimized Visualforce page.