1

I have a handler class that processes all actions from a custom object trigger. In the class I have a method for after insert and another method for after update. When a record is inserted I have a method to create new records that works great. When a record is updated I want to have some logic check if there were changes and then do some actions. Then go and create new records, it doesn't make sense to write two identical sections for creating new records so I want to reuse the same method. I don't know what the wording is for a google search so how do you pass data from one method to another within the same class?

Example:

public override void afterInsert(Map<Id, SObject> newMap){
  createRecords(newMap);
 }
public override void afterUpdate(Map<Id, SObject> newMap){
  checkRecords(newMap);
 }

public void createRecords(Map<Id, SObject> newMap){
  // some logic
  insert records;
 }
public void checkRecords(Map<Id, SObject> newMap, oldMap){
  // logic checks changes between newMap oldMap
  // If true send to delete method else nothing
  deleteRecords();
 }
public void deleteRecords(Map<Id, Sobject> newMap){
  // get record id from checkRecords
  delete records;
  // send to createRecords method to create new records
  createRecords();
 }

When I write the checkRecords method and try to call the deleteRecords it says

Method does not exist or incorrect signature: deleteSchedule(List Revenue_Pipeline__c>)

public void checkUpdate(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.Opportunity__r.Probability  != oldMap.get(revPipe.Id).Opportunity__r.Probability)
            {
                revPipeline.add(revPipe);
            }
    }
    deleteSchedule(revPipeline);
}

// Delete Revenue_Pipeline_Schedule records 
public void deleteSchedule(Map<Id, Revenue_Pipeline_Schedule__c> revPipeline)
{
}
4

Your signature is wrong.

deleteSchedule needs a Map<Id, Revenue_Pipeline_Schedule__c> and you're passing it a List<Revenue_Pipeline__c>.

Turn that List into a Map and you'll be in business

Sample code:

public void checkUpdate(Map<Id, Revenue_Pipeline__c> newMap, Map<Id, Revenue_Pipeline__c> oldMap)
{
    Map<Id, Revenue_Pipeline__c> revPipeline = new Map<Id, 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.Opportunity__r.Probability  != oldMap.get(revPipe.Id).Opportunity__r.Probability)
            {
                revPipeline.put(revPipe.id, revPipe);
            }
    }
    deleteSchedule(revPipeline);
}
  • Or better yet, accept a List parameter instead. – Adrian Larson Nov 7 '16 at 19:32
  • I don't know if he needs to run map-related operations.... I assumed he did. – Sebastian Kessel Nov 7 '16 at 19:33
  • I'm trying this whole separate my logic into testable methods so for this one I'm just going to delete records thus I just need the Ids that met the criteria, so would that be a set<id>? – Dan Wooding Nov 7 '16 at 19:35
  • If you just need to delete, then a List is probably enough. The Map is good when you want to ensure there are no dupes (which in this case won't be a problem) or you need access the records by their id. (There are other cases, but are not germane to what I see you doing here) – Sebastian Kessel Nov 7 '16 at 19:37
  • Either way, the signature in your method and the type of the parameter you pass need to be the same – Sebastian Kessel Nov 7 '16 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.