You need to sign in to do that
Don't have an account?

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??
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); } }
All Answers
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);
}
}