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 compare old vs new values in account and insert a record in custom oject

Hi all,
i have created a trigger on account object and i need to insert a record into another custom object<
sales__c> based on whether the account field email has changed to a new value from old value.
This is my code. which is not working.can someone let me know where i m going wrong??
trigger CreateSalesAccount on Account  (after update) {


    List<Sales__c> createSales = new List <Sales__c> ();
    for (Account acc : trigger.new) {
       Account  oldAccount = Trigger.oldMap.get(acc.Id);
       String oldEmail = oldAccount.emailaddress__c ;
       String newEmail = acc.emailaddress__c;
    
		if (oldEmail != newEmail) {
               createSales.add(
                    new Sales__c (  Type = 'presales',Error_Message__c  = 'error',Status__c  = 'success'));
            }                             
    }
    try {
        insert createSales;
    }
    catch (Exception Ex){
        system.debug(Ex);
    }
}

 
Best Answer chosen by sathishsfdc
Waqar Hussain SFWaqar Hussain SF
Try this code.
trigger CreateSalesAccount on Account  (after update) {


    List<Sales__c> createSales = new List <Sales__c> ();
	
    for (Account acc : trigger.new) {
       Account  oldAccount = Trigger.oldMap.get(acc.Id);
       String oldEmail = oldAccount.emailaddress__c ;
       String newEmail = acc.emailaddress__c;
    
		if (Trigger.oldMap.get(acc.Id).emailaddress__c != acc.emailaddress__c) {
               createSales.add(
                    new Sales__c (  Name=acc.Name+' Sale', Type = 'presales',Error_Message__c  = 'error',Status__c  = 'success'));
            }                             
    }
	if(createSales.size()>0){
		try {
				insert createSales;
		}
		catch (Exception Ex){
			system.debug(Ex);
		}
	}
}

All Answers

SRKSRK
when you are creating Sales__c record you are not passing Name , i belive it is a required field to create a record in custom object
until and unless name is auto number field

Also can you please try it with before update in place of after update once

trigger CreateSalesAccount on Account  (before update) {


    List<Sales__c> createSales = new List <Sales__c> ();
    for (Account acc : trigger.new) {
       Account  oldAccount = Trigger.oldMap.get(acc.Id);
       String oldEmail = oldAccount.emailaddress__c ;
       String newEmail = acc.emailaddress__c;
    
        if (oldEmail != newEmail) {
               createSales.add(
                    new Sales__c (  Type = 'presales',Error_Message__c  = 'error',Status__c  = 'success'));
            }                             
    }
    try {
        insert createSales;
    }
    catch (Exception Ex){
        system.debug(Ex);
    }
}
Waqar Hussain SFWaqar Hussain SF
Try this code.
trigger CreateSalesAccount on Account  (after update) {


    List<Sales__c> createSales = new List <Sales__c> ();
	
    for (Account acc : trigger.new) {
       Account  oldAccount = Trigger.oldMap.get(acc.Id);
       String oldEmail = oldAccount.emailaddress__c ;
       String newEmail = acc.emailaddress__c;
    
		if (Trigger.oldMap.get(acc.Id).emailaddress__c != acc.emailaddress__c) {
               createSales.add(
                    new Sales__c (  Name=acc.Name+' Sale', Type = 'presales',Error_Message__c  = 'error',Status__c  = 'success'));
            }                             
    }
	if(createSales.size()>0){
		try {
				insert createSales;
		}
		catch (Exception Ex){
			system.debug(Ex);
		}
	}
}
This was selected as the best answer