In many of my Controller extensions I have save action methods that work for NEW and EDIT scenarios. I simply use upsert record instead of update / insert record in such cases.

But now I am running into strange duplicate value found with customer workflow rules that set unique fields when something ISNEW().

Could this be connected to my use of upsert? And if so, is there a better and still short (code-wise) way to have an context-agnostic DML save?


The workflow rules would fire after the upsert (insert/update), so I wouldn't expect that to be the cause of your problem. Also, existing records would already have an ID associated with them while new ones wouldn't. Unless there's something about your coding that's inserting new records because the Id isn't following along with existing records, I wouldn't think the upsert would be the cause.

From what you're describing, it's the workflow that adds the data for the new fields, so if anything, you'd be doing an update to an existing record causing an update to fire and the ISNEW() in the workflow to be returned as false.

As always, without seeing specific code, one can't fully evaluate a specific situation. But that's my analysis based on your description of your situation. Obviously, it's always preferable to use insert when you know it's a new record and update when you know it's an edit to an existing record. To me, that would be a "best practice".

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.