You need to sign in to do that
Don't have an account?
Ankur Srivastava
How to display error message/notify user if my trigger fails to insert data in another object due to value for required field missing ?
Hi,
I am inserting a record in another object B, once an insert trigger is executed in my source object A.
however i want to display error message /notify if any of the fields of object B failed to get populated with values from object A.
The user should be notified that the insertion in object B failed ,because a field X couldn't be populated due to some reason.
I am inserting a record in another object B, once an insert trigger is executed in my source object A.
however i want to display error message /notify if any of the fields of object B failed to get populated with values from object A.
The user should be notified that the insertion in object B failed ,because a field X couldn't be populated due to some reason.
Here is the sample code
trigger insertContact on Opportunity (before insert) {
Contact c = new Contact();
c.FirstName= 'Tester';
try{
insert c;
System.Debug('Insert successful on Contact');
}
catch (Exception e)
{
System.Debug('The following error occured' + e);
}
}
To dispaly error message at Object A add addError method to inserting records.
Sample code to handle this
trigger insertContact on Opportunity (before insert) {
Contact c = new Contact();
c.FirstName= 'Tester';
try{
insert c;
System.Debug('Insert successful on Contact');
}
catch (Exception e)
{
for (Opportunity opp: Trigger.New) {
opp.addError('There was a problem Inserting the contacts');
}
}
}
Regards,
Naveen
http://www.autorabit.com
All Answers
https://developer.salesforce.com/forums?id=906F00000008zcBIAQ
http://www.sfdc99.com/2014/03/01/sending-emails-using-apex/
Write exception message in the place body.
Important :
If this is what you where looking for then please mark it as a solution for others benefits.
Thank You
But one more thing I want to know, that is there a generic way by which i can just display as an error message if such an error occurs.
For eg. A object has two fields x,y. These fields need to be inserted in object B, by an insert trigger defined in object A.
If during insert DML defined in trigger any of the columns have null value, then in object B the value will not be inserted and trigger fails.
This exception should be visible in the object A edit page as "Trigger failed due to <$field> had values missing". This should be a generic one.
I don't want mails. The exceptions are handled in debug logs. I don't want that as well. Just an error message on my edit page.
Here is the sample code
trigger insertContact on Opportunity (before insert) {
Contact c = new Contact();
c.FirstName= 'Tester';
try{
insert c;
System.Debug('Insert successful on Contact');
}
catch (Exception e)
{
System.Debug('The following error occured' + e);
}
}
To dispaly error message at Object A add addError method to inserting records.
Sample code to handle this
trigger insertContact on Opportunity (before insert) {
Contact c = new Contact();
c.FirstName= 'Tester';
try{
insert c;
System.Debug('Insert successful on Contact');
}
catch (Exception e)
{
for (Opportunity opp: Trigger.New) {
opp.addError('There was a problem Inserting the contacts');
}
}
}
Regards,
Naveen
http://www.autorabit.com
sNew.addError('Trigger failed due to'+DmlException.getDMLMessage(0));
Thanks Naveen and Ashish for the help.
Regards,
Ankur Srivastava