You need to sign in to do that
Don't have an account?

Accessing variables of object which is part of current object
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.
shipment.opportunity.id / shipment.Opportunity__c.id
shipment.opportunity.name / shipment.Opportunity__c.name
but its not working and saying id and name does not exist.
Can anyone please help me with this?
Let me know if I am not clear with the question.
Thanks,
Abhishek
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.
shipment.opportunity.id / shipment.Opportunity__c.id
shipment.opportunity.name / shipment.Opportunity__c.name
but its not working and saying id and name does not exist.
Can anyone please help me with this?
Let me know if I am not clear with the question.
Thanks,
Abhishek
https://developer.salesforce.com/page/A_Deeper_look_at_SOQL_and_Relationship_Queries_on_Force.com
For your query, it should be something like shipment.Opportunity__r.Id & shipment.Opportunity__r.Name
before doing this you need to retrieve them in your SOQL though,
Shipments__c shipment = [Select Id, Name, Opportunity__r.Id, Opportunity__r.Name from shipment LIMIT 1];
Hope this helps
Kartik is correct in that you'll need to use __r and requery for the Opportunity relationships, but his example isn't 100% accurate. You'll need to pass in the Shipment Id into the query as well.
That being said, how are you retrieving Shipment? Does your Apex class act as an extension controller? If so, the query and extension controller might look like the example below. Please note that Opportunity__c, Opportunity__r.Id are synonymous in this example.
Thanks a lot for directing me in right direction but I am still stuck with this issue.
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.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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