I have a custom object which contains "news articles" and these articles can be assigned to Topics. I'm attempting to create a JSON output through VisualForce/Apex which returns article content along with the Topics which are assigned, but I'm having trouble getting the Topics part of the code working.
This is my current code:
public with sharing class JSONContentController {
public String articlesJSON {get;set;}
public String title;
public JSONContentController() {
articlesJSON = prepareData();
}
private String prepareData() {
List<Articles__Custom_Content__c> articles = [SELECT Id, (SELECT Id, Topic.Name from TopicAssignment), Articles__Title__c, Articles__Text__c, Articles__Published__c FROM Articles__Custom_Content__c WHERE Articles__Published__c = True ORDER BY Articles__Publish_Start__c DESC LIMIT 5];
JSONGenerator gen = JSON.createGenerator(true);
for (Articles__Custom_Content__c article :articles) {
gen.writeStartObject();
gen.writeStringField('title', article.Articles__Title__c);
gen.writeStringField('content', article.Articles__Text__c);
gen.writeStringField('topics', article.TopicId);
gen.writeStringField('link', 'https://abc.visual.force.com/apex/articledetail?contentId=' + article.Id);
gen.writeEndObject();
}
String articlesJSON = gen.getAsString();
return articlesJSON;
}
}
The areas where I seem to be having trouble are:
- The subquery in the SOQL statement
(SELECT Id, Topic.Name from TopicAssignment)
This is currently giving me the error "Didn't understand relationship 'TopicAssignment' in FROM part of query call", though I have a feeling there are multiple things wrong with this line. - The
gen.writeStringField('topics', article.TopicId);
line that attempts to return the topics to JSON output. It's possible this is due to the first issue, but even when I've gotten the first issue to not error, this line then does error.