I am trying to get a value of a field from leads. Here is the code i have

System.debug(' l.getSObject(name) is ' + l.getSObject('name'));

Where l is the lead

This gives out a error

FATAL_ERROR|System.SObjectException: Invalid relationship name for Lead

The same error is there for custom fields as well. Is there something i am not doing correct

Here is the documentation. http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject.htm#apex_System_SObject_getSObject

up vote 4 down vote accepted

I think you're confusing some methods, to get lead fields you can use the get method.

sObject l = [select id,name from lead limit 1];
system.debug('this is it' + l.get('name'));

Have to admit that I find the getSObject(s) documentation somewhat vague and couldn't produce a working example in a short time. Maybe someone else can.

Edit

GetSObject is to retrieve data from a relationship, like this:

Sobject c = [select account.name from contact where Id = :someId];
String AccountName = (string)c.getSobject('account').get('name');
  • 2
    I updated the answer to include an example of getSobject. – sfdcfox Jun 18 '14 at 13:48
  • Thanks for the pointer.. i just figured it out and you had written it out before i could write the comment :P . – Prady Jun 18 '14 at 13:49

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.