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
jadentjadent 

Prevent Only Certain Inserts on Trigger

Is it possible to allow some records to be inserted but not others on a trigger?

 

The records are coming in via the API so i have no way to checking them before hand or overriding with vf and using the opt_allOrNone option on the database.insert method.

Best Answer chosen by Admin (Salesforce Developers) 
jhenningjhenning

Hi jadent:

 

For create(), update(), and delete() calls that attempt to write to multiple records in an object, each  record operation is treated as a separate transaction. For example, if a client application attempts to create two new accounts, they’re created using mutually exclusive insert operations that succeed or fail individually, not as a group.

 

Remember, the API uses a Request/Response design. When you send a list, of say, 100 objects in an update() call, you receive a Results list back with 100 results objects. You must then cycle through the results to see which ones succeeeded and which ones failed by looking at the isSuccess property.

All Answers

jhenningjhenning
Yes, in your BEFORE INSERT trigger code use the AddError method on each object in Trigger.New that you do not want the insert to suceed.
jadentjadent

thanks john for the reply. So via the API (not Bulk API) it will not rollback all operations if 1 record is marked as an error?

 

The APEX manual explains how it works when using the Bulk API (inserts succesfully records when their are errored records) and DML via APEX (rollsbackentire operation) will handle it. But it doesn't say anything for the Web Services API and someone told me it acts like the DML via APEX. is this incorrect?

 

Also is there anyway to supress the error returned? In this case even through we don't want to continue the insert it is not an error so we don't want to return one.

 

Thanks for all the help! 

 

 

jhenningjhenning

Hi jadent:

 

For create(), update(), and delete() calls that attempt to write to multiple records in an object, each  record operation is treated as a separate transaction. For example, if a client application attempts to create two new accounts, they’re created using mutually exclusive insert operations that succeed or fail individually, not as a group.

 

Remember, the API uses a Request/Response design. When you send a list, of say, 100 objects in an update() call, you receive a Results list back with 100 results objects. You must then cycle through the results to see which ones succeeeded and which ones failed by looking at the isSuccess property.

This was selected as the best answer
jadentjadent
Thanks for the explanation!
Ispita_NavatarIspita_Navatar

I had once implemented a Dot Net Application which used to pick records from a website and took them over to Salesforce and records were also created via the normal salesforce UI.


I had to do differential processing based on if they were coming from The Application in DotNet or via normal UI, I used flags to solve my problem when the records came via "DotNet" application I set the flags explicitly but when the user comes via Salesforce these flags are not set, hence on this basis I can differentialte betweeen records based on their source and your trigger too would be able to discriminate though with some added logic.