April 20, 2012

Accessing Sobject Field Sets in Apex Code !

I recently came across a problem, where I need to query the fields to be shown via FieldSet via Apex Code (No Standard Controller Love). After struggling for a while with Apex docs, I googled for the same and found an idea posted for the same.

So, I thought lets try using dynamic bindings in visual force if they can get the job done for me. Fortunately it worked like a charm in one go. Here is the solution

April 15, 2012

Chatter Apex Test Case Bug in API version 24

I noticed this issue while writing an Apex test case for some chatter related functionality. For sake of this post, I am just posting the simplified test case. Here is the code

public static testmethod void testFeeds() {
    // Create a Test Account
    Account acc = new Account(Name = 'Demo Account');
    insert acc;
    // Create FeedItem using this Account as a Parent Record
    FeedItem fp = new FeedItem();
    fp.Body = 'Testing via Apex';
    fp.parentId = acc.id;
    insert fp;
    // Query AccountFeed back to verify if the post was created correctly
    AccountFeed[] accFeed = [Select Id, Body From AccountFeed Where ParentId =:acc.Id];
    // should be 1 record in the feed for this accountid
    System.assertEquals(1, accFeed.size());

April 11, 2012

Fighting and fixing the “MIXED_DML_OPERATION” error !

“MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): User, original object: Account”

You can easily run into this error if you are trying to perform DML on setup and non-setup objects in the same transaction. Non-Setup objects can be any one of standard objects like Account or any custom object, here are few examples of the Setup Objects

  • Group1
  • GroupMember
  • QueueSObject
  • User2
  • UserRole
  • UserTerritory
  • Territory

This error typically comes in two different scenarios i.e.