+ Start a Discussion
bozotheclownbozotheclown 

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 force.com...but 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.

magicforce9magicforce9

Hi,

 

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;