+ Start a Discussion
santhosh konathala 17santhosh konathala 17 

Hi please help My apex code is well compiled.Here is my class calling through my trigger but functionality not executing in sandbox ?plz tell me where I did wrong?

My Trigger code:

Trigger insertNotification1 on Account(after insert,after update) {

Notificationserviceclass1 handler=new Notificationserviceclass1();
if(Trigger.isinsert ){
   handler.OnAfterInsert(Trigger.new);
}
if(Trigger.isupdate ){
    handler.OnAfterUpdate(Trigger.new,Trigger.oldMap);

         }}

My Apex class--

public  class NotificationServiceClass1 {

public void onAfterInsert (Account[] newAccounts){
     
        List<Notification__c> notifyInsertList = new list<Notification__c>();
        for(Account accname:newAccounts){
        If(userinfo.getusername() == 'Kanaka konathala')
          {
            Notification__c notify=new Notification__c();
            notify.name=accname.name;
            notify.Action__c='Add';
          notify.Test__c='Outbound';
          }
          else 
            If(userinfo.getusername() == 'reddy kon'){
            Notification__c notify12=new Notification__c();
            notify12.name=accname.name;
            notify12.Test__c='Inbound';
            notifyInsertList.add(notify12);
        }
        if(notifyInsertList.size()>0)
        {
        insert notifyInsertList;
        }
    }
    }
    
    public void onAfterUpdate(Account[] newAccounts, map<id, Account> oldMap ){
        List<Notification__c> notifyUpdateList = new list<Notification__c>();
        for(Account Acc:newAccounts){
            Account oldAcc = oldMap.get(Acc.Id);
            if(Acc.name!=oldAcc.name){
            If(userinfo.getusername() == 'Kanaka konathala'){
                Notification__c notifyUpdate = new Notification__c();
                notifyUpdate.name=Acc.name;
                notifyUpdate.Action__c= 'Modify';
              
                notifyUpdate.Test__c='Outbound';
                }
                else
                If(userinfo.getusername() == 'reddy kon'){
                Notification__c notifyUpdate1 = new Notification__c();
                
                notifyUpdate1.Test__c='Inbound';
                notifyUpdateList.add(notifyUpdate1);
            }
        }
        
        if(notifyUpdateList.size()>0 && notifyUpdateList.size()!=null){
            insert notifyUpdateList;
        }
        
    }
}
}
 
Best Answer chosen by santhosh konathala 17
sfdcMonkey.comsfdcMonkey.com
try this trigger 

Trigger :
 
Trigger insertNotification1 on Account(after insert,after update) {

Notificationserviceclass1 handler=new Notificationserviceclass1();
if(Trigger.isinsert ){
   handler.OnAfterInsert(Trigger.new);
}
if(Trigger.isupdate ){
    handler.OnAfterUpdate(Trigger.new,Trigger.oldMap);

         }
}
Apex class
 
public  class NotificationServiceClass1 {
 
public void onAfterInsert (Account[] newAccounts){
     
        List<Notification__c> notifyInsertList = new list<Notification__c>();
        for(Account accname:newAccounts){
        If(userinfo.getusername() == 'Kanaka konathala')
          {
            Notification__c notify=new Notification__c();
            notify.name=accname.name;
            notify.Action__c='Add';
            notify.Test__c='Outbound';
            notifyInsertList.add(notify);
          }
          else{ 
            If(userinfo.getusername() == 'reddy kon'){
            Notification__c notify12=new Notification__c();
            notify12.name=accname.name;
            notify12.Test__c='Inbound';
            notifyInsertList.add(notify12);
        } }
        if(notifyInsertList.size()>0)
        {
        insert notifyInsertList;
        }
    }
    }
    
    public void onAfterUpdate(Account[] newAccounts, map<id, Account> oldMap ){
        List<Notification__c> notifyUpdateList = new list<Notification__c>();
        for(Account Acc:newAccounts){
            Account oldAcc = oldMap.get(Acc.Id);
            if(Acc.name!=oldAcc.name){
            If(userinfo.getusername() == 'Kanaka konathala'){
                Notification__c notifyUpdate = new Notification__c();
                notifyUpdate.name=Acc.name;
                notifyUpdate.Action__c= 'Modify'; 
                notifyUpdate.Test__c='Outbound';
                notifyUpdateList.add(notifyUpdate );
                }
                else{
                If(userinfo.getusername() == 'reddy kon'){
                Notification__c notifyUpdate1 = new Notification__c();
                
                notifyUpdate1.Test__c='Inbound';
                notifyUpdateList.add(notifyUpdate1);
              }
            }
        }
        
        if(notifyUpdateList.size()>0 && notifyUpdateList.size()!=null){
            insert notifyUpdateList;
        }
        
    }
}
}
i hop its helps you :)
if it work let me inform and mark it best answer
Thanks

 

All Answers

vishnu Rvishnu R
hi,
try this......


Trigger :

Trigger insertNotification1 on Account(after insert,after update) {
NotificationServiceClass1 handler=new  NotificationServiceClass1();
if(Trigger.isinsert ){
   handler.onAfterInsert (Trigger.new);
}
if(Trigger.isupdate ){
    handler.onAfterUpdate(Trigger.new,Trigger.oldMap);

         }
}

