Skip to content

Instantly share code, notes, and snippets.

@abilal82
Last active November 14, 2023 14:13
Show Gist options
  • Save abilal82/6b28faf628384abbcc3a55c0ce1e51fd to your computer and use it in GitHub Desktop.
Save abilal82/6b28faf628384abbcc3a55c0ce1e51fd to your computer and use it in GitHub Desktop.
APEX SCHEDULE - Written to figure out new or latest owner of the property from sales history of property fetched by the real estate agents in whole day. Reonomy API don't provide latest or new owner flag into sales history data.
public with sharing class SaleScheduler implements Schedulable {
String OldOwner = 'Old';
String NewOwner = 'New';
public void execute(SchedulableContext sc) {
Set<Id> propertyIds = new Set<Id>();
List<Property_Owner__c> updatedPropertyOwners = new List<Property_Owner__c>();
Map<Id, List<Sale__c>> mapOfSales = new Map<Id, List<Sale__c>>();
List<Sale__c> maxDateSales = new List<Sale__c>();
try {
List<Sale__c> sales = [SELECT Id, Property__c, CreatedDate, Sale_Date__c FROM Sale__c WHERE CreatedDate = TODAY];
System.debug('Sales :: ' + sales);
for (Sale__c sale : sales)
if (String.isNotBlank(sale.Property__c))
propertyIds.add(sale.Property__c);
for (Id propertyId : propertyIds) {
List<Sale__c> propertySales = new List<Sale__c>();
for (Sale__c sale : sales)
if (sale.Property__c == propertyId)
propertySales.add(sale);
mapOfSales.put(propertyId, propertySales);
System.debug('Sales List :: ' + propertySales);
}
for (Id key : mapOfSales.keySet()) {
List<Sale__c> salesByPropId = mapOfSales.get(key);
maxDateSales.add(getMaxDate(salesByPropId));
}
List<Property_Owner__c> propertyOwners = [
SELECT
Id,
Owners__c,
Property__c,
Property__r.Renomy_Property_id__c,
Owners__r.Name,
Property__r.Title_Sale_Date__c,
Ownership_Status__c,
Owners__r.Property_Id__c,
Owners__r.CreatedDate
FROM Property_Owner__c
WHERE Property__c IN :propertyIds AND CreatedDate = TODAY
];
System.debug('propertyOwners :: ' + propertyOwners);
for (Sale__c sale : maxDateSales) {
for (Property_Owner__c propertyOwner : propertyOwners)
if ((propertyOwner.Property__c == sale.Property__c) && (propertyOwner.Property__r.Title_Sale_Date__c == sale.Sale_Date__c))
updatedPropertyOwners.add(new Property_Owner__c(Id = propertyOwner.Id, Ownership_Status__c = NewOwner));
}
update updatedPropertyOwners;
updateOldPropertyOwners(propertyIds);
} catch (Exception ex) {
System.debug('Sale Schedule Exception :: ' + ex);
}
}
public Sale__c getMaxDate(List<Sale__c> sales) {
Sale__c maxDateSale = null;
for (Sale__c sale : sales)
if (maxDateSale == null || sale.Sale_Date__c > maxDateSale.Sale_Date__c)
maxDateSale = sale;
return maxDateSale;
}
public void updateOldPropertyOwners(Set<Id> propertyIds) {
List<Property_Owner__c> oldPropertyOwners = new List<Property_Owner__c>();
try {
List<Property_Owner__c> propertyOwners = [
SELECT
Id,
Owners__c,
Property__c,
Property__r.Renomy_Property_id__c,
Owners__r.Name,
Property__r.Title_Sale_Date__c,
Ownership_Status__c,
Owners__r.Property_Id__c,
Owners__r.CreatedDate
FROM Property_Owner__c
WHERE Property__c IN :propertyIds AND CreatedDate != TODAY
];
if (propertyOwners != null) {
for (Property_Owner__c propertyOwner : propertyOwners)
if (String.isBlank(propertyOwner.Ownership_Status__c) || propertyOwner.Ownership_Status__c == NewOwner)
oldPropertyOwners.add( new Property_Owner__c( Id = propertyOwner.Id, Ownership_Status__c = OldOwner));
}
if (oldPropertyOwners != null)
update oldPropertyOwners;
} catch (Exception ex) {
System.debug('updateOldPropertyOwners :: ' + ex);
}
}
}
/**
* String cronExp = '0 55 23 * * ?';
* SaleScheduler saleSchedular = new SaleScheduler();
* String jobId = System.schedule('Sale Record Schedule',cronExp,saleSchedular);
* if(String.isBlank(jobId))
* System.debug('Schedule Created - Job ID :: '+ jobId);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment