1

In a salesforce project, I have an object called Project__c which I want to upsert. I have recently added a lookup to the Project__c object and now when I do an upsert project, the following error message is generated:

Field name provided, Project__c is not an External ID or indexed field for Project_Element__c.

NOTE: Project_Element holds a reference to Project, so I can do a query call WHERE ID =: currentProject.Id, so that I can display more than a single element and this error only occurs when there are no Project_elements with their projectID to the curentProject.

As I understand it, upsert will only look at a specific external ID field when it is told to do so. My upsert call looks like this:

upsert currentProject;

Shouldn't it just look at the project Id?

EDIT

I get the same error even if I call upsert like this:

upsert currentProject Id;

0
1

I think the following documents may apply to your situation.

Per the SOAP API Developer's Guide for upsert:

upsert() and Foreign Keys

You can use external ID fields as a foreign key, which allows you to create or update a record and relate it to another existing record in a single step instead of querying the parent record ID first. To do this, set the foreign key to an instance of the parent sObject that has only the external ID field specified. This external ID should match the external ID value on the parent record. Unlike create(), the parent record must already exist when using upsert() to create or update a child record related by a foreign key.

The above also applies to Apex in general per the following post in UPSERT without External ID from the SF User Forums:

Any field with the idLookup attribute set to true (which includes Name fields on custom objects, but not all Name fields on standard objects) can be used for record matching in the upsert() call. This is stated as such in the API doc but not in the Apex doc, but applies to Apex as well.

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.