Apex class--

public  class NotificationServiceClass1 {
public void onAfterInsert (Account[] newAccounts){
     
        List<Notification__c> notifyInsertList = new list<Notification__c>();
        for(Account accname:newAccounts){
        If(userinfo.getusername() == 'Kanaka konathala')
          {
            Notification__c notify=new Notification__c();
            notify.name=accname.name;
            notify.Action__c='Add';
          notify.Test__c='Outbound';
          }
          else 
            If(userinfo.getusername() == 'reddy kon'){
            Notification__c notify12=new Notification__c();
            notify12.name=accname.name;
            notify12.Test__c='Inbound';
            notifyInsertList.add(notify12);
        }
        if(notifyInsertList.size()>0)
        {
        insert notifyInsertList;
        }
    }
    }
    
    public void onAfterUpdate(Account[] newAccounts, map<id, Account> oldMap ){
        List<Notification__c> notifyUpdateList = new list<Notification__c>();
        for(Account Acc:newAccounts){
            Account oldAcc = oldMap.get(Acc.Id);
            if(Acc.name!=oldAcc.name){
            If(userinfo.getusername() == 'Kanaka konathala'){
                Notification__c notifyUpdate = new Notification__c();
                notifyUpdate.name=Acc.name;
                notifyUpdate.Action__c= 'Modify';
              
                notifyUpdate.Test__c='Outbound';
                }
                else
                If(userinfo.getusername() == 'reddy kon'){
                Notification__c notifyUpdate1 = new Notification__c();
                
                notifyUpdate1.Test__c='Inbound';
                notifyUpdateList.add(notifyUpdate1);
            }
        }
        
        if(notifyUpdateList.size()>0 && notifyUpdateList.size()!=null){
            insert notifyUpdateList;
        }
        
    }
}
}

hope this helps you..
thanks
santhosh konathala 17santhosh konathala 17
Hi vishnu,

Same problem getting..record not created in notication object once record created in account
sfdcMonkey.comsfdcMonkey.com
try this trigger 

Trigger :
 
Trigger insertNotification1 on Account(after insert,after update) {

Notificationserviceclass1 handler=new Notificationserviceclass1();
if(Trigger.isinsert ){
   handler.OnAfterInsert(Trigger.new);
}
if(Trigger.isupdate ){
    handler.OnAfterUpdate(Trigger.new,Trigger.oldMap);

         }
}
Apex class
 
public  class NotificationServiceClass1 {
 
public void onAfterInsert (Account[] newAccounts){
     
        List<Notification__c> notifyInsertList = new list<Notification__c>();
        for(Account accname:newAccounts){
        If(userinfo.getusername() == 'Kanaka konathala')
          {
            Notification__c notify=new Notification__c();
            notify.name=accname.name;
            notify.Action__c='Add';
            notify.Test__c='Outbound';
            notifyInsertList.add(notify);
          }
          else{ 
            If(userinfo.getusername() == 'reddy kon'){
            Notification__c notify12=new Notification__c();
            notify12.name=accname.name;
            notify12.Test__c='Inbound';
            notifyInsertList.add(notify12);
        } }
        if(notifyInsertList.size()>0)
        {
        insert notifyInsertList;
        }
    }
    }
    
    public void onAfterUpdate(Account[] newAccounts, map<id, Account> oldMap ){
        List<Notification__c> notifyUpdateList = new list<Notification__c>();
        for(Account Acc:newAccounts){
            Account oldAcc = oldMap.get(Acc.Id);
            if(Acc.name!=oldAcc.name){
            If(userinfo.getusername() == 'Kanaka konathala'){
                Notification__c notifyUpdate = new Notification__c();
                notifyUpdate.name=Acc.name;
                notifyUpdate.Action__c= 'Modify'; 
                notifyUpdate.Test__c='Outbound';
                notifyUpdateList.add(notifyUpdate );
                }
                else{
                If(userinfo.getusername() == 'reddy kon'){
                Notification__c notifyUpdate1 = new Notification__c();
                
                notifyUpdate1.Test__c='Inbound';
                notifyUpdateList.add(notifyUpdate1);
              }
            }
        }
        
        if(notifyUpdateList.size()>0 && notifyUpdateList.size()!=null){
            insert notifyUpdateList;
        }
        
    }
}
}
i hop its helps you :)
if it work let me inform and mark it best answer
Thanks

 
This was selected as the best answer
santhosh konathala 17santhosh konathala 17
No I am not able to create record in after update event in notification object.May I know what is the reason?Insert is working fine but update not working..
santhosh konathala 17santhosh konathala 17
Thanks Soni,
Your code is working fine ...Thanks for your your help
santhosh konathala 17santhosh konathala 17
Hi Soni,

I need an help .In above code if Account record is created, automatically that record goes into Notification Object.In the same way,if contact is created same record goes into Notification object.So for both Account and contact created ,Notification record should be created.So , for this what changes we need to do in Above Apex class .please help me ...
sfdcMonkey.comsfdcMonkey.com
hi santhosh
i think no need to changes in above code. you should create a new trigger on contact object on after insert of after update  as like above code change account object code with contact object :)