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
krish123krish123 

Error : unexpected token public

Hi ...

 

I am new to sfdc ....please help me this issus

 

 

trigger HelloWorld on Account (before insert , before update) {
List<Account> aaa=Trigger.new;
MyHelloWorld my=new MyHelloWorld();
my.addHelloWorld(aaa);
}
public class MyHelloWorld{  ------------------the error show msg like  Unexpected token : public
Public void addHelloWorld( List <Account> aaa){
for(Account acc:aaa){
if(acc.Hello__c!='World'){
acc.Hello__c='World';
}
}
}
}

Best Answer chosen by krish123
zachbarkleyzachbarkley

Hi kirsh123,

 

1. Create your trigger handler class by going to Your Name | Setup | Apex Classes | New

 

public class Account_HDL_BIBU{
    public void OnInsert(List<Account> upRec,List<Account> oldRec){
        updateRecord(upRec,oldRec); 
    }      
    public void OnUpdate(List<Account> upRec,List<Account> oldRec){
        updateRecord(upRec,oldRec); 
    }  
    private void updateRecord(List<Account> upRec,List<Account> oldRec) {
      	for(Account R:upRec){
                if(R.Hello__c!='World'){
			R.Hello__c='World';
		}
      	}
    }
}

 

2. Create your trigger by going to Your Name | Setup | Customize | Accounts | Triggers

 

trigger Account_BIBU on Account (before insert, before update) {
    Account_HDL_BIBU hdl = new Account_HDL_BIBU();    
    if(Trigger.isInsert && Trigger.isBefore) {
        hdl.OnInsert(Trigger.new,Trigger.old);
    }else if(Trigger.isUpdate && Trigger.isBefore) { 
        hdl.OnUpdate(Trigger.new,Trigger.old);
    }
}

 

All Answers

zachbarkleyzachbarkley

Hi kirsh123,

 

I don't think you can have a class in a trigger.

 

Why don't you check out the solution on this page which provides you an exmaple of a trigger class, a handler class, and also a test class for your trigger. Be sure to use your own object names and fields.

 

Let me know if you are still having difficulties :-)

krish123krish123

Hi Zachbarkley ...Thanku for reply

 

But I am not geting your point so please tell me step by step 

zachbarkleyzachbarkley

Hi kirsh123,

 

1. Create your trigger handler class by going to Your Name | Setup | Apex Classes | New

 

public class Account_HDL_BIBU{
    public void OnInsert(List<Account> upRec,List<Account> oldRec){
        updateRecord(upRec,oldRec); 
    }      
    public void OnUpdate(List<Account> upRec,List<Account> oldRec){
        updateRecord(upRec,oldRec); 
    }  
    private void updateRecord(List<Account> upRec,List<Account> oldRec) {
      	for(Account R:upRec){
                if(R.Hello__c!='World'){
			R.Hello__c='World';
		}
      	}
    }
}

 

2. Create your trigger by going to Your Name | Setup | Customize | Accounts | Triggers

 

trigger Account_BIBU on Account (before insert, before update) {
    Account_HDL_BIBU hdl = new Account_HDL_BIBU();    
    if(Trigger.isInsert && Trigger.isBefore) {
        hdl.OnInsert(Trigger.new,Trigger.old);
    }else if(Trigger.isUpdate && Trigger.isBefore) { 
        hdl.OnUpdate(Trigger.new,Trigger.old);
    }
}

 

This was selected as the best answer