Skip to content

Instantly share code, notes, and snippets.

@shinchit
Last active November 7, 2019 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinchit/7c05aceeb24b8e8aaf97d8c658eb77f8 to your computer and use it in GitHub Desktop.
Save shinchit/7c05aceeb24b8e8aaf97d8c658eb77f8 to your computer and use it in GitHub Desktop.
static void someMethod() {
List<SObject> sobjs = [
SELECT
Id,
Name,
AccountId,
Account.Name,
SomeCustom__r.Id,
SomeCustom__r.Name
FROM
Opportunity
];
Schema.DescribeFieldResult fieldResult = null;
Schema.SObjectField field = null;
/* print Opprutunity.Id and Opportunity.Name */
System.debug(sojbs);
/* print Account.Id and Account.Name */
fieldResult = Schema.Opportunity.Account.getDescribe();
field = fieldResult.getSObjectField();
System.debug(sobjs.getSObject(field));
// or
System.debug(sobjs.getSObject('Account'));
/* print SomeCustom__c.Id and SomeCustom__c.Name */
fieldResult = Schema.Opportunity.SomeCustom__c.getDescribe();
field = fieldResult.getSObjectField();
System.debug(sobjs.getSObject(field));
// or
System.debug(sobjs.getSObject('SomeCustom__r'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment