How To Query Opportunity History Object in Salesforce - salesforce

We have opportunity History report type in salesforce Reports & Dashboard where we can use fields like "From Stage" & "To Stage". How can we use those two fields in SOQL? I tried but there is no such field on "Opportunity History" object. Is there any workaround for the same? (Need to use this in SOQL only no custom coding)

If you are looking for the From and To values, you need to query OpportunityFieldHistory.
SELECT CreatedDate, OldValue, NewValue
FROM OpportunityFieldHistory
WHERE opportunityid = 'yourid' AND Field = 'StageName'
ORDER BY createddate

Related

There are two date fields on an Object say Date1 & Date2,Now I want to Write a SOQL Query in SOQL Editor to fetch all the Object records

There are two date fields on an Object say Date1 & Date2, Now I want to Write a SOQL Query in SOQL Editor to fetch all the Object records where Date 2 is Greater than Date 1.
how you will do this using SOQL only?
Best way is we can create a Boolean/checkbox formula field on that objects which has below condition Date2_c > Date1_c
Then query the records where this formula field is true
i.e
SELECT Date1,date2 FROM object WHERE Date2_c >Date1_c

SOQL Query for Left Join for custom objects

I have a requirement to fetching data from Sales force. I need to get the data from two custom objects. I
have written query in sql can anyone help me to convert it into SOQL
SELECT ID, Name, Crop_Year__c, Targeted_Enrollment_Segments__c, Description__c, Start_Date__c,
End_Date__c from Enrollment_Program__c EP
Left Join Account_Enrollment__c AE on EP.Crop_Year__c = AE.Crop_Year__c and EP.ID =
AE.Enrollment_Program__c
where AE.Account__c = 'xyz'
As you probably know, Salesforce SOQL doesn't have explicit JOIN clauses. It does that for you implicitly based on related object fields. That means you'll have to query Account_Enrollment__c and traverse the fields to get the related Enrollment_Program__c Lookup relationship.
Another problem is Salesforce only performs joins based on primary and foreign keys, so the EP.Crop_Year__c = AE.Crop_Year__c in your query won't work.
So, with that said, you can try this:
SELECT Enrollment_Program__c, Enrollment_Program__e.Name,
Enrollment_Program__r.Crop_Year__c, Enrollment_Program__r.Targeted_Enrollment_Segments__c,
Enrollment_Program__r.Description__c, Enrollment_Program__r.Start_Date__c,
Enrollment_Program__r.End_Date__c
FROM Account_Entrollment_Program__c WHERE Account__c = 'zyz'
If you know beforehand what the Crop_Year__c value is, you can just add this to your query:
AND Crop_Year__c=:year AND Enrollment_Program__c.Crop_Year__c=:year
Some details on the queries:
The __r suffix is how you get the lookup object addressed in the query. If you are interested only in the id, you can use __c.
The :year is how you pass the parameter year to the query. If you want to append it as text you can just use ... Crop_Year='+ year + '.

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'

fetching picklist records from sales force using soql

I am trying to fetch picklist records from sales force using soql. My Table name is Industry__c its having a field called Industry_Group__c whose data type is picklist. How can I fetch the records of the Industry_group__c picklist.
I have tried the below queries but it did not work.
select Industry_Group__c from Industry__c
First get the DurableId from the FieldDefinition:
Select DurableId
From FieldDefinition
where EntityDefinition.DeveloperName='Industry__c'
and DataType = 'Picklist'
and DeveloperName = 'Industry_Group__c'
Then you can run the following query to get out the picklist values, replacing the EntityParticleId with the DurableId from the last query:
SELECT DurableId, EntityParticleId, IsActive, IsDefaultValue, Label, ValidFor, Value
FROM PicklistValueInfo
WHERE EntityParticleId = 'Account.00N1a00000ZZSAS'
Actually the field is not accessible to user .. Please check the field level security whether it has a read/Write access to system Administrator and then try to access it..

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);
}
}

Resources