Salesforce SOQL Query with Self Join + Relationship (ContentDocument/ContentVersion) - salesforce

I'm trying to write a SOQL query to retrieve some Salesforce Content records and am having a bit of difficulty figuring out my next step. I want to exclude all versions of a document if a custom field of any version of that document has a value (is not null). Here's slimmed down version of what I am trying to do:
Select Id, Title
From ContentVersion
Where ContentDocumentId Not In
(
Select ContentDocumentId,
From ContentVersion
Where Custom_Field__c != null
)
So I know that you can't write a subquery that targets the same object as its outer query, so obviously what I've specified above won't work. Any suggestions on what will work?
Thank you.

Can You try something like this:
Select C.Id from ContentDocument C where
ID not in ( Select ContentDocumentId
From ContentVersion
where Custom_Field__c != null)

Related

How to get binarydata from ContentVersion based on Case ID

I need help to make a soql to get the images from ContentVersion based on caseId
We have 3 objects:
ContentDocument
ContentVersion
ContentDocumentlink
ContentDocument is parent of ContentVersion and ContentDocumentLink
ContentVersion has versiondata(that is binary data)
ContentDocumentLink having case id(Fieldname is LinkedEntityId)
I need to create a SOQL that provides version data of ContentVersion based on linkedentityID ( case- ID) when I use this query
select versiondata from contentversion where contentdocumentid in(select contentdocumentid from contentdocumentlink where linkedentityid in(select id from case))
Than it is giving the error like
Entity 'contentdocumentlink' is not supported for semi join inner selects
It is giving error like semi join is not supported.
This works with any id (any object that you marked as allow attachments)
SELECT ContentDocument.LatestPublishedVersion.VersionData
FROM ContentDocumentLink
WHERE LinkedEntityId = '...'
Or top-down (useful if you need multiple records and their files)
SELECT Id, CaseNumber, Subject,
(SELECT ContentDocument.LatestPublishedVersion.VersionData FROM ContentDocumentLinks)
FROM Case
WHERE Id = '...'

How to find all the fields value of an item with sql query in sitecore?

So here is a scenario is like:
find the fields value of all the items of specific parent template item in Sitecore
(i.e. suppose i want to list fields value of newslist, where newslist is news type item)
with SQL Query so what i am doing with the help of SharedFields and Items table I am trying to get the information but unable to get the information.
For single field I am getting but for multiple I am not able to do :
Here is the query:
SELECT distinct S.ItemId, S.Value AS NewsType,
FORMAT(S.Created,'yyyy/MM/dd') AS CreatedOn, FORMAT(S.Updated,'yyyy/MM/dd') AS UpdatedOn
FROM
[DBName].[dbo].[Items] I,
[DBName].[dbo].[SharedFields] S
WHERE I.ParentID='{XXXXXX-X-XXXXX-XXXXX-XXXXXX}'
AND S.FieldId='{YYYY-YYYY-Y-Y-Y-Y-Y-YYYYY}'
where PARENTID is the id of news item
and fieldid is id of newstype
now i want to add one more column into the query as newsOf
So how can I do that?
You need to add another table to your FROM clause (second SharedFields) and use JOIN like this:
SELECT
S.ItemId,
S.Value AS NewsType,
S2.Value AS NewsOf,
FORMAT(S.Created,'yyyy/MM/dd') AS CreatedOn,
FORMAT(S.Updated,'yyyy/MM/dd') AS UpdatedOn
FROM
[DBName].[dbo].[Items] I
JOIN [DBName].[dbo].[SharedFields] S ON S.ItemId = I.ID
JOIN [DBName].[dbo].[SharedFields] S2 ON S2.ItemId = I.ID
WHERE
I.ParentID='{11111111-1111-1111-1111-111111111111}'
AND S.FieldId='{field-1-id}'
AND S2.FieldId='{field-2-id}'

Using the result of one query to feed into another query

