soql select individual CaseComment with all its FeedComments - salesforce

I am trying to select all comments ,feeds and feedcomments for an individual case.
The hierarchy is like
Case
|
CaseComment
|
FeedComments(commnets or feeds under a CaseComment)
I could not find any relation between CaseComments and FeedComments nor CaseComments and CaseFeeds.
How can I select all together in a soql or individual soqls which relates Case, CaseComment,CaseFeed,FeedComment?

EDIT
The query you've included in the comment looks good. I'd write it as something like that:
SELECT Id, Body, ParentId, Parent.CaseNumber, CreatedDate,
(SELECT Id, CommentBody, CommentType FROM FeedComments)
FROM CaseFeed
ORDER BY Parent.CaseNumber, CreatedDate
(this is sample output rendered in Real Force Explorer, a pretty neat tool)
If I'll click into the "2 records" bit I can drill down to the fields selected from FeedComment for "this" CaseFeed:
If your query renders differently for you (some stuff is blank) - maybe try this different editor or even go to https://workbench.developerforce.com
If only some comments contain text - they might be uploaded images for example - filter them by CommentType = 'TextComment'?
ORIGINAL
FeedComments(commnets or feeds under a CaseComment)
No, not really. FeedComment is a Chatter table that can link to many objects but CaseComment is not one of them.
Maybe study the Chatter Entity Relationship Diagram?
Anyway - relationship to feed* objects doesn't have a nice name exposed so we can't query it all in one go:
I think you'll need something like this:
SELECT Id, CaseNumber,
(SELECT Id, CommentBody FROM CaseComments),
(SELECT Id, Body FROM Feeds)
FROM Case
SELECT Id, FeedItemId, ParentId, CommentBody
FROM FeedComment
WHERE ParentId = :caseIdHere

Related

How can i make a multi level query using SOQL in Salesforce?

I am running the nested query
SELECT Id, Name, Account.Name,
(SELECT Id, OrderItemNumber, Product2.Name
FROM OrderItems)
FROM Order
This is only 1 level deep.
Is there an example which can show me how to make a 3 to 5 level deep select in SOQL ?
SOQL is limited in that subqueries like you show are only allowed at a single level.
However, you can go higher (5 levels) if you do bottom up.
E.g. we rewrite your query like so:
SELECT Id, OrderItemNumber, Product2.Name,
Order.Id, Order.Name, Order.Account.Name,
Order.Account.Parent.Name,
Order.Account.Parent.Parent.Name,
Order.Account.Parent.Parent.Parent.Name,
Order.Pricebook2.Name
FROM OrderItem
To get some account hierarchy.
It's clunky as SOQL is, but you get the idea

SOQL Subquery counting the number of results

