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
Alejandro Ortega AsunAlejandro Ortega Asun 

how to call a class method in a trigger

Hi!
I'm blocked in an Apex field update in Case object.
On the one hand, I have a class that have 2 decimal variables that must have values depending on case fields:

Apex Class
public class HelloWorld{
         public static decimal helloWorld2(){
         decimal x;
         decimal y;
         case c = new Case;
                if(c.CustomField1__c == 'A'){x = 1}else{x=0}
                if(c.CustomField2__c == 'B'){y = 1}else{y=0}
                c.CustomField3__c = x+y;
}
}

On the other hand, I have a trigger that calls the class method:

Trigger
trigger CaseTrigger on Case (before insert, before update, after insert) {
    if (Trigger.isBefore) {
        if (Trigger.isInsert) {
              HelloWorld.helloWorld2( Trigger.new );
        }}}

With this code I have an error like "Method does not exist or incorrect signature: void helloWorld2(List<Case>) from the type HelloWorld"

I'm new in Apex and I will be appreciated if someone can give any help.

Regards,

Alex.
Best Answer chosen by Alejandro Ortega Asun
Raj VakatiRaj Vakati
My bad .. No need of return statement .. use this code pls
 
public class HelloWorld{
         public static void helloWorld2(List<Case> cases ){
         
		 for( Case c :cases){
			 decimal x;
             decimal y;
			  if(c.CustomField1__c == 'A'){
				  x = 1;
				  }else{
					  x=0;
				}
                if(c.CustomField2__c == 'B')
				{y = 1;
			}else{y=0;}
			
                c.CustomField3__c = x+y;
			 
		 }
          
               
}
}

And use this trigger
 
trigger CaseTrigger on Case (before insert, before update) {
    if (Trigger.isBefore) {
        if (Trigger.isInsert) {
              HelloWorld.helloWorld2( Trigger.new );
        }
}
}

 

All Answers

Raj VakatiRaj Vakati
Change like this 
 
trigger CaseTrigger on Case (before insert, before update, after insert) {
    if (Trigger.isBefore) {
        if (Trigger.isInsert) {
              HelloWorld.helloWorld2( Trigger.new );
        }
}
}

Apex CLass
 
public class HelloWorld{
         public static decimal helloWorld2(List<Case> cases ){
         decimal x;
         decimal y;
         case c = new Case;
                if(c.CustomField1__c == 'A'){x = 1}else{x=0}
                if(c.CustomField2__c == 'B'){y = 1}else{y=0}
                c.CustomField3__c = x+y;
}
}

 
Alejandro Ortega AsunAlejandro Ortega Asun
Lots of thanks Raj, that compiles fine!
But there is no any calculous in "CustomField3__c", the field before inserting a new case is empty
Raj VakatiRaj Vakati
Use this
 
public class HelloWorld{
         public static decimal helloWorld2(List<Case> cases ){
         
		 for( Case c :cases){
			 decimal x;
             decimal y;
			  if(c.CustomField1__c == 'A'){
				  x = 1;
				  }else{
					  x=0;
				}
                if(c.CustomField2__c == 'B')
				{y = 1;
			}else{y=0;}
			
                c.CustomField3__c = x+y;
			 
		 }
          
               
}
}

 
Alejandro Ortega AsunAlejandro Ortega Asun
Should I use a return statement? I'm getting this error: "Missing return statement required return type: Decimal"
Raj VakatiRaj Vakati
My bad .. No need of return statement .. use this code pls
 
public class HelloWorld{
         public static void helloWorld2(List<Case> cases ){
         
		 for( Case c :cases){
			 decimal x;
             decimal y;
			  if(c.CustomField1__c == 'A'){
				  x = 1;
				  }else{
					  x=0;
				}
                if(c.CustomField2__c == 'B')
				{y = 1;
			}else{y=0;}
			
                c.CustomField3__c = x+y;
			 
		 }
          
               
}
}

And use this trigger
 
trigger CaseTrigger on Case (before insert, before update) {
    if (Trigger.isBefore) {
        if (Trigger.isInsert) {
              HelloWorld.helloWorld2( Trigger.new );
        }
}
}

 
This was selected as the best answer
Alejandro Ortega AsunAlejandro Ortega Asun
Top!! that works. Too much thanks!
Regards,