Monthly Archives: October 2014

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: Platform & Web Performance Optimization. Developer and Administrator Certified. Senior Frontend, Javascript and HTML5. Heavy Ajax WebApps Mobile WebApps – Phonegap Webapps Developer (Php/Django/Python) Team Work & Managment Now leading a Salesforce applications company