+ Start a Discussion
kdaviskdavis 

Trigger.New and Workflow

Hello,

 

I have a (before insert, before update) trigger on a custom object called Assignment that reassigns the record ownerID to a user ID based on a custom field in the Assignment object that looks up to another custom object called Requested Employee that looks up to a user record.

 

In addition I have a workflow rule that populates the users email in a custom field on the Assignment object.

 

The trigger works, I am able to reassign the owner, but only when the workflow rule is active. When I deactivate the workflow rule, the record owner will not reassign when I insert a new record, it will only update after I have edited a record TWICE.

 

My question is...why does this occur. I'm relatively new to SF and Apex, and trying to learn what is going on behind the scenes in this situation.

I'm assuming the workflow rule has some effect on the records contained in the Trigger.NEW list but I am having trouble wrapping my head around what is going on.

 

Any help or advice would be greatly appreciated.

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
JFraileJFraile

Hi.

 

There is a chance that the insert trigger part is not working or is in conflict with the update part. For ex., when inserting a new record the insert part is not working, then the workflow field update provokes the update trigger to run so it does the reassignment, and not the insert trigger as expected. Or maybe, the insert part is executing but then the update part is corrupting the assignment. Try to verify this using some system.debug statements in you trigger to see what code executes on inserts. Also split your trigger code into two parts: isInsert and isUpdate,  to help see the differences.

 

You could try to write the trigger just for updates, and see if the assignment occurs on inserts.

 

Some interesting info about trigger execution can be found here and here

All Answers

JFraileJFraile

Hi.

 

There is a chance that the insert trigger part is not working or is in conflict with the update part. For ex., when inserting a new record the insert part is not working, then the workflow field update provokes the update trigger to run so it does the reassignment, and not the insert trigger as expected. Or maybe, the insert part is executing but then the update part is corrupting the assignment. Try to verify this using some system.debug statements in you trigger to see what code executes on inserts. Also split your trigger code into two parts: isInsert and isUpdate,  to help see the differences.

 

You could try to write the trigger just for updates, and see if the assignment occurs on inserts.

 

Some interesting info about trigger execution can be found here and here

This was selected as the best answer
kdaviskdavis

Thank you JFraile!

 

My trigger wasn't looping through my insert you were exactly right. By breaking up the code I was able to locate the issue.

Thanks again!