Skip to content

Instantly share code, notes, and snippets.

@jsullivanlive
Created October 12, 2015 18:37
Show Gist options
  • Save jsullivanlive/f3a483f7a62e52aa523d to your computer and use it in GitHub Desktop.
Save jsullivanlive/f3a483f7a62e52aa523d to your computer and use it in GitHub Desktop.
Update date field based on lead history using anonymous apex
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