+ Start a Discussion
Shravya Rama 9Shravya Rama 9 

Throwing error on current record when new record insertion is failed

Hallo all,
Is it possible to stop inserting current record when the new record insertion from the apex trigger fails. It should also throw an error on UI "The current record failed to created new records".
UC InnovationUC Innovation
Hi Shravya, 

Try using sobject add error method. Something like this might be in order:
 
Trigger.new[0].addError('bad');

you can read more on this as well as other similar functions here (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm).

Hope this helps!

AM
UC InnovationUC Innovation
Also you should return from the function after that call.
Shravya Rama 9Shravya Rama 9
Sorry I forgot to mention. I am using this trigger on visualforce page. This method Trigger.new[0].addError('bad'); is not working on it. I have also tried
Apexpages.addMessage(new Apexpages.message(ApexPages.Severity.Error,'Not possible')); with <apex:pageMessages> tag. Even this is not working.