2

My Apex Test failed with the following error: Cannot specify both an external ID reference Owner and a salesforce id, OwnerId.

It throws on attempt to Database.update a record. In the debug, I’ve noticed that the record includes data that I don’t specify in a SOQL request: “OwnerId”: “005QL000005HQAFYA4", “Owner”:{“Id”:“005QL000005HQAFYA4”, ...}. I used serialize-deserialize approach to remove the data from the record and the issue disappeared.

I find this behavior odd and would like to learn more about it. My assumption is this is due to the System.runAs that I use upon record creation in Test Setup. Does anyone have an idea why it leads to such behavior? Or is there another reason for that?

P.S.

Here are some explanations on why DML fails with such error in the first place https://salesforce.stackexchange.com/a/423776/70921

1 Answer 1

4

Salesforce includes certain fields in a query, even when you don't ask for them. As a simple example, consider:

Account record = [SELECT Name FROM Account WHERE Id = :someRecordId]; System.debug(record); 

You might expect to see:

(Name=Account Name) 

However, that's not what happens. Typically, you might see something like:

(Name=Account Name, Id=001xxxxxxxxxxxxYYY) 

Or:

(Name=Account Name, Id=001xxxxxxxxxxxxYYY, RecordTypeId=012xxxxxxxxxxxxYYY) 

Etc...

In fact, the returned object from a query might include fields depending on the runtime API version, the API version of a compiled class or trigger, whether or not specific features are enabled (e.g. having multicurrency enabled may return CurrencyIsoCode), the type of request (API, Visualforce, Lightning, etc), and so on.

Do not assume that the query you write will return precisely the data you request. At minimum, the Id field will always be added to a query run in Apex. Instead of using a record from a query, you should generally consider creating a new record in memory and explicitly setting just the fields you intend to use.

2
  • Thank you for your comment. Yes, I'm aware of that standard behavior. But I have never faced a situation when it retrieves OwnerId and Owner data and blocks you from direct update of the record. When Id is automatically retrieved, it doesn't require me to implement additional logic like in this case. That is why I'd like to learn more about it, so that I can be better prepared next time. Commented May 7, 2024 at 18:11
  • @DuckTheDeveloper Well, you didn't demonstrate the code that is actually causing the problem, so we would generally have no way to reproduce the problem. If you can provide a Short, Self Contained, Correct (Compilable), Example, I'd be happy to revise this answer with more specific details. In lieu of that, a generic answer is probably the best anyone could offer. Commented May 7, 2024 at 20:31

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.