function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
d3developerd3developer 

Accessing Feedpost.Body with SObject.get('FeedPost.Body')

 

I'm trying to write a function to merge comments and posts from one feed to another. Sadly I get this error (from the debug log) :

 

 


13:28:14.418|SOQL_EXECUTE_BEGIN|[343,39]|Aggregations:0|SELECT id, FeedPost.Body from ChatterTasks__ToDoItem__Feed where id = '0D5A0000000GoXbKAK' limit 1
13:28:14.431|SOQL_EXECUTE_END|[343,39]|Rows:1|Duration:13
13:28:14.431|METHOD_EXIT|[343,39]|query(String)
13:28:14.431|METHOD_ENTRY|[347,47]|SObject.get(String)
13:28:14.431|METHOD_EXIT|[347,47]|get(String)
13:28:14.431|METHOD_ENTRY|[348,52]|SObject.get(String)
13:28:14.431|EXCEPTION_THROWN|[348,52]|System.SObjectException: Invalid field FeedPost.Body for ChatterTasks__ToDoItem__Feed

 

 

When I run this code:

 

 

               for(String id : activeSObjectFeedItems) {
try {
String feedObjectName = feedObjectName(activeSObjectType);
String queryString = 'SELECT id, FeedPost.Body from ' + feedObjectName + ' where id = \'' + id + '\' limit 1';
List<SObject> sos = Database.query(queryString);
FeedComment fcomment = new FeedComment();
fcomment.FeedItemId = (Id) sos[0].get('id');
fcomment.CommentBody = (String) sos[0].get('FeedPost.Body');
insert fcomment;
}
catch(Exception e) {
System.debug('MergeFeed: ' + e);
}
}

 

But I can't think of a workaround since I can't query the FeedPosts directly.

 

Ideas?

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

This worked for me

 

 

public string getFeedPostBody() {
    list<SObject> af = database.query('select id, feedpost.body from accountfeed where parentId=\'0013000000ZWHQN\'');
    String b = '';
    for (SObject f : af) {
        SObject fp = f.getSObject('feedPost');
        if (fp == null) {
            b = b + '{null}';
        } else {
            String x = (String)f.getSObject('feedPost').get('body');
            b = b + x + '<br>';
        }
    }
    return b;
}

 

 

All Answers

SuperfellSuperfell

its something like sos[0].get("FeedPost").get("Body")

d3developerd3developer

 

Doesn't quite work.

 

Get this when I try to save:

 

 

Save error: Method does not exist or incorrect signature: [Object].get(String)

 

 

Also tried this:

 

 

						SObject so = (SObject) sos[0].get('FeedPost');      
						fcomment.CommentBody = (String) so.get('Body');  

 

 

 

But gives me this error:

 

 

14:5:18.312|EXCEPTION_THROWN|[346,37]|System.SObjectException: Invalid field FeedPost for ChatterTasks__ToDoItem__Feed

 

 

ehartyeehartye

Were you able to resolve this?  I'm having the same issue.

d3developerd3developer

Nope. Made my generic Feed merge tool somewhat less than I hoped.

ehartyeehartye

SimonF -- Do you have any new information to add? :smileyhappy:  I'm trying to make a generic Visualforce feed component, and this is a major roadblock. 

SuperfellSuperfell

This worked for me

 

 

public string getFeedPostBody() {
    list<SObject> af = database.query('select id, feedpost.body from accountfeed where parentId=\'0013000000ZWHQN\'');
    String b = '';
    for (SObject f : af) {
        SObject fp = f.getSObject('feedPost');
        if (fp == null) {
            b = b + '{null}';
        } else {
            String x = (String)f.getSObject('feedPost').get('body');
            b = b + x + '<br>';
        }
    }
    return b;
}

 

 

This was selected as the best answer
ehartyeehartye

Simon, you're the man :) This worked perfectly. I was missing the ".getSObject"

d3developerd3developer

cool.