function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bp-devbp-dev 

Discard DML without Error

Hi, I'm wonderinf if it's possible to discard a dml action without an error, specificly on insert. I know that you can do record.adderror() in apex on a trigger and the dml action won't go through, but that'll cause everything else to halt as well as soon as that error is hit. What I'm wonderinf is if it's possible to simply discard the action somehow. So for a before insert trigger:

 

for(Contact c : trigger.new){

if(c.someField__c == 'some value'){

do something that c is never inserted.

}

}

 

I know I can always mark this record for deletion and get rid of it via a future method with a delete statment, but that's extra queries and dmls that I'm trying to avoid. Any ideas?

JitendraJitendra

Hi,

 

Use database.insert(listContainingRecords,false)

 

The false in above statement says that error occurred in between, then still insert remaining records.

 

Read more here :

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_insert.htm

bp-devbp-dev

Humm I wonder if it'll work because the insertion is actually coming from an integration, not another class. That's why I was inclined to do a check on a trigger as the cotnacts come in. Would what you suggest still work? Would it have to be on a before or after trigger?

sfdcfoxsfdcfox

The integration can set the the allOrNone SOAP header to cause failures to be reported back as non-fatal:

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_header_allornoneheader.htm

 

You could also build a Force.com Apex Code webservice class, and you could update the integration to use that call instead; it would also allow you to use Database.<dml>(sobject[], boolean) methods to acheive a similar result.