I'm trying to create a compliance rule that checks for a listing of the sys.traces
SELECT id FROM sys.traces
and then use that result to list which trace events have been added to the trace
SELECT DISTINCT(eventid)
FROM sys.fn_trace_geteventinfo(#result from previous query)
I want the result to be just a list of trace ids that I can compare to a set requirement.
I'm obviously new to this and have searched but didn't find anything specifically relevant. I know this is an easy one.
SELECT DISTINCT(eventid)
FROM sys.fn_trace_geteventinfo(SELECT id FROM sys.traces)
SELECT DISTINCT(eventid)
FROM sys.fn_trace_geteventinfo(SELECT top 1 id FROM sys.traces)
Try this query
select distinct info.eventid
from sys.traces as tr
cross apply sys.fn_trace_geteventinfo (tr.id) as info

Database schema for posts with multiple tags

I am building the typical post and tag application where a post can have multiple tags. I am stuck on setting up the database schema. I have so far:
Post:
POST_ID PRIMARY KEY
POST_TITLE
POST_BODY
POST_DATE
POST_USERID
Tag:
TAG_ID PRIMARY KEY
TAG_TAGNAME
PT:
PT_ID
PT_POSTID
PT_TAGID
When a user submits a post, I insert the form data into the post table. Next step I loop through the tagnames the user provided and see if they are in the TAG_TAGNAME field. If there is a match, grab the ID and insert it into the PT table. ELSE insert the name into the tag table and grab the ID and insert it into the PT table.Both are along with the postid generated in the POST table insert.
Finally I have a view called PTVIEW with the following schema:
SELECT *
FROM dbo.PT
JOIN Post
ON PT_PostID = dbo.Post.POST_ID
JOIN Tag
ON PT_TagID = tag.TAG_ID
Here is a sample result of select * from PTVIEW
Problem is I can’t get a view of the unique posts along with their tags.
I am using MSSQL so I DO NOT have the Group_concat function built into mySQL.
I can’t build the initial page that will show each post along with its correlating tags like stackoverflow does on the homepage. What am I doing wrong in the PTVIEW ?
You basically have 2 options:
Split it up into 2 different queries, get a post, then get it's
associated tags.
Concatenate the tags into a single field using a statement like
this:
SELECT DISTINCT
P.Post_ID ,
SUBSTRING(( SELECT ',' + T.TAG_TAGNAME AS [text()]
FROM dbo.PT PT
INNER JOIN dbo.Tag T ON PT.PT_TAGID = T.TAG_ID
WHERE P.POST_ID = PT.POST_ID
FOR
XML PATH('')
), 2, 1000) [Tags]
FROM dbo.Post P
Even though the second option looks kinda nifty, the first option has my personal preference.
I think the second is not very readable and performance would not be very good on larger datasets.
Here is how I learned to do it.
SELECT *
,(
SELECT TAG_TAGNAME+ ', '
from PT
JOIN Tag ON PT_TagID = TAG_ID
Where PT_PostID = POST_ID
ORDER BY TAG_TAGNAME
FOR XML PATH('')
) AS Tags
FROM Post

Doing a join with SOQL

Here is my SOQL problem.
Query 1:
Select
c.Date_Joined__c,
c.Email,
c.FirstName,
c.LastName,
c.regcode__c
from Contact c WHERE c.regcode__c ='XXXXXXXXX'
Query 2:
Select
p.Account__c,
p.Date__c,
p.Points__c,
p.Description__c,
p.Code__c
from Points__c p where p.Account__c ='YYYYYYYYYYYY' and (p.Points__c > 0)
Order by p.Date__c DESC
The relationship between the two queries is that c.regcode__c will have the same value as p.Code__c.
I want to combine Query1 and Query2, so c.regcode__c = p.Code__c
I'm stuck, I can't seem to get the syntax right for SOQL.
Is it even possible to do joins in the API?
You can't really create a join per se but you can do some filtering using a syntax similar to this:
SELECT Id FROM Contact WHERE c.RegCode__c IN (SELECT p.Code__c FROM Account)
As long as the subquery in the WHERE clause is only returning a single value and codes is a filterable field this should work. Also, this doesn't work if you were trying to filter by the same object (i.e. Account to Account). You can add more criteria to the account side to match your example queries.
Again, this isn't a true join so you can't put the account fields from your subquery. But you can at least filter down your contacts.

Resources