Tag Archives: apex

Apex Tip: SQOL Error: only aggregate expressions use field aliasing

For those who get that error like me.

Actually, That means that you are missing a “comma” in your SOQL query.

Usually between fields.

I know, Crappy SQOL parser message, but wake up and keep coding! :)

 

About Martin Borthiry

I’m a fully trained and qualified software engineer with more than 15 years of varied experience, specialized in frontend performance, web developing , cloud computing, salesforce, as well as CRM functionalties and Social Networks. Specialties: Salesforce.com Platform & Web Performance Optimization. Salesforce.com Developer and Administrator Certified. Senior Frontend, Javascript and HTML5. Heavy Ajax WebApps Mobile WebApps – Phonegap Webapps Developer (Php/Django/Python) Team Work & Managment

Apex Tip: Simplest way to detect Salesforce 1

If you are working to adapt or create a Visualforce page for Salesforce 1, you would know that you need to detect what type of client application (mobile or desktop) is doing the request.

You would need that in order to avoid certain apex:tags that are not compatible with Salesforce1, to change the layout and look and feel and code some special lines like replace links by sforce.one.navigateToSObject(objectId);

After some research I’ve found those nice methods to detect what type of client is requesting the page

Client side (Javascript):

 

Server-side (Apex):

 

About Martin Borthiry

I’m a fully trained and qualified software engineer with more than 15 years of varied experience, specialized in frontend performance, web developing , cloud computing, salesforce, as well as CRM functionalties and Social Networks. Specialties: Salesforce.com Platform & Web Performance Optimization. Salesforce.com Developer and Administrator Certified. Senior Frontend, Javascript and HTML5. Heavy Ajax WebApps Mobile WebApps – Phonegap Webapps Developer (Php/Django/Python) Team Work & Managment

Apex Tip: get relation field value on Dynamic SOQL

I’m used to work on dynamic Apex/SQOL. I think that is always a better use this good feature of Salesforce during the development step in order to reuse the code to many SObjects as possible.

You have to understand in deep the SObject class in Apex to maximise this technique. Once you work with SObject instead of the Object (Account for instance). You are able to reuse your method/code.

The basic is quite simple, you have an SObject and you can write a field value using o.put(‘fieldName’,’value’), and read a field value using o.get(‘fieldName’).

So, you can use Database.query() method in order to get a SObject list and apply your dynamic method.

However, I’ve faced a common issue when I tried to access to a field value which came from relation field.

For example if you try to get the values from this query:

You would get this error:

My solution was to implement a recursive helper method:

 

Happy code ;)

About Martin Borthiry

I’m a fully trained and qualified software engineer with more than 15 years of varied experience, specialized in frontend performance, web developing , cloud computing, salesforce, as well as CRM functionalties and Social Networks. Specialties: Salesforce.com Platform & Web Performance Optimization. Salesforce.com Developer and Administrator Certified. Senior Frontend, Javascript and HTML5. Heavy Ajax WebApps Mobile WebApps – Phonegap Webapps Developer (Php/Django/Python) Team Work & Managment

Apex Tip: How to keep values during Batch iterations and get the total amount of records processed on final

As you know the Apex Batch structure has 3 basic method:

  • Start : This is only executed on the first iteration before execute
  • Execute: This method runs for each scope (more info about scope here) you defined
  • Finish : this method runs after the last scope execution

Suppose now that you want to know the total amount of records processed. The natural approach to that is to have a variable and increment on each iteration, something like this:

