+ Start a Discussion
King KooKing Koo 

Infamous INVALID_FIELD_FOR_INSERT_UPDATE when upserting child object

Hi there

I have an object which is the child to Account in a master-detail relationship.

In the REST API, when creating the child record for an existing account (with external ID AAA), this is the JSON I use.
 
{
  "batchRequests": [
    {
      "method": "PATCH",
      "url": "v35.0/sobjects/Account_Child__c/External_Id__c/ABCDE",
      "richInput": {
        "Account__r": {
          "External_Id__cc": "AAA"
        },
        "Other_Field__c": "whatever"
      }
    }
  ]
}

This is fine when I'm inserting.  However, when I'm updating the content for "Other_Field__c", if I include "Account__r" then I get the infamous error message:  
Unable to create/update fields: Account__c. Please check the security settings of this field and verify that it is read/write for your profile or permission set.
So I'm forced to not include Account__c.

The thing is, if the integration developer is using a tool to send the JSON over to Salesforce (from SAP in this case), he will not know whether the child object record exists or not.  I'm hoping I could use the SAME structure for both insert and update in this upsert.

Is my only solution to set the "Child records can be reparented to other parent records after they are created" checkbox?  

If so I'm just amazed how you could do that before this checkbox was introduced (whcih was only less than 4 years ago).

Thanks a lot
King




 
Tolga SunarTolga Sunar
Yes, "Child records can be reparented to other parent records after they are created" checkbox being off seems to be the root of this issue here.

If you have to keep this option off, you can query if this child record is already present in SFDC; if so, delete it and then parse the JSON and insert. However, for this method to work, you must be bringing an ID information from SAP, so you can query.
King KooKing Koo
Hi Tolga

Based on what you said though, for each child record I need to do this query first, wouldn't that be a lot of traffic back and forth?

(It does sound like having reparent option turned off is the easiest.)

Thanks
King
Tolga SunarTolga Sunar
The method I proposed does not affect the amount of callouts. You simply deserialize the IDs into an array, then iterate the query inside a for loop. 

However, I'm sure that enabling "Child records can be reparented to other parent records after they are created" option would solve this issue. What's keeping you from enabling it?