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
sathishsfdcsathishsfdc 

trigger to insert data into custom object from another object thru apex class

Hi,
I am trying to insert data into obj2 based on some condition in obj1 through handler /apex class but it is not working.
Please find the code
trigger orderAccountTransactionUpdate on Account (before update) {

 Order__c  orderAccount = new Order__c();

 system.debug('inside trigger');

orderAccount.lstNewAccounts = Trigger.new;
orderAccount.lstOldAccounts = Trigger.old;

// Trigger Logic – Before Update

    if((Trigger.isAfter && Trigger.isUpdate) ) {
 
  orderAccount.check(Trigger.new, Trigger.oldMap);

    }
    
  }
  
  
  public class OrderAccountTransaction {

    public List<Account> lstNewAccounts = new List<Account>();
    public List<Account> lstOldAccounts = new List<Account>();
    
    public void check(List<Account> Accounts, Map<Id, Account> oldAccount) {
  
    Map<Id, Account> mapVerifyOldAccounts = new Map<Id, Account>();
    List<Order__c> createOrder = new List <Order__c> ();
    
       for(Account acc : Accounts){
			system.debug('Accounts' + Accounts);
			system.debug('oldAccount' + oldAccount);
          Account beforeUpdate = oldAccount.get(acc.Id);
		   
          if(acc.Email!=beforeUpdate.Email) {
		  
		  NOT GOING INSIDE THIS
          
          system.debug('inside if');
              createOrder.add(new Order__c (Action__c  = 'insert',Status__c  = 'success'));
            }                             
    }
    try {
        insert createOrder;
    }
    catch (Exception Ex){
        system.debug(Ex);
    }


}

}



 
Best Answer chosen by sathishsfdc
ManojjenaManojjena
Hi Satish,
Try with belwo code let me know if it helps !!
 
trigger orderAccountTransactionUpdate on Account (before update) {
	if((Trigger.isBefore && Trigger.isUpdate) ) {
		OrderAccountTransaction.check(Trigger.new, Trigger.oldMap);
	}
}

public class OrderAccountTransaction {
	public static void check(List<Account> accList, Map<Id, Account> oldAccount) {
		List<Order__c> createOrder = new List <Order__c> ();
		for(Account acc : accList){
			if(acc.Email!=oldAccount.get(acc.Id).Email) {
				createOrder.add(new Order__c (Action__c  = 'insert',Status__c  = 'success'));
			}                             
		}
		if(!createOrder.isEmpty())
			try {
				insert createOrder;
			}
			catch (Exception Ex){
				system.debug(Ex);
			}
		}
    }
}


Thanks
Manoj

All Answers

Gaurav_SrivastavaGaurav_Srivastava
Hi

Try below code.
 
trigger orderAccountTransactionUpdate on Account (before update) {

 OrderAccountTransaction  orderAccount = new OrderAccountTransaction();

 system.debug('inside trigger');

orderAccount.lstNewAccounts = Trigger.new;
orderAccount.lstOldAccounts = Trigger.old;

// Trigger Logic – Before Update

    if((Trigger.isAfter && Trigger.isUpdate) ) {
 
  orderAccount.check(Trigger.new, Trigger.oldMap);

    }
    
  }
 
 
  public class OrderAccountTransaction {

    public List<Account> lstNewAccounts = new List<Account>();
    public List<Account> lstOldAccounts = new List<Account>();
    
    public void check(List<Account> Accounts, Map<Id, Account> oldAccount) {
 
    Map<Id, Account> mapVerifyOldAccounts = new Map<Id, Account>();
    List<Order__c> createOrder = new List <Order__c> ();
    
       for(Account acc : Accounts){
            system.debug('Accounts' + Accounts);
            system.debug('oldAccount' + oldAccount);
          Account beforeUpdate = oldAccount.get(acc.Id);
           
          if(acc.Email!=beforeUpdate.Email) {
          
          NOT GOING INSIDE THIS
          
          system.debug('inside if');
              createOrder.add(new Order__c (Action__c  = 'insert',Status__c  = 'success'));
            }                             
    }
    try {
        insert createOrder;
    }
    catch (Exception Ex){
        system.debug(Ex);
    }


}

}
Thanks,
Gaurav
ManojjenaManojjena
Hi Satish,
Try with belwo code let me know if it helps !!
 
trigger orderAccountTransactionUpdate on Account (before update) {
	if((Trigger.isBefore && Trigger.isUpdate) ) {
		OrderAccountTransaction.check(Trigger.new, Trigger.oldMap);
	}
}

public class OrderAccountTransaction {
	public static void check(List<Account> accList, Map<Id, Account> oldAccount) {
		List<Order__c> createOrder = new List <Order__c> ();
		for(Account acc : accList){
			if(acc.Email!=oldAccount.get(acc.Id).Email) {
				createOrder.add(new Order__c (Action__c  = 'insert',Status__c  = 'success'));
			}                             
		}
		if(!createOrder.isEmpty())
			try {
				insert createOrder;
			}
			catch (Exception Ex){
				system.debug(Ex);
			}
		}
    }
}


Thanks
Manoj
This was selected as the best answer