2

Problem statement:

If the customer phone number was enrolled in free water scheme for 2020, status field in 2021 scheme object should ''You cannot avail this scheme in 2021, as you have already received free water in 2020'.

Error on Trigger:

Method does not exist or incorrect signature: void CheckPreviousYearEntryByPhoneNumber(list) from type FreeWaterSchemeClass

APEX CLASS:

public class FreeWaterSchemeClass{
    public static list<2021_Scheme__c> 2021SchemeList = [SELECT Phone__c FROM 2021_Scheme__c];
    public static list<2020_Scheme__c> 2020SchemeList = [SELECT Phone__c FROM 2020_Scheme__c];
    public static set<string> 2020_Scheme_Set = new set<string>();

    // function to check if the customer has been enrolled in the 2020 scheme, before data entry into 2021 scheme.    
    public static void CheckPreviousYearEntryByPhoneNumber()
    {
        for (2020_Scheme__c s: 2020SchemeList)
        {
            2020_Scheme_Set.add(s.Phone__c);
        }

        //if the phone number is found 
        for(2021_Scheme__c s : 2021SchemeList)
        {
            if(2020_Scheme_Set.contains(s.Phone__c))
            {
                s.Status = 'You cannot avail this scheme in 2021, as you have already received free water in 2020'; 
            }
            else
            {
                s.Status = 'You are eligible for free water in 2021.'; 
            }
        }
    }
}

TRIGGER:

trigger 2021FreeWater_Trigger on 2021_Scheme__c (before insert) {
    if(Trigger.IsInsert == true)
    {
        FreeWaterSchemeClass.CheckPreviousYearEntryByPhoneNumber(Trigger.new);
    }
}
2
  • 1
    In this CheckPreviousYearEntryByPhoneNumber method you are not allowing any parameters, But in Trigger you are passing List as parameter. Can you change it and check. Dec 21, 2021 at 14:22
  • It works perfectly now, SaiPraveen. I have now understood my mistake of not passing the 2021SchemeList into the method.
    – vjVik
    Dec 21, 2021 at 15:30

1 Answer 1

4

Apex allows you to overload methods, which means they all have the same name but take different parameters.

"Different" in this case means either a different number of parameters and/or different types of parameters - in a different order.

For example you could have this...

public class DoSomethingClass {
   public static String doSomethingMethod() {
      return 'I did something with nothing.';
   }

   public static String doSomethingMethod(Integer someNum) {
      return 'I did something with ' + String.valueOf(someNum);
   }
}

...and you could then call these:

String result = DoSomethingClass.doSomethingMethod();
System.debug('result: ' + result);
>> result: I did something with nothing.

String result = DoSomethingClass.doSomethingMethod(42);
System.debug('result: ' + result);
>> result: I did something with 42.

As another example, you could have this...

public class DoSomethingClass {
    public static String doSomethingMethod(Integer someNum, String someString) {
        return String.valueOf(someNum) + ' ' + someString;
    }
    
    public static String doSomethingmethod(String someString, Integer someNum) {
        return someString + ' ' + String.valueOf(someNum);
    }
}

...which both take one String and one Integer, but in different orders, and call these:

String result = DoSomethingClass .doSomethingMethod(42, 'then this string');
System.debug('result: ' + result);
>> result: 42 then this string

result = DoSomethingClass .doSomethingmethod('First this string, then', 42);
System.debug('result: ' + result);
>> result: First this string, then 42

You could NOT define methods like this, though:

// NOT ALLOWED //
public class DoSomethingClass {
   public static String doSomethingMethod(Integer someNum) {
      return 'I did something with ' + String.valueOf(someNum);
   }

   public static Integer doSomethingMethod(Integer someNum) {
      return someNum + someNum;
   }
}

Even though they have different return types, their signature (number & type of parameters in order) is the same.


In your case your one CheckPreviousYearEntryByPhoneNumber method has a signature of taking no parameters. But as @SaiPraveenKakkirala said in a comment, you are calling it with arguments. Your argument is a List<2021_Scheme__c> (which is what Trigger.new() is in this case).

So (among other possible corrections) you would need to change the signature of your CheckPreviousYearEntryByPhoneNumber method to take a List<2021_Scheme__c>, or create a new overloaded method with the same name that takes a List<2021_Scheme__c>.

1
  • Works perfectly, Moonpie. Thanks to you and Sai for making me understand the reason for passing parameters in the method.
    – vjVik
    Dec 21, 2021 at 15:32

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.