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
FdeBergeyckFdeBergeyck 

remove record from trigger.new

Hello,
 
Does anybody knows how you can remove a record from trigger.new before the insert? I tried with

System.Trigger.new.remove(i);

But I get the followin error message:

 
19:00:04 INFO - <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sf="http://soap.sforce.com/2006/08/apex"><soapenv:Body><soapenv:Fault><faultcode>sf:UNKNOWN_EXCEPTION</faultcode><faultstring>UNKNOWN_EXCEPTION: An unexpected error occured. Please include this ErrorId if you contact support: 554952691-62</faultstring></soapenv:Fault></soapenv:Body></soapenv:Envelope>
 
Thank you for your help
 
François de Bergeyck
MyGodItsColdMyGodItsCold
Would it make sense to create lists of the same type (e.g. List*lt;contact&gt; changedContacts = trigger.new
and List&lt;contact&gt; oldContacts = trigger.old), and then delete from those lists?

Also, might there be a problem if you only have one object in the list and are making the list go empty?

nikolanikola
François,
 
You can always add an error to an object in Trigger.new which will prevent its insertion. This will also notify the user/client that initiated the insert that the particular object was not inserted.
 
For example, if you want to prevent an account form being inserted if an important field is empty:
 
Code:
for( Account account : Trigger.new )
{
  if( account.importantField__c == null )
  {
    account.addError('Failed to insert this account!');
  }
}

Is there any reason why you do not want to notify the user?

Nikola

FdeBergeyckFdeBergeyck

Thank you,

I actually figured a way around to solve my problem: I created a toDelete checkbox on the object, and a trigger after insert that deletes the record if the checkbox is checked.

The users are not notified because the only user that can create the records is the integration user...

Thank you again

François

Dongzhi Yang 27Dongzhi Yang 27
@FdeBergeyck this actually made great sense. I implemented similar logic. Thanks