I got advice earlier today on how to separate my handler class's concerns so that I can test them better.
I cannot get the class to compile and it's killing me inside. The idea is that when a record is edited the there is a method to filter the records and then another method to do things with that filtered list.
When I try to save I get the error on this line:
List<Revenue_Pipeline__c> filtered = filterUpdate(newMap, oldMap);
Illegal assignment from void to List Line 18.
This is what I have thus far, can you help me understand how I can pass the value of the query to the list so I can pass the values to the separate methods?
public override void afterInsert(Map<Id, SObject> newMapGeneric){
Map<Id, Revenue_Pipeline__c> newMap = (Map<Id, Revenue_Pipeline__c>) newMapGeneric;
/*AFTER INSERT METHODS START*/
if(!isRecursive)
{
isRecursive = true;
createRevSchedule(newMap);
}
/*AFTER INSERT METHODS END*/
}
public override void afterUpdate(Map<Id, SObject> oldMapGeneric, Map<Id, SObject> newMapGeneric){
Map<Id, Revenue_Pipeline__c> oldMap = (Map<Id, Revenue_Pipeline__c>) oldMapGeneric;
Map<Id, Revenue_Pipeline__c> newMap = (Map<Id, Revenue_Pipeline__c>) newMapGeneric;
/*AFTER UPDATE METHODS START*/
if(!isRecursive)
{
isRecursive = true;
List<Revenue_Pipeline__c> filtered = filterUpdate(newMap, oldMap);
System.debug('filtered is:: ' + filtered);
deleteSchedule(filtered);
createScheduvoidle(filtered);
}
public static void filterUpdate(Map<Id, Revenue_Pipeline__c> newMap, Map<Id, Revenue_Pipeline__c> oldMap)
{
List<Revenue_Pipeline__c> revPipeline = new List<Revenue_Pipeline__c>();
// Check if the fields changed
for(Revenue_Pipeline__c revPipe : newMap.values())
{
if(
revPipe.Product_Start_Date__c != oldMap.get(revPipe.Id).Product_Start_Date__c ||
revPipe.Product_End_Date__c != oldMap.get(revPipe.Id).Product_End_Date__c ||
revPipe.Product_Price__c != oldMap.get(revPipe.Id).Product_Price__c ||
revPipe.Weighted_Product_Price__c != oldMap.get(revPipe.Id).Weighted_Product_Price__c
)
{
revPipeline.add(revPipe);
}
}
System.debug('revPipeline is::' + revPipeline);
}
public static void createRevSchedule(Map<Id, Revenue_Pipeline__c> newMap)
{
List<Revenue_Pipeline_Schedule__c> newSchedule = new List<Revenue_Pipeline_Schedule__c>();
for(Revenue_Pipeline__c revPipe : newMap.values()) {
Integer numOfMonths = revPipe.Product_Start_Date__c.monthsBetween(revPipe.Product_End_Date__c) + 1;
Integer n = revPipe.Subscription_Type__c == 'Subscription' ? 1 : 0;
for(Integer i=0; i < numOfMonths; i++) {
System.debug('Weighted Product Price is:: ' + revPipe.Weighted_Product_Price__c);
Revenue_Pipeline_Schedule__c revSchedule = new Revenue_Pipeline_Schedule__c();
revSchedule.Amount__c = revPipe.Weighted_Product_Price__c/numOfMonths;
revSchedule.Date__c = revPipe.Product_Start_Date__c.addMonths(i + n);
revSchedule.Name = revPipe.Name + ' - ' + revSchedule.Date__c.year() + '-' + revSchedule.Date__c.month();
revSchedule.Revenue_Pipeline__c = revPipe.Id;
revSchedule.CurrencyIsoCode = revPipe.CurrencyIsoCode;
newSchedule.add(revSchedule);
}
}
if(newSchedule.size() > 0){
try{
insert newSchedule;
} catch (DmlException d)
{
for (Integer i = 0; i < d.getNumDml(); i++)
{
Id parentId = newSchedule[i].Id;
//parentId.addError(d);
}
}
}
}
public static void createSchedule(List<Revenue_Pipeline__c> filtered)
{
List<Revenue_Pipeline_Schedule__c> newSchedule = new List<Revenue_Pipeline_Schedule__c>();
for(Revenue_Pipeline__c revPipe : filtered) {
Integer numOfMonths = revPipe.Product_Start_Date__c.monthsBetween(revPipe.Product_End_Date__c) + 1;
Integer n = revPipe.Subscription_Type__c == 'Subscription' ? 1 : 0;
for(Integer i=0; i < numOfMonths; i++) {
System.debug('Weighted Product Price is:: ' + revPipe.Weighted_Product_Price__c);
Revenue_Pipeline_Schedule__c revSchedule = new Revenue_Pipeline_Schedule__c();
revSchedule.Amount__c = revPipe.Weighted_Product_Price__c/numOfMonths;
revSchedule.Date__c = revPipe.Product_Start_Date__c.addMonths(i + n);
revSchedule.Name = revPipe.Name + ' - ' + revSchedule.Date__c.year() + '-' + revSchedule.Date__c.month();
revSchedule.Revenue_Pipeline__c = revPipe.Id;
revSchedule.CurrencyIsoCode = revPipe.CurrencyIsoCode;
newSchedule.add(revSchedule);
}
}
if(newSchedule.size() > 0){
try{
insert newSchedule;
} catch (DmlException d)
{
for (Integer i = 0; i < d.getNumDml(); i++)
{
Id parentId = newSchedule[i].Id;
//parentId.addError(d);
}
}
}
}
// Delete Revenue Pipleine Schedule Records
public static void deleteSchedule(List<Revenue_Pipeline__c> filtered)
{
List<Revenue_Pipeline_Schedule__c> revPipeS = [SELECT Id,
Revenue_Pipeline__r.Id
FROM Revenue_Pipeline_Schedule__c
WHERE Revenue_Pipeline__r.Id In : filtered];
delete revPipeS;
}
}