Child to Parent SOQL Query - salesforce

I need a query between Opportunity and OpportunityLineItem.
OpportunityLineItem has a lookup for Opportunity. Child relationship name is OpportunityLineItems. Opportunity Lookup field is called OpportunityId.
I'm trying the below query but it doesn't work.
select Id,Name,OpportunityId__r.Description from OpportunityLineItem
Also tried:
select Id,Name,OpportunityLineItems__r.Description from OpportunityLineItem

Top-down
SELECT Id, Name, Description,
(SELECT Id, Name FROM OpportunityLineItems)
FROM Opportunity
Bottom-up
SELECT Id, Name, Opportunity.Name, Opportunity.Description
FROM OpportunityLineItem
Check out the links in https://stackoverflow.com/a/73913839/313628 and https://stackoverflow.com/a/73877986/313628 too

Related

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}'

Salesforce REST API SOQL get account for opportunies

I want to do a query for all my opportunities (or contacts or whatever) and then join in the account so I can get the name etc. I'm able to get it to work querying accounts and joining the opportunities, but not the other way around.
select Name, Id, (select Id, Name, CloseDate, StageName, Amount from Opportunities where ownerId = 'id') from Account
Is there a way to do the query on opportunities instead? I don't want to get all accounts and loop through to get the opportunity data. Should be a simple belongs to type relationship.
I've tried such queries as:
select Id, Name, CloseDate, StageName, Amount, (select Name, Id from Account) from Opportunity where ownerId = 'id'
But that gives me "Didn't understand relationship 'Account' in FROM part of query call."
Thanks!
For child->parent relationships you just use a dotted path to the fields you want, e.g.
Select id, Amount, CloseDate, Account.Name from Opportunity

Limit SOQL query results from child clause

Hopefully this will be easy to answer:
select name, id, description, isactive, productcode, imageurl__c, (select name, id, unitprice,Must_Override_Price__c, Is_Taxable__c from PricebookEntries where pricebook2id =: pbe.id) from product2
How do I prevent the product2 records that have no child pricebookentry records from being queried?
So lets say I have 10 products, but only two match the WHERE clause. I only want those two to show up so I'm not wasting resources/rows.
Perform the query relationship in reverse. You can still get all the required Product2 fields, and you will only get results for existing PricebookEntry records.
E.g.
select Id, Name, Pricebook2Id, Product2Id, UnitPrice, IsActive, UseStandardPrice,
ProductCode, IsDeleted,
Product2.Id, Product2.Name
from PricebookEntry
where pricebook2id = :pbe.id
You can do the query in reverse as mentioned in the Daniel's comment or try putting where condition in the top query as well
select name, id, description, isactive, productcode, imageurl__c,
(select name, id, unitprice,Must_Override_Price__c, Is_Taxable__c
from PricebookEntries
where pricebook2id =: pbe.id)
from product2
where Id IN (select Product2Id
from PricebookEntries
where pricebook2id =: pbe.id)

joining tables in salesforce explorer

trying to join lead table with opportunity table in force.com explorer. New to soql. Here is what I have tried with error messages as results... can anyone help please?
SELECT CreatedDate, LeadSource, RecordTypeId, Name__c, StageName, HubSpot_Original_Source_Data_1__c, Name, HubSpot_Original_Source_Data_2__c, HubSpot_Original_Source_Type__c, (SELECT Company, IsConverted, CreatedDate, HubSpot_Inc__HubSpot_Intelligence__c, HubSpot_Original_Source_Data_1__c, HubSpot_Original_Source_Data_2__c, HubSpot_Original_Source_Type__c, LeadSource, RecordTypeId FROM Lead) FROM Opportunity where Name in (select Company from Lead)
Where clause of your query will not work. You can use another query in where clause only for checking matching Id.
SELECT CreatedDate, LeadSource, RecordTypeId, Name__c, StageName, HubSpot_Original_Source_Data_1__c, Name, HubSpot_Original_Source_Data_2__c, HubSpot_Original_Source_Type__c, (SELECT Company, IsConverted, CreatedDate, HubSpot_Inc__HubSpot_Intelligence__c, HubSpot_Original_Source_Data_1__c, HubSpot_Original_Source_Data_2__c, HubSpot_Original_Source_Type__c, LeadSource, RecordTypeId FROM Lead) FROM Opportunity where name = 'test'

Salesforce SOQL Query to access a child field in WHERE statement

I am attempting to write a query on an object, Opportunity, this object has a child object Quotes.
In Quotes where have a field named, Order_Ready.
What I need to do is filter in all opportunities that have approved quotes (Order_Ready__c == true).
Here is the query I have been attempting to get working,
SELECT Id, Name (SELECT Order_Ready__c FROM Quotes) FROM Opportunity WHERE Opportunity.Quotes.Order_Ready__c = true
I have tried a few variations of this,
SELECT Id, Name (SELECT Order_Ready__c FROM Quotes) FROM Opportunity WHERE Quotes.Order_Ready__c = true
SELECT Id, Name (SELECT Order_Ready__c FROM Quotes) FROM Opportunity WHERE Order_Ready__c = true
I have to admit, I'm not the strongest with SQL/SOQL. Any insight into where my mistake or misunderstanding might be?
Thanks!
SELECT Id, Name FROM Opportunity WHERE Id IN
(Select OpportunityId FROM Quote WHERE Order_Ready__c = true)

Resources