+ Start a Discussion
TedLiuTedLiu 

dynamic biding of reference field

Hi All, I need some help with

Myobject__c c = new MyObject__c();

c.put('carrier__c', 'xyz'); // This works
c.put('Name__r.lastName__c', null); // THIS DOES NOT WORK when I view it through my VF page I get invalid field. For some reason REFERENCE to another field from other object does not work 

In developer console when I run query like Select Name__r.lastName__c From Myobject__c where id..... I can access Name__r.lastName__c
Best Answer chosen by TedLiu
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello Mastram,

To bind values using relationship fields you should use the putSObject method instead. 

Here is an example:
Interaction__c test = new Interaction__c();

test.put('Points__c', 43);
test.putSObject('Contact__r', new Contact( LastName='contact'));

system.debug(test.Contact__r.LastName);

Please refer to:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_methods_system_sobject (http://​https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_methods_system_sobject)

Hope to have helped!

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
 

All Answers

Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello Mastram,

To bind values using relationship fields you should use the putSObject method instead. 

Here is an example:
Interaction__c test = new Interaction__c();

test.put('Points__c', 43);
test.putSObject('Contact__r', new Contact( LastName='contact'));

system.debug(test.Contact__r.LastName);

Please refer to:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_methods_system_sobject (http://​https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_methods_system_sobject)

Hope to have helped!

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
 
This was selected as the best answer
TedLiuTedLiu
@Zuinglio Thank you !!!