+ Start a Discussion
Praveen NPraveen N 

getting dynamically field value from the object - not able to do

Hi ,

 

approval__c app=[select id,name,opportunity__r.name,opportunity__r.Account.name from Approval__c limit 1];

string str='name';
if(app.get(str)=='test')
system.debug('passed');
else
system.debug('failed');
// The above is working fine
str='opportunity__r.name';
if(app.get(str)=='test') // this is not working
system.debug('passed');
else
system.debug('failed');

actually str valu will come from another object.

when we have relationship not able to get the value from the object. is there any workaround to get?

 

Thanks

Praveen

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

Instead of the .get method on the SObject you will have to use the .getSObject method instead.

 

From there you can use the .get to get the property from the child Object.

 

Like so:

 

approval__c app=[select id,name,opportunity__r.name,opportunity__r.Account.name from Approval__c limit 1];

string str='name';

if(app.get(str)=='test')
	system.debug('passed');
else
	system.debug('failed');
	
SObject childObject = app.getSObject('opportunity__r');

if (childObject != null)
{
	if(childObject.get(str)=='test')
		system.debug('passed');
	else
		system.debug('failed');
}

 

All Answers

Sean TanSean Tan

Instead of the .get method on the SObject you will have to use the .getSObject method instead.

 

From there you can use the .get to get the property from the child Object.

 

Like so:

 

approval__c app=[select id,name,opportunity__r.name,opportunity__r.Account.name from Approval__c limit 1];

string str='name';

if(app.get(str)=='test')
	system.debug('passed');
else
	system.debug('failed');
	
SObject childObject = app.getSObject('opportunity__r');

if (childObject != null)
{
	if(childObject.get(str)=='test')
		system.debug('passed');
	else
		system.debug('failed');
}

 

This was selected as the best answer
Praveen NPraveen N

Hi Tan,

 

Thank you so much. it is working good. i had one more issue. 

the order of relationship as like approval__c --> opportunity --> Account. means account is second level parent to approval__c.

i am not able to get these details.

approval__c app=[select id,name,opportunity__r.id,opportunity__r.name,opportunity__r.Account.name from Approval__c limit 1];
system.debug(app.opportunity__r.name);
string str='name';
if(app.get(str)=='test')
system.debug('passed');
else
system.debug('failed');
// The above is working fine
str='opportunityid';


if(((opportunity)app.getSObject('opportunity__r')).get('name')=='test') // this is not working
system.debug('passed');
else
system.debug('failed');

 

the above was working fine. 

 

Sobject c=app.getSObject('opportunity.Account'); or Sobject c=app.getSObject('opportunity__r.Account');

Sobject c=app.getSObject('opportunity__r.Account__r');

 

i have tried in above three ways not able to get. could you please tell me is there any way to get.

thanks in advance.

 

Praveen

Sean TanSean Tan

You'll have to use the getSObject on the Opporunity SObject that you retrieved from your Approval. You can't specify the relationships fields in the getSObject directly. So it will have to be something like this:

 

approval__c app=[select id,name,opportunity__r.name,opportunity__r.Account.name from Approval__c limit 1];

string str='name';

if(app.get(str)=='test')
	system.debug('passed');
else
	system.debug('failed');
	
SObject childObject = app.getSObject('opportunity__r');

if (childObject != null)
{
	if(childObject.get(str)=='test') 
		system.debug('passed');
	else
		system.debug('failed');
		
	SObject accountObject = childObject.getSObject('Account');
	
	if (accountObject != null)
	{
		//Logic
	}
}

 

Praveen NPraveen N

really super......

Thank you somuch Tan................