1

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:

  1. 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.
  2. 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.

1 Answer 1

3

For the first part, standard relationships in Salesforce are usually plural. That means for the contacts on an account, you'd use Contacts, and for TopicAssignment on an object, you'd use TopicAssignments.

For the second part, you need to iterate over your values and output them somehow. Here, I joined them into a string, but you could also write them out as an array if you wanted.


List<Articles__Custom_Content__c> articles = [SELECT Id, (SELECT Id, Topic.Name from TopicAssignments), 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);
        String[] topics = new String[0];
        for(TopicAssignment topic: article.TopicAssignments) {
            topics.add(topic.Topic.Name);
        }
        gen.writeStringField('topics', String.join(topics,','));
        gen.writeStringField('link',  'https://abc.visual.force.com/apex/articledetail?contentId=' + article.Id);
        gen.writeEndObject();
    }
    String articlesJSON = gen.getAsString();
    return articlesJSON;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .