Salesforce SOQL relationship query question - salesforce

I want to get the field userhomepage from the custom table WebsiteUser via a SOQL query on the Account table. I tried about 10 different queries but i'm not getting it working...
fe. I've tried SELECT field1, (SELECT userhomepage FROM User) FROM Account with all the __c and __r combinations.
I've got the following structure:
<complexType name="Account">
<complexContent>
<extension base="ens:sObject">
<sequence>
...
<element name="WebsiteUser__c" nillable="true" minOccurs="0" type="tns:ID"/>
<element name="WebsiteUser__r" nillable="true" minOccurs="0" type="ens:WebsiteUser"/>
And the WebsiteUser table has a string field called userhomepage.
How do I put that in a query? i'm completely stuck, thnx in advance!

Maybe a child-to-parent style query will work.
Try this?
SELECT Account__r.field1, userhomepage FROM WebsiteUser__c
Relevant-looking documentation:
http://www.salesforce.com/us/developer/docs/api/index_Left.htm#StartTopic=Content/sforce_api_calls_soql.htm

I'm assuming that WebsiteUser__c has a lookup to Account which is what I believe the snippet you've posted is showing, however that doesn't look like the standard object XML so I'm not 100% on where it's coming from.
A lookup from WebsiteUser__c to account creates a Many Website User to One Account relationship. If you were selecting from the Website User table you'd do something like:
select Id, UserHomePage__c, Account__r.Name
from WebsiteUser__c where some conditional
Querying the other way around requires a subquery:
select Id, Name, (select Id, UserHomePage__c from WebsiteUser__r)
from Account
This will return an Account with a list of all WebsiteUser__c records which are associated with it, you could run through the results like so:
for(Account sAcct : select Id, Name,
(select Id, UserHomePage__c from WebsiteUser__r)
from Account limit 200)
{
for(WebsiteUser__c sUser : sAcct.WebsiteUser__r)
{
System.Debug(sUser.UserHomePage__c);
// etc.
Some things to watch out for are that WebsiteUser__r might be a plural, i.e. WebsiteUsers__r, and if you've tried all combinations and it's not working, check that you didn't put in __c or __r yourself into the API object name, the system does this automatically so you'd end up with fields ending in __c__c or __r__r.
If you say what information you have to base the query on I might be able to make this answer a little bit more specific for you!

SELECT id, (SELECT UserHomePage__c FROM WebsiteUser__r) FROM ACCOUNT
if websiteUser__r is the child relation
SELECT id, WebsiteUser__r.UserHomePage__c FROM ACCOUNT
if websiteUser__r is the parent relation
it seems like a parent relationship to me so I think the later query would work...
Hope that helps

Related

Salesforce - Querying to fetch data from junction object and Accounts

I have a junction object called "JIRA_Data" and its linked/ related to Accounts. How do I query to fetch data from this relationship. I am trying to extract id,type from Account object and Name from JIRA_Data junction object.
Could anyone assist. Thanks
You could do it multiple ways depending on how you want the data. If you query it from the parent account it would be something to the effect:
SELECT Id, Name, (Select ID, Name, Other_Parent__r.Name FROM Jira_Datas__r) FROM Account
or from the junction
SELECT Id, Name, Account.Name, Other_Parent__r.Name FROM Jira_Data__c

Filtering issue with multiple forloop in apex | salesforce

I have a scenario where multiple loopings are causing the system resource error.
I need some help with map of map syntax or coding sample for this requirement.
Requirement is:
Account has 1 or more ReportCard records.
ReportCard has Account and Contact.
Now i need to get the list of ReportCards and filter by 1 per contact and recently created records only.
If ReportCard has 2 records with same contact, include only recently created.
// get list of unique accounts from the set
list<Account> accList = new list<Account >([SELECT Id,Average_of_Pulse_Check_Recommend_Score_N__c,Average_of_Recommend_Score_Lanyon_N__c,Average_of_Touchpoint_Recommend_Score_N__c,Average_of_Touch_Point_Satisfaction_N__c FROM Account WHERE Id in:AccIds]);
list<ReportCard__c> allRCList = new list<ReportCard__c>([SELECT Id,Net_Promoter_text__c,CreatedDate, Contact__c, Account__c, RecordTypeID, Touchpoint_Satisfaction_text__c FROM ReportCard__c WHERE Account__c in:accList Order By Account__c, CreatedDate Desc]);
List<ReportCard__c> rcListbyAccounts = new List<ReportCard__c>();
for(Account acc:accList)
{
Any help would be appreciated.
Thanks
I'm not sure I understand your situation correctly. You've skipped the for loop - I strongly suspect any issues you have there sit in the loop rather than in the queries.
Looks like you should read about using relationship queries (salesforce versions of JOIN in regular database): http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_relationships.htm
Pay special attention to subqueries (which behave similar to how a related list behaves on record's detail page).
From what I see I'd say you don't need to query for Accounts at all, or at least not like that. This will work equally well:
SELECT Id,Net_Promoter_text__c,CreatedDate, Contact__c, Account__c, ...
FROM ReportCard__c
WHERE Account__c in:accIds
ORDER BY Account__c, CreatedDate Desc
Now lets attack this:
List of ReportCards and filter by 1 per contact and recently created
records only. If ReportCard has 2 records with same contact, include
only recently created.
I'd reverse it - I'd start the query from Contact level, go down to the related list of Report Cards and pick the latest one. That way it eliminates the issue with duplicate contacts for you. Something like this:
SELECT Id, Name, Email,
(SELECT Id, Net_Promoter_text__c, CreatedDate, Account__c, Account__r.Name, Account__r.Average_of_Pulse_Check_Recommend_Score_N__c
FROM ReportCards__r
WHERE Account__c IN :accIds
ORDER BY CreatedDate DESC
LIMIT 1)
FROM Contact
WHERE AccountId IN :accIds
This goes from Contact "down" to Report Cards (via the relationship name ReportCards__r) and then "up" from Card to Account via Account__r.Name, Account__r.Average_of_Pulse_Check_Recommend_Score_N__c...

Salesforce - Apex - query accounts based on Activity History

Hey Salesforce experts,
I have a question on query account information efficiently. I would like to query accounts based on the updates in an activityHistory object. The problem I'm getting is that all the accounts are being retrieved no matter if there's "complete" activeHistory or not. So, Is there a way I can write this query to retrieve only accounts with activeHistory that has status="complete" and Type_for_reporting='QRC'?
List<Account> AccountsWithActivityHistories = [
SELECT
Id
,Name
,( SELECT
ActivityDate
,ActivityType
,Type_for_Reporting__c
,Description
,CreatedBy.Name
,Status
,WhatId
FROM ActivityHistories
WHERE Status ='complete' and Type_for_Reporting__c = 'QRC'
)
FROM Account
];
You have a WHERE clause on the histories but you still miss one on the Account level.
For example this would return only Accounts that have Contacts:
SELECT Id, Name
FROM Account
WHERE Id IN (SELECT AccountId FROM Contact) // try with NOT IN too
With Activities it's trickier because they don't like to be used in WHERE in that way.
http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_soql_select.htm
The following objects are not currently supported in subqueries:
ActivityHistory
Attachments
Event
EventAttendee
Note
OpenActivity
Tags (AccountTag, ContactTag, and all other tag objects)
Task
Additionally the fine print at the bottom of ActivityHistory definition is also a bit discouraging.
The following restrictions on users who don’t have “View All Data” permission help prevent performance issues:
In the main clause of the relationship query, you can reference only
one record. For example, you can’t filter on all records where the
account name starts with ‘A’; instead, you must reference a single
account record.
You can’t use WHERE clauses.
You must specify a limit of 499 or fewer on the number of rows returned in the list.
You must sort on ActivityDate in ascending order and LastModifiedDate in descending order; you can display nulls last. For
example: ORDER BY ActivityDate ASC NULLS LAST, LastModifiedDate DESC.
Looks like you will need multiple queries. Go for Task (or Event, depending for which the custom field is visible), compose a set of AccountIds and then query the Accounts?
Or you can manually filter through list from your original query, copying accounts to helper list:
List<Account> finalResults = new List<Account>();
for(Account a : [SELECT...]){
if(!a.ActivityHistories.isEmpty()){
finalResults.add(a);
}
}

soql select individual CaseComment with all its FeedComments

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

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.

Resources