Failing to reference name of user from relationship query - salesforce

I am trying to get just get the permissionSetGroupComponents for test users in Salesforce with this query.
SELECT AssigneeId, IsActive, ExpirationDate, PermissionSetGroupId, PermissionSetId FROM PermissionSetAssignment WHERE User.FirstName = 'Test'
But when I run that I get this error.
PermissionSetAssignment WHERE User.FirstName = 'Test'
^
ERROR at Row:1:Column:119
Didn't understand relationship 'User' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
According to the API documentation there is a relationship between the two objects so I don't understand why it cannot find the relationship.
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_permissionsetassignment.htm

User is not the name of the relationship. The relationship field is AssigneeId, so the relationship field is Assignee:
SELECT AssigneeId, IsActive, ExpirationDate, PermissionSetGroupId, PermissionSetId
FROM PermissionSetAssignment
WHERE Assignee.FirstName = 'Test'

Related

Who to access field of nested relationship in soql where first relation is simple and next is polymorphic

I want to access custom field of Owner (User) fields where Owner is polymorphic relation it can be either User or Group.
I tried following query:
SELECT Id, Email, Name, Lead.TYPEOF Owner WHEN User THEN custom__c FROM CampaignMember WHERE CampaignId='xxxxxx'
Relationship stack is:
Lead > Owner (User,Group) > custom__c.
SELECT field1__c,
field2__c
FROM Lead
WHERE Owner.type = 'User'
AND CampaignID = 'xxxxxx'
EDIT:
Leaving my original answer in case it helps someone else.
I believe what you are looking for is this:
SELECT Id, Email, Name,
TYPEOF Lead.Owner
WHEN User THEN custom__c
END
FROM CampaignMember
WHERE Field != 'value'

Soql object has multiple lookups to user

Afternoon,
I am gathering data from the LiveChatTranscript and there are multiple references to user as lookup fields. How do I pick which property I want to use as my reference to user?
LiveChatTranscript properties
LastModifiedById Lookup(User)
CreatedById Lookup(User)
OwnerId Lookup(User,Group)
query
-webroot-/query/?q=select chatkey, caseid, status,requesttime, starttime, endtime,endedby,name,ReferrerUri,platform,location, waittime,body,supervisortranscriptbody, case.description, ownerid,ownerid.user.alias?? from livechattranscript where chatkey = '12345'
In this case, owner is the field name. It looks up to the user object. No need to include the object name there. Just drop the id potion for ownerid.
Use owner.alias.

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

Salesforce Cross Object SOQL Query

I have a custom object called 'Billings'. On that object there is a custom lookup field to the Opportunity. I'm trying to query all the 'Billings' records associated with a particular Opportunity. I get "Unknown error parsing query".
SELECT Id, StageName,
( SELECT Name, Email FROM Billings__C )
FROM Opportunity WHERE Opportunity ID = '0011000002mfTil'
For Nested Queries in SOQL, you need to use the '__r' suffix on the Child Relationship Name for the field that provides the look-up from the Child Record to the Parent.
In your case, this is most likely 'Billings__r'. However, to confirm, go to the Billings object and click on the custom field that provides the lookup to the Opportunity object. In the "Lookup Options" section on the Custom Field details screen you will see the official Child Relationship Name without the '__r' suffix. This is the correct name for this relationship for a Nested Query.
The other error in your query is that you have "WHERE Opportunity Id =". It should just be "WHERE Id ="
Based on the information you provided, your query should be:
SELECT Id, StageName, ( SELECT Name, Email FROM Billings__r ) FROM Opportunity WHERE ID = '0011000002mfTil'

Best rules to get data with Contain

In CakePHP 3 ORM has changed and I can't find the proper way to select needed data from the database.
In CakePHP 2, I use contain('User.name','User.id'), but In CakePHP 3 this code doesn't work.
So how can I select only id and name from User?
The code:
$query = $data->find()->contain(['Users'])->execute()->fetchAll('assoc');
// I want only user.id and user.name.
$articles = $this->Model->find()
->select(['fields_you_want_from_this_Model'])
->contain(['Assoc_Model' => function($q) {
return $q
->select(['fields_you_want_from_the_associated_model']);
}]);
U must take a look about this page: http://book.cakephp.org/3.0/en/orm/query-builder.html#passing-conditions-to-contain
In certain case you must use autoFields method.
Be carefull with contain when u select few fields in the callable, u always have to select the foreign key also:
When you limit the fields that are fetched from an association, you must ensure that the foreign key columns are selected. Failing to select foreign key fields will cause associated data to not be present in the final result.

Resources