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

Getting Error: "Sobject row does not allow errors"
Here is the code:
public void psiFailed()
{
Logger.info(ir, 'psiFailed() called. Inserting Case.');
Case c = new Case();
c.Type = 'Arbitration';
c.Reason = 'Undisclosed Damage';
c.Description = 'PSI FAILED: ' + ir.PSI_Review_Comments__c;
c.Inspection_Request__c = ir.Id;
c.OwnerId = UserInfo.getUserId();
c.RecordTypeId = Utils.getRecordTypeId('Case', 'Arbitration - Open');
c.VIN__c = ir.Purchase__c;
try{
insert c;
}catch(System.DmlException e){
for (Integer i = 0; i < e.getNumDml(); i++) {
if(e.getDmlType(i) == StatusCode.FIELD_CUSTOM_VALIDATION_EXCEPTION){
ir.addError(e.getDmlMessage(i));
}
}
}
Logger.flush();
Logger.flush();
}
It's being passed in the Insepction Request object (ir) and creates a new Case if the ir failed. The error occurs on line 60 which is the line "addError" is called. I checked the forms and most people mentioned the object not being in the Trigger Context when addError is called on it. However since my trigger calls this function, I do believe it's in the context. I can provide more data if needed.
Thanks,
-Eric
Eric
Asuming this happens during a before Trigger; then variable ir needs to be bound to an SObject for Inspection_Request__c within the list Trigger.new. That is, addError works to prevent DML operations and is typically used on the list members within Trigger.new
Hi Eric,
The Class this code is in is called in a After Insert Update trigger. The class starts like this:
public class InspectionRequestHelper
{
Inspection_Request__c ir;
public InspectionRequestHelper(Inspection_Request__c ir)
{
this.ir = ir;
}
The call from the trigger looks like this:
else if('FAIL'.equalsIgnoreCase(newIr.PSI_Passed__c))
{
irh.psiFailed();
}
Does this help in solving the issue?
You can't call addError on an after dml event sobject. Use the before insert/before update dml events instead.
Eh... you can use addError during an after trigger. It's just not as efficient as doing so during a before-trigger. Or are you referring to something else?
Dan
You can use an addError anywhere, but the design of addError is that it is used before the commit (as in, a validation rule). See "SObject > addError" documentation, specifically:
Note that last line. They specifically advise you to use it in a before trigger. This is because if you do not do this, the error won't appear to the user, and causes abnormal script termination, which causes frustration to the user.
Well, I would interpret the documenation the same way you would, but... try it.
If you put an addError in an after insert trigger for a lead object, then try to add a new lead, you'll see exactly the same user experience as you do if you use a before insert trigger.
Now, where I would expect a difference is if you use the field.addError syntax - but that's been deprecated anyway....
Dan
Add Error can be called even in after trigger. Noproblems with that . however addError is a generic method with sObject and not neccessary in context of trigger.
It's ability to work (correctly) in an after dml trigger is (relatively) new. Previously, it threw an exception that was confusing for users. It now works in a much more sane manner. Thanks for the update, Sammy, kibitzer.
Right - it's always better to use a before trigger though for performance reasons. It should be used in an after trigger only when there's no choice - such as after a lead conversion (where before triggers may not be called, depending on configuration).
And there are times where addError just doen't work - but I haven't found reliable documentation yet on that and haven't researched it myself. I'd be glad to hear if anyone has more specifics on that.
Dan
I'm having a bit of trouble understanding why it wouldn't be in Context. The Trigger "InspectionRequestAfterInsertUpdate" calls a method from class "InspectionRequestHelper" which catches a DML exception and uses addError. Is it because we create the "ir" (Inspection_Request__c object) object using this code?:
public class InspectionRequestHelper
{
Inspection_Request__c ir;
public InspectionRequestHelper(Inspection_Request__c ir)
{
this.ir = ir;
}
Is there a better method to display the error message I want when I hit a DML exception?
Thanks,
-Eric
Is there a way to replace system error with more user friendly error message? I have a unique field and when 2 users tried to submit a record at same time, they see an error like "Error: Duplicate value on record".
I would like to replace such errors to "System error, please hit Save again." Is there a way to do that in before trigger? I do not see a method getError() or something.
Dan: Great book! I just love reading it.
regards
Mitesh