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
Abhishek KedariAbhishek Kedari 

Accessing variables of object which is part of current object in apex class

Hi All,

   I want to access variables of object which is part of current object in my apex class.
   e.g.
           Shipments__c shipment       where Shipments__c contains object opportunity as one of its field. Suppose oppourtunity field contains field name. How I can access these fields (id and name) of opportunity using shipment object variable.

it should be something like shipment.Opportunity__r.Id & shipment.Opportunity__r.Name

Apex code :
    System.debug('I M WRITTNG   :  ' + shipment.Related_Opportunity__r.Account.Name);

Debug Log :
   14:58:44.271 (271899101)|USER_DEBUG|[63]|DEBUG|I M WRITTNG   :  null



How I am creating new shipmenet object -
1 ) Create new account
2) Inside account there is a field call Opportunity
3) Every opportunity has shipment object associated with it.

User-added image

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

User-added image


I expect to see account name for any particular shipment as account and opportunity are already created and I am creating new shipment inside this opportunity,

I am accessing values getting passed from viualforce page to my apex class.
Do I need to populate the value in visualforce manually or it will get populate automatically ?

Thanks,
Abhishek
Best Answer chosen by Abhishek Kedari
KevinPKevinP
You're very close. You'll need to Query the object specifically asking for the fields you want to use. I'm guessing you're using a controller extension so you have the option of looking at the extensions addFields() method. More than likely though, you'll need to query for the fields you're looking to use.

All Answers

KevinPKevinP
You're very close. You'll need to Query the object specifically asking for the fields you want to use. I'm guessing you're using a controller extension so you have the option of looking at the extensions addFields() method. More than likely though, you'll need to query for the fields you're looking to use.
This was selected as the best answer
Abhishek KedariAbhishek Kedari
Thanks Kevin for replying.

 What I want to know is how I can make query if that object is not saved yet?
 I want to get all the data from shipment object, create JSON object and pass it to my web application to process even before saving this shipment object to salesforce cloud.
What I really want to know is, When I click on new button to create new shipment from visualforce page, I believe some of the fields of that new shipment object should get auto populated like account, opportunity because I am creating this new shipment object from opportunity tab and shipment object is a part of opportunity object.


Please let me know if i am not clear. Your help in this regards is highly appriciated. 

Thanks,
Abhishek