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
BrianWKBrianWK 

Retrieve value of Sobject Field without knowing Sobject or FieldName

Hey everyone. I've been working with Dynamic Apex. I may not be understand how to use it correctly but here is my scenario.

 

I have an Unknown Sobject that will have a Look field to the Account object -- but the field name is not the same from Sobject to Sobject (example Account__c vs AccountID). I want to be able to pull the ID of the lookup for a specific Sobject.

 

My inputs for the Sobject is an ID. So Far I have been able to:

 

1. Identify the Sobject by parsing the 3 digit key of the ID returning "MySobject"

2. String of all Accessible Fields on the Sobject by looping through the Describ of MySobject and looping through all the field tokens

3. RelatedItem defined through Data Query using MySobject, my All field list and the Input ID

4. Identify the Field Name of the loop field by looping through all the field Tokens and looking at the getRelationshipName() of the token

 

However, I can't seem to find a valid way of taking my "RelatedItem" (returned through data Query) and get the value from the field in step 4. I know I could create a separate Data Query that will return JUST that field -- but I rather not use a second query when I already have the full object defined from Step 3 (used later).

 

Is what I"m trying possible? Or am I stuck with having to create an additional query just to get the value of that 1 field?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
BrianWKBrianWK

I figured it out!

 

I can take my RelatedItem (Sobject) and my FieldName (string) and do RealtedItem. get ( FieldName)

 

And it'll return the value!

 

Yay:)

All Answers

BrianWKBrianWK

I figured it out!

 

I can take my RelatedItem (Sobject) and my FieldName (string) and do RealtedItem. get ( FieldName)

 

And it'll return the value!

 

Yay:)

This was selected as the best answer
Seema ASeema A
hi BrianWK,
I also want to do the same I have unknown sObject with me . How will I access individual fields in controller , as well as want to determine field type and depending on that want to display respective input field on vf page. Please let me know the way to do this.
BrianWKBrianWK

Seema - it depends on what you're trying to do:)

 

In my scenario I knew any sObject would related to the Account. So it was a matter of just finding all the potential RelatedItems of that Accounts and doing a bunch of loops. To be honest I can't remember what my use case is for this!

 

Regardless you need something that will give you some direction. Be it the Sobject is always related the specific record in use (like mine was with an Account) - or the sObject is selected by user input - or some other method.

 

The neat thing about dynamic apex is it is possible to write apex without ever declaring a specific object (like Contracts). I have a function method that takes an input ID and then Identifies the sObject (based on the first 3 characters). After I have the sObject I can get all the fields on the sObject and put into a string. From there I could do a database.query to get all the values.

 

Then on the visualforce page I could iterate over the fields known. I haven't figured out a use case for doing this step. I do use the ID sobject by ID and "get all fields" method I created a good deal. I send you a PM with that code for reference purposes.

ChitranshChitransh
Hi BrianWK

Can you please upload your snippet of code, I am working on the same scenerio so it would be great help for me.

THanks