[SalesForce] System.SObjectException: Invalid relationship name for Lead

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

Best Answer

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');