You need to sign in to do that
Don't have an account?
add error in trigger
i made a trigger after insert and after update on account object , and in this trigger i want to insert contact into account.
but if any problem occurs in contacts and contact could not be inserted then i wnat to show error in corresponding account obj.
List<Contact> contactLst=new List<Contact>();
for(Accoubnt a:Trigger.new)
{
contact c=new contact(account.Id=a.Id);
contactLst.add(c);
}
insert contactLst;
if i write insert command on contact list then how to idientify that on which account i have to add error....and if i write it into loop then its wrong approach(dml statement should not be in loops)
you want to display the error on account detail screen or wat? then i think this not possible....
you can put adderror for contract and catch it as a exception and then do a roll back. keep the save point above account update or insert.
I can give you an example that how you can add an error via a trigger, not sure whether its is useful in youor scenario or not.
For example you hav a trigger on account
trigger TestTrgigger on Account (before insert) {
for (Account accObj : Trigger.new ){
if (error condition== true){
// add error
accObj.addError('Error occured, Please contact the System Administrator');
}
}
}
First thing to keep in mind is that you should always write triggers that are bulk operation supported. For current situation that you have described if there is a any bulk operation you will insert all contacts in a single DML statement.
If any of contact insert to fail whole batch will be rolled back. It doesn't matter for which account it failes because buld operation are not possible from standard UI, you can add error to all the contacts.
If user is editing Account from UI it will be always one and you can add error on that Account.