However,  total will returns 0.  The reason of that is Apex Batch reset attributes on each iteration  :(.

The solution for that is to include the interface Database.Stateful in order to keep the variable status over the whole execution.

Finally, you’ll have something like this:

 

About Martin Borthiry

I’m a fully trained and qualified software engineer with more than 15 years of varied experience, specialized in frontend performance, web developing , cloud computing, salesforce, as well as CRM functionalties and Social Networks. Specialties: Salesforce.com Platform & Web Performance Optimization. Salesforce.com Developer and Administrator Certified. Senior Frontend, Javascript and HTML5. Heavy Ajax WebApps Mobile WebApps – Phonegap Webapps Developer (Php/Django/Python) Team Work & Managment

Apex Tip: How to get a RecordType Id by Name without SOQL

The usual way to get a recordTypeId in Apex is to make a query like this:

Instead of that try this line:

Just replace Account and ‘Person Account’ by your Object and Record Type Name
UPDATE:
I’ve been notice that “Person Account” is the label description and not the developer name of the Record Type. Having said that if you work in a multi-lang org, You’ll need to make a Query.
The most efficient way to make that kind of query is to cache the result in a static variable. Like this:

 

Happy coding

About Martin Borthiry

I’m a fully trained and qualified software engineer with more than 15 years of varied experience, specialized in frontend performance, web developing , cloud computing, salesforce, as well as CRM functionalties and Social Networks. Specialties: Salesforce.com Platform & Web Performance Optimization. Salesforce.com Developer and Administrator Certified. Senior Frontend, Javascript and HTML5. Heavy Ajax WebApps Mobile WebApps – Phonegap Webapps Developer (Php/Django/Python) Team Work & Managment

Apex Tip: Create a Knowledge Base Article from Apex

Backgound

If you worked with Salesforce Knowledge Base Articles in Apex, I would know that so many objects and restrictions that make that feature really difficult to rich a deep customization. 

As you can read in the official Doc you have to handle many tables related to Articles:
  • Article Type__DataCategorySelection Object A data category selection represents a data category that classifies an article.
  • Article Type__ka Object Gives access to an article from a specific article type independent of its version. This object is read–only and can’t be used in a SOQL clause or in a WITH DATA CATEGORY DataCategorySpec SOSL clause. For more information, see KnowledgeArticle.
  • Article Type__kav Object Gives access to all articles from a specific article type depending on their version. This object gives access to the fields available in KnowledgeArticleVersion. For more information, see KnowledgeArticleVersion.
  • Article Type__Feed Object Represents a single feed item in the feed displayed on the detail page for an article.
  • Article Type__ViewStat Object Provides statistics on the number of views for an article from a specific article type. For more information, see KnowledgeArticleViewStat.
  • Article Type__VoteStat Object Provides the weighted rating for an article from a specific article type on a scale of 1 to 5. For more information, see KnowledgeArticleVoteStat.
  • CaseArticle Object Represents the association between a Case and a KnowledgeArticle.
  • FeedComment Object Represents a comment added to a feed by a user.
  • FeedItem Object FeedItem represents an entry in the feed, such as changes in a record feed, including text posts, link posts, and content posts.
  • KnowledgeArticle Object Gives access to an article independent of its version. This object is read–only and can’t be used in a SOQL clause or in a WITH DATA CATEGORY DataCategorySpec SOSL clause.
  • KnowledgeArticleVersion Object Provides a global view of standard article fields across all article types depending on their version.
  • KnowledgeArticleViewStat Object Provides statistics on the number of views for the specified article across all article types.
  • KnowledgeArticleVoteStat Object Provides the weighted rating for the specified article on a scale of 1 to 5 across all article types.
  • NewsFeed Object Represents a single feed item on a user’s home tab. A Chatter feed shows recent changes to records that the user is following.
  • UserProfileFeed Object Represents a user profile feed, which tracks all actions by a user on records that can be tracked in a feed. This feed is displayed on the user profile page.
This week I had to create a REST web service to expose some FAQ articles to an external web site. That is not a big deal because you only have to query the ArticleType__kav object. The only tricky part there is to include the Language and PublishStatus in the Where clause.
Let me show you an example, if the ArticleType is FAQ, the query should be something like:

Create an Article from Apex.

After create the web service I had to create the Test Class (I really create it before ;)) I faced the problem to create the article from Apex.
After spend some time googling what table should I insert the article and what order I figured out that it is really easy. I only needed insert the article in FAQ__kav and SFDC trigger the insertion of the rest related records.
So, this is the TestHelper I made to create an Article:
More about the publishing service here.
Enjoy :)

About Martin Borthiry

I’m a fully trained and qualified software engineer with more than 15 years of varied experience, specialized in frontend performance, web developing , cloud computing, salesforce, as well as CRM functionalties and Social Networks. Specialties: Salesforce.com Platform & Web Performance Optimization. Salesforce.com Developer and Administrator Certified. Senior Frontend, Javascript and HTML5. Heavy Ajax WebApps Mobile WebApps – Phonegap Webapps Developer (Php/Django/Python) Team Work & Managment

Apex Tip: How to make a DML operation in a trigger After insert/update

Many times I’ve faced requirements to write a field in a trigger after the record is inserted or update. So, naturally I’ve tried something like this:

But there the record is read only and if you try to change a field value this exception will be fired:

execution of AfterInsert caused by: System.FinalException: Record is read-only

The workaround is quite simple. The object contained by the global trigger.new is read-only, but you can create a new object referring the same ID and change the value that you need.

In addition you have to avoid the recursion since you are changing the record the trigger will be fired again. To avoid that you have to use a static flag which says that the trigger has been already executed during the current executing context.

So, the final code should be like this:

Enjoy :)

About Martin Borthiry

I’m a fully trained and qualified software engineer with more than 15 years of varied experience, specialized in frontend performance, web developing , cloud computing, salesforce, as well as CRM functionalties and Social Networks. Specialties: Salesforce.com Platform & Web Performance Optimization. Salesforce.com Developer and Administrator Certified. Senior Frontend, Javascript and HTML5. Heavy Ajax WebApps Mobile WebApps – Phonegap Webapps Developer (Php/Django/Python) Team Work & Managment

Tip: the simplest way to list all objects

Since SFDC contains variable objects (custom and standard) we have many times to list them.

This is a simple snippet to do it.

Tip Apex: get SObject Type from Id

This is a really piece of cake apex snippet. Extremely useful when you are developing generic methods.

About Martin Borthiry

I’m a fully trained and qualified software engineer with more than 15 years of varied experience, specialized in frontend performance, web developing , cloud computing, salesforce, as well as CRM functionalties and Social Networks. Specialties: Salesforce.com Platform & Web Performance Optimization. Salesforce.com Developer and Administrator Certified. Senior Frontend, Javascript and HTML5. Heavy Ajax WebApps Mobile WebApps – Phonegap Webapps Developer (Php/Django/Python) Team Work & Managment

Tip Apex: Select * from X on apex (SOQL)

I’ve faced many times two problems on SOQL queries:

1. They have not * (star) operator and hence it is really tough to list all fields on large objects.
2. If you try to modify a field name or delete or update the type, salesforce won’t allow you if it is referenced on the code. It is really painful on deploys.

As a best practices when I have to retrieve all fields I use this simple function.

About Martin Borthiry

I’m a fully trained and qualified software engineer with more than 15 years of varied experience, specialized in frontend performance, web developing , cloud computing, salesforce, as well as CRM functionalties and Social Networks. Specialties: Salesforce.com Platform & Web Performance Optimization. Salesforce.com Developer and Administrator Certified. Senior Frontend, Javascript and HTML5. Heavy Ajax WebApps Mobile WebApps – Phonegap Webapps Developer (Php/Django/Python) Team Work & Managment