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
JaanuJaanu 

Trigger Code Issue .. Help me pls

I am trying to call the class from trigger... getting the error 'Method does not exist or incorrect signature: void acctphoneupdate(List<Account>) from the type AccClass1'. Pls help in resolving this issue.

I have the following Class

public class AccClass1 {
           public static acctphoneupdate(List<Account> Acc) {
//        list<Account> Acct = [Select Phone from Account];
//            for ( Account Acc : Acct) {
                if (Acc.phone == '4001002000')
                {
                    Acc.phone = '4040404040';
                    insert Acc;
                }
//            }
    }
}

I have the trigger as below:

trigger Acctrigger1 on Account (before insert) {
    AccClass1 first = new AccClass1();
    first.acctphoneupdate(Trigger.new); <--- This is the line of code having issue. 
}
Best Answer chosen by Jaanu
Gustavo BertolinoGustavo Bertolino
You've passed a reference to list of new sObject records in your method (Trigger.new is a context variable which gives you such a list) and tried inserting new things on such list after selecting things from a query and put it in a list you created inside the method and interates over it with the for loop. This throws an error, I guess, because you're trying putting in new things on a list which has new records already.

All Answers

Raj VakatiRaj Vakati
Use this code
 
public class AccClass1 {
           public static void acctphoneupdate(List<Account> Acc) {
//        list<Account> Acct = [Select Phone from Account];
//            for ( Account Acc : Acct) {
                if (Acc.phone == '4001002000')
                {
                    Acc.phone = '4040404040';
                   // insert Acc;
                }
//            }
    }
}

trigger Acctrigger1 on Account (before insert) {
    AccClass1.acctphoneupdate(Trigger.new); 
}

 
SARVESH SHARMA 6SARVESH SHARMA 6
Your method is Static , You must call this using ClassName.MethodName()

AccClass1.acctphoneupdate() ;
Deepak Kumar SharmaDeepak Kumar Sharma
Hi, 
Please use the code below:-

public class AccClass1 {
public static void acctphoneupdate(List<Account> Acct) {
     for ( Account Acc : Acct) {
           if (Acc.phone == '4001002000') {
               Acc.phone = '4040404040';
           }  
    }
}
}
..........................................................
trigger Acctrigger1 on Account (before insert) {
    AccClass1.acctphoneupdate(Trigger.new);
}


Inside your code please use return type for the method and second you should call all static things via class name. Always remember you can call static things(Methods or variable) with in a class directly but if you gonna call any static things (Methods or variable) from another class just use class name e.g. ClassName.Method_Name();
or
Classname.variable_name;
If you get your solution please mark it as Solved or you may select it as best answere.
Happy Coding :)
Thanks & Regards,
Deepak
JaanuJaanu
Hello, Thanks ...Still the code issue is not resolved...
public class AccClass1 {
           public static void acctphoneupdate(Account Acc) {
                if (Acc.phone == '4001002000')
                {
                    Acc.phone = '4040404040';
                    Insert Acc;
                }
    }
}

trigger Acctrigger1 on Account (before insert) {
    AccClass1.acctphoneupdate(Trigger.new); ---> Error Message "Method does not exist or incorrect signature: void acctphoneupdate(List<Account>) from the type AccClass1"
}

What is wrong with the above code pls ?
 
Amit Chaudhary 8Amit Chaudhary 8
Try to update your code like below
trigger Acctrigger1 on Account (before insert) {
    AccClass1.acctphoneupdate(Trigger.new); 
}

Apex Class like below
public class AccClass1 
{
   public static acctphoneupdate(List<Account> Acc) 
   {
		for ( Account Acc : Acct) 
		{
			if (Acc.phone == '4001002000')
			{
				Acc.phone = '4040404040';
				//insert Acc; //  No need of this
			}
		}
	}	
}

Let us know if this will help you

 
Gustavo BertolinoGustavo Bertolino
In the first version of your code, you're trying to call the method on an instance/object's reference, which throws an error message because your method is defined as static and, for this reason, should be called only on class reference.
Gustavo BertolinoGustavo Bertolino
You've passed a reference to list of new sObject records in your method (Trigger.new is a context variable which gives you such a list) and tried inserting new things on such list after selecting things from a query and put it in a list you created inside the method and interates over it with the for loop. This throws an error, I guess, because you're trying putting in new things on a list which has new records already.
This was selected as the best answer
JaanuJaanu
Hi Gustavo,

 Thanks . Can you pls provide the code. thanks.
Amit Chaudhary 8Amit Chaudhary 8
update your apex class like below
 
public class AccClass1 
{
   public static acctphoneupdate(List<Account> Acct) 
   {
		for ( Account Acc : Acct) 
		{
			if (Acc.phone == '4001002000')
			{
				Acc.phone = '4040404040';
				//insert Acc; //  No need of this
			}
		}
	}	
}

Change public static acctphoneupdate(List<Account> Acct)
JaanuJaanu
It worked .. Thanks a lot.