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
santhosh konathala 17santhosh konathala 17 

Hi Community I am new to Apex , can anybody call my Trigger through Apex class.Below is my code.

Trigger insertNotification on Account(after insert,after update) {
List<Notification__c> member1 = new list<Notification__c>();
List<Notification__c> member2 = new list<Notification__c>();
  if(Trigger.isinsert)
   {
   for(account acclist:Trigger.new)
   {
   Notification__c nr=new notification__c();
   nr.name=acclist.name;
   nr.Action__c='Add';
   member2.add(nr);
   }
   insert member2;
   }
    if(Trigger.isupdate)
   {
   
 for(Account member:Trigger.new)
{
Account oldacc = Trigger.oldMap.get(member.Id);
if(member.name!=oldacc.name)
{

       Notification__c memb = new Notification__c();
       memb.name=member.name;
       memb.Action__c= 'Modify';
       member1.add(memb);
       }
       if(member1.size()>0 && member1.size()!=null)
       {
       insert member1;
       }
       }
       }
       }
Best Answer chosen by santhosh konathala 17
sfdcMonkey.comsfdcMonkey.com
hi try this 
Trigger insertNotification on Account(after insert,after update) {
List<Notification__c> member1 = new list<Notification__c>();
List<Notification__c> member2 = new list<Notification__c>();
  if(Trigger.isinsert){
   handler.OnAfterInsert(Trigger.new);
   }
    if(Trigger.isupdate){
   
     handler.OnAfterUpdate(Trigger.new , Trigger.oldMap);
         }
       
}
 
public with sharing class AccountTriggerHandler {
  public void OnAfterInsert(Account[] newAccounts){
   for(account acclist:newAccounts)
     {
   Notification__c nr=new notification__c();
   nr.name=acclist.name;
   nr.Action__c='Add';
   member2.add(nr);
   }
   insert member2;
   
   }
  public void OnAfterUpdate(Account[] newAccounts, map<id, Account> oldMap ){

    for(Account member:newAccounts){
      Account oldacc = oldMap.get(member.Id);
     if(member.name!=oldacc.name){
       Notification__c memb = new Notification__c();
       memb.name=member.name;
       memb.Action__c= 'Modify';
       member1.add(memb);
       }
       if(member1.size()>0 && member1.size()!=null)
       {
       insert member1;
       }
       }  
	 }
	   }

Thanks :)

All Answers

sfdcMonkey.comsfdcMonkey.com
hi santosh 
your trigger is automatic call when you create or update a account record with you apex class 

first write a apex class for insert record 
public class AccountHandler {
    public static Account insertNewAccount(String name){
        Account a = new Account();
        a.Name = name;
       // or more requried fields in account object 
        try{
            insert a ;
        }catch (Exception e){
            return null;
        }
        return a ;
     }
  }

call this apex class from execute anonymous window from developer console 
open developer console ---> debug ---> open execute anonymous window and call your static class 

AccountHandler.insertNewAccount('dummy account1');

and click Execute button 
and then your account trigger automatic call :)
Thanks 
Mark it best answer if it helps you 



 
santhosh konathala 17santhosh konathala 17
plz help me the above trigger I called it through Apex class instead of writing code in Trigger.I will write the code in Apex class then my trigger need to be executed.
santhosh konathala 17santhosh konathala 17
Hi Soni ,

overall code must be in Apex class then from Apex class my Trigger need to be executed
santhosh konathala 17santhosh konathala 17
Hi Soni,

The above Apex class code is not compiling coming an error "Loop variable must be of type SObject "
sfdcMonkey.comsfdcMonkey.com
hi try this 
Trigger insertNotification on Account(after insert,after update) {
List<Notification__c> member1 = new list<Notification__c>();
List<Notification__c> member2 = new list<Notification__c>();
  if(Trigger.isinsert){
   handler.OnAfterInsert(Trigger.new);
   }
    if(Trigger.isupdate){
   
     handler.OnAfterUpdate(Trigger.new , Trigger.oldMap);
         }
       
}
 
public with sharing class AccountTriggerHandler {
  public void OnAfterInsert(Account[] newAccounts){
   for(account acclist:newAccounts)
     {
   Notification__c nr=new notification__c();
   nr.name=acclist.name;
   nr.Action__c='Add';
   member2.add(nr);
   }
   insert member2;
   
   }
  public void OnAfterUpdate(Account[] newAccounts, map<id, Account> oldMap ){

    for(Account member:newAccounts){
      Account oldacc = oldMap.get(member.Id);
     if(member.name!=oldacc.name){
       Notification__c memb = new Notification__c();
       memb.name=member.name;
       memb.Action__c= 'Modify';
       member1.add(memb);
       }
       if(member1.size()>0 && member1.size()!=null)
       {
       insert member1;
       }
       }  
	 }
	   }

Thanks :)
This was selected as the best answer
santhosh konathala 17santhosh konathala 17
Hi Soni ,
Apex class is fine but while executing Trigger I got an error"Variable does not exist: handler at line 9 "
santhosh konathala 17santhosh konathala 17
Hi Soni ,

A great Thanks to you. code is very well executing..