function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Yetian XiaYetian Xia 

How to get Leads whose status just changed from A to B?

Hi,

We want to use SOQL to get all Leads whose status changed from A to B in a time window. My original idea is query LeadHistory and filter by OldValue and NewValue.
 
select fields(standard),Lead.Id, Lead.something from LeadHistory where Field='Status' and OldValue='Working - Contacted' and NewValue='Closed - Converted'
And then the query failed and I realized that OldValue and NewValue are not filterable.

The only workaround I can figure it out is to only condition on Field and use our downstream tasks to filter OldValue and NewValue.
select fields(standard),Lead.Id, Lead.something from LeadHistory where Field='Status'
But if a Lead changes status a couple times in the time window, the downstream tasks will see the same Lead multiple times. And it adds more burden on us to generalize the solution to more objects (such as Oppurtunity vs. OppurtunityFieldHistory).

I wonder if there is an eaiser way to get all Leads whose status just changed from A to B through SOQL?

Thank you!
AnkaiahAnkaiah (Salesforce Developers) 
Hi Yetina,

You can generate a report on Leadhistory object. it will be easy to identify the new values & old vaules for the specific object.

User-added image

Thanks!!
 
Robert A. EliasRobert A. Elias
 think my question wasn't as clear as it could have been: Does my company need a "Sandbox (Developer Pro)" subscription in order to create connected apps that are able to interact with said organization's SalesForce account? 
Dinar Detective News (https://www.dinarguru.onl/)
Yetian XiaYetian Xia
Hi Ankaiah,

Unforturnately, we have to use SOQL to query Leads and other objects. Is there a way/workaround to do that purely through SOQL?

Thanks!