I'm trying to create a SOQL query that pulls records from an Object based on if there are any results from another linked object. Here is sort of what I am trying to do.
SELECT id, casenumber FROM case WHERE count(SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = case.id) > 0
This returns the error,
MALFORMED_QUERY:
casenumber FROM case WHERE count(SELECT ContentDocumentId FROM ContentDocumentLink
^
ERROR at Row:1:Column:44
unexpected token: 'SELECT'
After a few attempts at trying to make this work I am not sure what else try. I know SOQL has some limitations but I am unsure if this is one of them. Anyone have insight? Thanks.
It's "down, then up", via many to many relationship. 1 document could be linked to many Cases but wasting storage space just once.
Normally you'd try something like this
SELECT Id, CaseNumber, Subject
FROM Case
WHERE Id IN (SELECT LinkedEntityId FROM ContentDocumentLink)
But it won't compile, there are some limitations. Experiment, worst case query all cases and use apex to inspect the related list?
SELECT Id, CaseNumber, Subject, (SELECT ContentDocumentId FROM ContentDocumentLinks LIMIT 1)
FROM Case
Case is funny. Attachments linked to emails (if you have Email-to-Case) will show on related list of Files but they won't be physically there. The full link will be Content Document -> ContentDocumentLink -> EmailMessage -> Case. Argh. CombinedAttachment is supposed to help with it, special readonly related list / view / whatever.
One more idea using polymorphic soql, bit bass-ackwards but give it a go:
SELECT ContentDocumentId, TYPEOF LinkedEntity WHEN Case THEN Id, Subject, CaseNumber END
FROM ContentDocumentLink
WHERE LinkedEntityId IN (SELECT Id FROM Case)

What's better in designing object attachment in sql server

I need to represent adding attachments to order or order item using sql server.
I think of two ways but wonder which one is better.
Solution 1:
table1: attachment (id, name, description, uri, ...)
table2: order (id, client_id, ...)
table3: orderItem (id, order_id, product_id, ...)
table2: objectAttachment (id, objectType, object_id, attachment_id, ...)
In this case I retrieved order attachments like this:
create proc GetOrderAttachments(#order_id int)
AS
select attachment_name, attachment_uri, attachment_desription, order.(...)
from objectAttachment orderAttachment
inner join order on order.id = orderAttachment.object_id
inner join attachment on attachment.id = orderAttachment.attachment_id
where orderAttachment.objectType = 'order' and orderAttachment.object_id = #order_id
Solution 2:
table1: attachment (id, name, description, uri, ...)
table2: orderAttachment (id, order_id, orderItem_id, ...)
table3: order (id, client_id, ...)
table4: orderItem (id, order_id, product_id, ...)
In this case I retrieved order attachments like this (knowing that orderItem_id is neglected):
create proc GetOrderAttachments(#order_id int)
AS
select attachment_name, attachment_uri, attachment_desription, order.(...)
from orderAttachment orderAttachment
inner join order on order.id = orderAttachment.order_id
inner join attachment on attachment.id = orderAttachment.attachment_id and orderAttachment.order_id = #order_id
Lets consider various situations and compare each approach.
Question. How many attachment are there of any type?
1 approach will select from one table(good).
2 approach will need to union many tables.
What if you add one more entity with attachments. You will need to remember all places where you are answering questions like this and add one more case. First approach is safe from this point of view.
You are asked to add additional attributes for order attachments and some other attributes for some other entity's attachments.
With first approach you will need to add attributes of both in one table which will be nullable and filled for just one type of entity type.
Second approach is safe. You add attributes to appropriate entities. There are no many many nullable columns just to maintain one type of attachment.
You are asked to delete all attachments of orders.
With first approach if you have huge amount of data delete operation will take long period of time.
With second approach you can remove relationships and truncate table which will take millisecond.
You can continue this list. Answering various questions and considering cons and pros of different approaches will help you to decide which approach fits better to your needs. There wouldn't be just one correct answer.
There is no better solution. All depends on your needs. The problem here is which table inheritance model fits for you.
You can start reading here:
Single Table Inheritance
Class Table Inheritance
Concrete Table Inheritance
Single Table Inheritance
Class Table Inheritance
*ttp://martinfowler.com/eaaCatalog/concreteTableInheritance.html

Chatter : SOQL for fetching CommentLikes

I have requirement to fetch comment likes. I'm not able to figure out on how to retrieve commentlikes.
FeedLike object represents likes. You can't query FeedLike records directly. They can only be queried via the parent NewsFeed, UserProfileFeed, or entity feed, such as AccountFeed.
So to query FeedPost likes, use following:
SELECT Id, (SELECT Id, CreatedById, CreatedDate, FeedItemId, FeedEntityId FROM FeedLikes) FROM UserFeed
to query group post likes, use following:
SELECT Id, (SELECT Id, CreatedById, CreatedDate, FeedItemId, FeedEntityId FROM FeedLikes) FROM CollaborationGroupFeed
How to retrieve comment likes?
They are available through the Chatter REST API: http://wiki.developerforce.com/page/Chatter_API
I think you want to ChatterActivity SObject
Select c.ParentId, c.LikeReceivedCount, c.Id, c.CommentReceivedCount From ChatterActivity c
This should get what you are looking for.
EDIT:
Strange, I don't seem to be able to find anything on doing this in apex, you can do it through the api by calling
/chatter/comments/commentId/likes
So it should be possible somewhere along the line.This could work as a workaround though.

Complex query possible?

I have 3 tables, a parent table and 2 child tables. Lets say Mother_c is the parent. Then Child_c and Pet_c are the 2 child tables that have master-detail relationship pointer to Mother_c.
I have the Id of one row from Child_c, I want to retrieve all the rows from Pet_c that correspond to the Mother_c of that single Child_c row.
I'm wondering if this is possible in one SOQL query?
Yes, this is totally possible with semi-join SOQL. I tested this out with the standard CRM objects like this:
SELECT Id,
(SELECT Id FROM Cases)
FROM Account
WHERE Id IN (SELECT AccountId
FROM Contact
WHERE Id = '0036000000qCwp9'
)
To walk you through this, with a given Contact id, you first find the parent Account, and then traverse back down to the child Cases. In your example with custom objects, it would be very similar, but would use the __r custom relationships names instead:
SELECT Id,
(SELECT Id FROM Pet__r)
FROM Mother__c
WHERE Id IN (SELECT Mother__c
FROM Child__c
WHERE Id = '003a000000qCwp9'
)

Resources