Created
October 12, 2015 18:37
-
-
Save jsullivanlive/f3a483f7a62e52aa523d to your computer and use it in GitHub Desktop.
Update date field based on lead history using anonymous apex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Lead[] leads = [ | |
select Id, | |
(select Id, Field, OldValue, NewValue, CreatedDate from Histories order by CreatedDate asc) | |
from Lead | |
where Status='Closed' | |
and Closed_Date__c = null | |
order by LastModifiedDate asc | |
limit 1000 | |
]; | |
Lead[] toUpdate = new Lead[]{}; | |
for (Lead l : leads) { | |
for (LeadHistory lh : l.Histories) { | |
System.debug('****** ' + lh); | |
if (lh.Field == 'Status' && lh.NewValue == 'Closed') { | |
l.Closed_Date__c = Date.valueOf(lh.CreatedDate); | |
toUpdate.add( | |
new Lead( | |
Id = l.Id, | |
Closed_Date__c=Date.valueOf(lh.CreatedDate) | |
) | |
); | |
} | |
} | |
} | |
System.debug(toUpdate); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment