function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vwvw 

method does not exist or incorrect signature when calling public method

Hi there, I've created a public class called SponsorshipMaintenance that has a public method UpdateSponsorshipCountry. Looks like this:

public class SponsorshipMaintenance {
   
    public void UpdateSponsorshipCountry(List<Sponsorship__c> Sponsorships, List<Id> ChildIds){
          // .... some code here
   }

}

The class compiles with no errors. When I try to compile a trigger that has a call to the UpdateSponsorshipCountry method, I get the message "method does not exist or incorrect signature" on the line that calls the method. The trigger looks like this:

trigger SponsorshipSetCountry on Sponsorship__c (before insert, before update) {   
    //get list of sponsored child Ids of new sponsorships
    Id[] SponsoredKids = new Id[]{};
    for (Sponsorship__c spons : Trigger.new){
        SponsoredKids.add(spons.Child__c);   
    }
   
    //call method to set the correct country on the sponsorship objects
    SponsorshipMaintenance.UpdateSponsorshipCountry(Trigger.New,SponsoredKids); //this is the line that's generating the error. If I comment it out, the trigger compiles.
}

Any guidance would be most appreciated. Thank you!

Best Answer chosen by Admin (Salesforce Developers) 
claudioclaudio
To call your method like that, you need to make it a static method:

public static void UpdateSponsorshipCountry(List<Sponsorship__c> Sponsorships, List<Id> ChildIds) {
....
}

Or you need to create an instance of your class, like this:

//Create the instance
SponsorshipMaintenance sm = new SponsorshipMaintenance();
//Call the method
sm.UpdateSponsorshipCountry(Trigger.New,SponsoredKids);

All Answers

claudioclaudio
To call your method like that, you need to make it a static method:

public static void UpdateSponsorshipCountry(List<Sponsorship__c> Sponsorships, List<Id> ChildIds) {
....
}

Or you need to create an instance of your class, like this:

//Create the instance
SponsorshipMaintenance sm = new SponsorshipMaintenance();
//Call the method
sm.UpdateSponsorshipCountry(Trigger.New,SponsoredKids);
This was selected as the best answer
vwvw
Aha! Thanks Claudio, that did the trick. I appreciate your help!
Eva DeLoriolEva DeLoriol
This worked for me too.  Is there a reason we need to create an instance of the class?  

Thanks
shivam tripathi 13shivam tripathi 13

apex class
public static class property_class
{
    public static void applyDiscount(List<Property__c> NewHousesList)
{
        for(Property__c p :NewHousesList) {
            if(p.Type__c == 'vila'){ 
                p.Pricer__c = p.Pricer__c - (p.Pricer__c x 0.05);
                }
            }
    }
}



//TRIGGER
trigger discount_trigger on Property__c(Before Insert)
{
property_class.applyDiscount(trigger.new);
}

FACING SAME ISSUE MENTIONED ABOVE ,METHOD DOES NOT EXITS OR NO SIGNATURE .

PLEASE HELP
Rajesh KanojiaRajesh Kanojia
Hey @shivam tripathi 13,

have you fixed the issue? if yes please post the solution I am also dealing with same.
Rajesh KanojiaRajesh Kanojia
Hey @Shivan Tripathi 13 

try this code
public class property_class{
    public static void applyDiscount(List<Property__c> NewHousesList){
        for(Property__c p :NewHousesList){
            if(p.Type__c == 'vila'){ 
                p.Pricer__c = p.Pricer__c - (p.Pricer__c x 0.05);
            }
        }
    }
}



//TRIGGER
trigger discount_trigger on Property__c(Before Insert){
	property_class.applyDiscount(trigger.new);
}

In Apex you can't define a class as Static.
Asif Ali 49Asif Ali 49
AccountProcessor

public class AccountProcessor {
@future public static void countContacts(Set<id> setId){
List<Account> lstAccount = [select id,Number_of_Contacts__c ,
(select id from contacts ) from account where id in :setId ];
for( Account acc : lstAccount ) { List<Contact> lstCont = acc.contacts ;
acc.Number_of_Contacts__c = lstCont.size(); } update lstAccount;
}
}

----------------------------------------------------------
@IsTest
public class AccountProcessorTest {
public static testmethod void TestAccountProcessorTest() {
Account a = new Account(); a.Name = 'Test Account';
Insert a; Contact cont = New Contact();
cont.FirstName ='Bob';
cont.LastName ='Masters';
cont.AccountId = a.Id;
Insert cont; set<Id> setAccId = new Set<ID>();
setAccId.add(a.id);
Test.startTest();
AccountProcessor.countContacts(setAccId);
Test.stopTest();
Account ACC = [select Number_of_Contacts__c from Account where id = :a.id LIMIT 1];
System.assertEquals ( Integer.valueOf(ACC.Number_of_Contacts__c) ,1);
}
}
Elvin Callistus LopezElvin Callistus Lopez
Thanks a lot ! @claudio