+ Start a Discussion
Ankur SrivastavaAnkur 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.

Best Answer chosen by Ankur Srivastava
NaveenReddyNaveenReddy
Enclose your block of code in try/catch block to handle exceptions.
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

asish1989asish1989
if you want to display error messge then use adderror method in trigger. if you want to send error message in mail then handle error messsage in catch block and use sendmail method to send mail.

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
Ankur SrivastavaAnkur Srivastava
Thanks Ashish. I have modified my code to check if the required fields data is NULL in the trigger.New and if Yes then an addError method will be executed.

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.
 
NaveenReddyNaveenReddy
Enclose your block of code in try/catch block to handle exceptions.
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
This was selected as the best answer
Ankur SrivastavaAnkur Srivastava
Thanks all the problem is resolved. I have added the following statement to display the error message from the Exception Thrown in the debug log to the addError method in Trigger.

sNew.addError('Trigger failed due to'+DmlException.getDMLMessage(0));

Thanks Naveen and Ashish for the help.

Regards,
Ankur Srivastava
HelloSanHelloSan
Hi Ankur i am working with same issue i was unable to add DmlException in to addError method getting the error message variable does not exist DmlException please suggest me some sample code to make it work