I'm getting a List of javascript object from lightning component, that contains updated values and Id of a customObject in form:

[{"contact__r.name":"Name","Id":"a3Rn0000000D2wcEAC", 'field1__c':'text'}]

I can directly update the fields for this object using apex controller like this:

public static void saveObjects(List<customObject__c> customObjectList){
    update customObjectList;

But this won't allow me to update the fields on related object - contact__r.name - infact this field disappears when the parameter being passed into the method is defined as customObject.

How best to update these related objects?

Here is what I'm thinking at the moment - I am trying to define the parameter passed to this method as a list<Map<String, string>> and then I need to work through the fields and update them. But not sure how to do this - I wonder if there is any way to iterate through the list of fieldNames in the keyset of the parameter and then dynamically assign them to an object, something like this:

public static void saveObjects2(List<Map<String, String>> customObjectList){

    for(Map<String, String> customObjectData :customObjectList) {
       <!-- NEED CODE HERE -->
       <!-- Example: -->
       for (String customObjectDataKey :customObjectData.keyset()) {
            relatedObject relatedObject;
            customObject__c customObject;
            if (customObjectDataKey == relatedObjectField){
                relatedObject.customObjectDataKey = customObjectData.get(customObjectDataKey);
            } else {
                customObject.customObjectDataKey = customObjectData.get(customObjectDataKey);
            update customObject;
            update relatedObject;

I don't know if the above approach is possible or if there is an easier way to do this.

1 Answer 1


A few things here:

  • Contact.Name is a read-only field, so you can't update it anywhere, full stop.
  • DML operations never update related records (directly - they can be indirectly updated due to roll-up summary operations, other triggers, processes, etc). You always need to explicitly update each sObject instance that you wish to persist.
  • When sObject instances are nested, they actually nest whole objects, not just fields with relationship paths in their names. Trying to use the relationship paths will make your code more complex and tricky.

I would suggest that this problem is best solved outside the bounds of what you've shown us in this question. If you want to update related records, have your JavaScript controller track those related records separately - as full object instances - and call your server controller separately to persist those changes.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .