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 please help me..

Hi In below code if Account record is created,  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  what changes we need to do in below Apex class ..plz help anybody.
Apex class:


public  class NotificationServiceClass1 {

User varUser = [SELECT Id, Name FROM User WHERE Id =: UserInfo.getUserId()];

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

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);

         }}
sfdcMonkey.comsfdcMonkey.com
hi
create a new trigger on contact object 

Trigger insertNotification2 on Contact(after insert,after update) {

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

         }}

handler
public  class NotificationServiceClass2 {

User varUser = [SELECT Id, Name FROM User WHERE Id =: UserInfo.getUserId()];

public void onAfterInsert(Contact[] newContact){
     
        List<Notification__c> notifyInsertList = new list<Notification__c>();
        
        for(Contact conName:newContact){        
        If(varUser.name =='santhosh konathala')
          {
            Notification__c notify=new Notification__c();
            notify.name=conName.name;
            notify.Action__c='Add';
          notify.Test__c='Outbound';
          notifyInsertList.add(notify);
          
          }
          else If(varUser.name == 'reddy kon'){
            Notification__c notify=new Notification__c();
            notify.name=conName.name;
            notify.Test__c='Inbound';
            notify.Action__c='Add';
            notifyInsertList.add(notify);
        }
        }  
        insert notifyInsertList;
        }        
        
       public void onAfterUpdate(Contact[] newContact, map<id, Contact> oldMap ){
    
            List<Notification__c> notifyUpdateList = new list<Notification__c>();
            for(Contact conVar:newContact){
                    Contact oldcon = oldMap.get(conVar.Id);
                    if(oldcon.name!=conVar.name){
                        If(varUser.name =='santhosh konathala'){
                                Notification__c notifyUpdate = new Notification__c();
                                notifyUpdate.name=conVar.name;
                                notifyUpdate.Action__c= 'Modify';
                              
                                notifyUpdate.Test__c='Outbound';
                                  notifyUpdateList.add(notifyUpdate);
                            }
                            else If(varUser.name == 'reddy kon'){
                            
                            Notification__c notifyUpdate = new Notification__c();
                            notifyUpdate.name=conVar.name;
                            notifyUpdate.Test__c='Inbound';
                            notifyUpdate.Action__c= 'Modify';
                            notifyUpdateList.add(notifyUpdate);
                        }
                }
            }
            
            
            if(notifyUpdateList.size()>0 && notifyUpdateList.size()!=null){
                insert notifyUpdateList;
            }
            
        }
}

Thanks
Mark it best answer if it helps you :)

 
sfdcMonkey.comsfdcMonkey.com
your above trigger executed only for account object because it's write for account object so how we can map it with contact ?
sfdcMonkey.comsfdcMonkey.com
can you explen your all requirement Please?
sfdcMonkey.comsfdcMonkey.com
with one trigger its not possible as per my understanding
you can create 2 trigger one for account and one for contact and you can use one handler class for both trigger but trigger are created for per object
Thanks :)