function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Saving a Related Object Values in a List

Hello.  I am having some issues identifying the correct way to save a list and ALSO save the values from a RELATED OBJECT in this list.


From my research, I know this can be done in I just am not using the appropriate syntax.


I have a list

myList = SELECT [modelcomments__c, modelname__c, make__c,  manufacturer__r.rating__c FROM models__c];


I have a VF page that uses inputFields  to ask the user to update the value of modelcomments__c and manufacturer__r.rating__c fields.


I easily am able to save the modelcomments__c field via...

"update myList;"


However, to update the manufacturer object, I thought all I needed was to enter...

"update myList.manufacturer__r;"


Unfortunately, I was wrong.  Can anyone tell me how I can save the updated value of manufactuer__r.rating field?


Thanks in advance.




With adding relationship '__r' to your query you can get the related field value but you'll still the reference Id to update that related record. So modify your query to some thing like this


myList = [SELECT modelcomments__c, modelname__c, make__c, manufacturer__c, manufacturer__r.rating__c FROM models__c];

Note that I'm also queriying for manufacturer__c field which is the relationship field that has the Id of the related record and you need the id of a record to do any DML operations. So now you can update the related record it like this


manufacturer__c mf = new manufacturer__c(id = mylist[recordNumber].manufacturer__c, //depends how you want the manufacturer id to be assigned
rating__c = "new rating capured from VF Page");
update mf;