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
KevinRussellKevinRussell 

Trap and handle specific error instance while inserting record into custom object

I manually insert a record inserted into a custom object.  One of the fields is a "Lookup(Contact)" to the Contact object.

This object does not require the Conttact field to be populated when manually entering and saving a record, it saves fine.  However, when the record is saved I fire a trigger to run some code.  if the Contact field is not populated, the trigger throws an error and it's code does not run.  What is the way to solve this?

 

1) I could tell my users that they are required to populate that field, maybe with a dummy Contact.

 

2) I could trap the error and somehow allow the trigger code execution to continue.

 

I'm using Try Catch. 

This is my error:

 

08:35:09.479 (479495000)|EXCEPTION_THROWN|[118]|System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

 

What could I put here to catch and allow the record to update anyway?

catch (Exception e) {
}

 

Thanks for any help.

 

Kevin

 

Peter_sfdcPeter_sfdc
The "Id not specified in an update call" is indicative of not having set the id for the record itself, not the Contact relationship field. Are you sure your code that throws this has correctly populated the ID value.

For instance you might place a System.debug(myInstance); call just before your trigger updates.