+ Start a Discussion
PremInfoPremInfo 

How to get the Parent Name

Hi,

 

SELECT Body, CommentCount, Id, InsertedById, IsDeleted, LikeCount, LinkUrl, ParentId, RelatedRecordId, Title, Type, CreatedDate, ContentData, ContentDescription, ContentFileName, ContentSize, ContentType, Parent.Name, CreatedBy.FirstName FROM FeedItem WHERE Type != 'ContentPost' ORDER  BY  CreatedDate  DESC

 

My aim is to get the Parent name and the feed created person name with the feedItem using above query.

 

The Parent name and created by fields are allways returing null. Kindly advice am I doing any mistake or what is the right way to get the parent name from the FeedItem.ParetnId.

 

Thanks,

Best Answer chosen by Admin (Salesforce Developers) 
PremInfoPremInfo

I am able to get the parent name. 

 

Solution: Parent.name is a realted object to FeedComment. If we try to get the value of it directly it returns null. Instead we need to get the children of Parent.name.

 

[code]

String req = "SELECT Body, ParentId, Parent.Name FROM FeedItem ORDER BY CreatedDate DESC";
QueryResult res = connection.query(req);
for(SObject s:res.getRecords()){
Iterator<XmlObject> iterator = s.getChildren();
while(iterator.hasNext()){
XmlObject xmlObject = iterator.next();
if(xmlObject.getName().toString().substring(37).equalsIgnoreCase("Parent")){
Iterator<XmlObject> childs = xmlObject.getChildren();
while(childs.hasNext()){
XmlObject childNode = childs.next();
System.out.println(childNode.getName()+":"+childNode.getValue());

}

}else{
System.out.println(xmlObject.getName().toString()+":"+xmlObject.getValue());
}
}
System.out.print("\r\n==========================\r\n");
}

[/cpde]

All Answers

BharathimohanBharathimohan

Hi Prem,

 

All all the Custom Objects will have a Feed Object if chatter Feed Tracking is enabled for that object.

For example if the custom object is Deal__c and if Chatter Feed Tracking is enabled, then Deal__Feed is the object to be queried.

For Standard Objects, you need to use FeedItem and the way of querying Parent.Name and CreatedBy.Name is correct.

 

Let me know if it works for you.

 

Thanks,

Bharathi

Salesforce For All

ShozubQShozubQ

do you have view all access? does this query return any results? if the parentId null as well?

PremInfoPremInfo

I am getting the ParentId but the Parent.name and CreatedBy are coming as null."view All Data" Access is enabled for my account.

 

 

Jia HuJia Hu

Since FirstName field on the User object is not an necessary filed, it can be null.
Basically, Parent.name should not be null.
Which object did you post on?

You can use following query to check the feed type and parent type,
FeedItem fi = [SELECT Type, ParentId, Parent.Type, Parent.Name, CreatedBy.FirstName FROM FeedItem WHERE Type != 'ContentPost' ORDER BY CreatedDate DESC limit 1];

System.debug( ' Type: ' + fi.Type + ' Name: ' + fi.Parent.Name + ' FirstName: ' + fi.CreatedBy.FirstName + ' Parent.Type: ' + fi.Parent.Type );

PremInfoPremInfo

I am able to get the parent name. 

 

Solution: Parent.name is a realted object to FeedComment. If we try to get the value of it directly it returns null. Instead we need to get the children of Parent.name.

 

[code]

String req = "SELECT Body, ParentId, Parent.Name FROM FeedItem ORDER BY CreatedDate DESC";
QueryResult res = connection.query(req);
for(SObject s:res.getRecords()){
Iterator<XmlObject> iterator = s.getChildren();
while(iterator.hasNext()){
XmlObject xmlObject = iterator.next();
if(xmlObject.getName().toString().substring(37).equalsIgnoreCase("Parent")){
Iterator<XmlObject> childs = xmlObject.getChildren();
while(childs.hasNext()){
XmlObject childNode = childs.next();
System.out.println(childNode.getName()+":"+childNode.getValue());

}

}else{
System.out.println(xmlObject.getName().toString()+":"+xmlObject.getValue());
}
}
System.out.print("\r\n==========================\r\n");
}

[/cpde]

This was selected as the best answer