2

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; } } 
3
  • What is written on line 18? Commented Jan 19, 2017 at 4:04
  • this line: List<Revenue_Pipeline__c> filtered = filterUpdate(newMap, oldMap); Commented Jan 19, 2017 at 4:05
  • your filterUpdate method is returning void Commented Jan 19, 2017 at 4:05

3 Answers 3

2

You need to add return keyword at end also replace the void with the type you are returning here it is List<Revenue_Pipeline__c>:

public static List<Revenue_Pipeline__c> 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); return revPipeline; } 

Make sure the return variable type and the return type (we write in place of void) must be same.

4

You need to change the return type on your filter method from void to List<Revenue_Pipeline__c>.

No Joy

public static void myfilterMethod(...) { List<Revenue_Pipeline__c> filtered = new List<Revenue_Pipeline__c>(); // filter logic } 

Joy

public static List<Revenue_Pipeline__c> myfilterMethod(...) { List<Revenue_Pipeline__c> filtered = new List<Revenue_Pipeline__c>(); // filter logic return filtered; } 
4
  • Looking at the methods is this separated enough to test thoroughly? Commented Jan 19, 2017 at 4:19
  • I would separate out the error handling on insert into a safeInsert method. Also note that there is no reason to skip DML on empty lists. Commented Jan 19, 2017 at 4:21
  • What? what do you mean? Commented Jan 19, 2017 at 4:24
  • If you split out your try/catch logic into a separate method, it is much easier to induce a DmlException. Commented Jan 19, 2017 at 4:25
2

You need to modify the below method

public static List<Revenue_Pipeline__c> 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); return revPipeline; } 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.