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);
}
}
Best Answer
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...
...and you could then call these:
As another example, you could have this...
...which both take one String and one Integer, but in different orders, and call these:
You could NOT define methods like this, though:
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 aList<2021_Scheme__c>
(which is whatTrigger.new()
is in this case).So (among other possible corrections) you would need to change the signature of your
CheckPreviousYearEntryByPhoneNumber
method to take aList<2021_Scheme__c>
, or create a new overloaded method with the same name that takes aList<2021_Scheme__c>
.