Skip to main content
Tweeted twitter.com/StackSalesforce/status/1251752392183463938
deleted 39 characters in body; edited title; edited tags
Source Link
Adrian Larson
  • 151.3k
  • 38
  • 247
  • 431

How to use GetSObject is to retrieve related data fromin a relationshipnull safe manner?

I have thea lookup relationship.

ifIf I just do the following SOQLquery it works fine:

Sobject c = [select field__r.fieldchild__c from custom_obj where Id = :someId];
system.debug('c:' + c);

However with the above works andfollowing I get the result.

if I do the followingan error:

Sobject c = [select field__r.fieldchild__c from custom_obj where Id = :someId];
String c2 = (String) r.getSobject('field__r').get('fieldchild__c');

then I get the following errorError message:

FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object

GetSObject is to retrieve data from a relationship

I have the lookup relationship.

if I just do the following SOQL it works fine:

Sobject c = [select field__r.fieldchild__c from custom_obj where Id = :someId];
system.debug('c:' + c);

the above works and I get the result.

if I do the following:

Sobject c = [select field__r.fieldchild__c from custom_obj where Id = :someId];
String c2 = (String) r.getSobject('field__r').get('fieldchild__c');

then I get the following error:

FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object

How to use GetSObject to retrieve related data in a null safe manner?

I have a lookup relationship.

If I just do the following query it works fine:

Sobject c = [select field__r.fieldchild__c from custom_obj where Id = :someId];
system.debug('c:' + c);

However with the following I get an error:

Sobject c = [select field__r.fieldchild__c from custom_obj where Id = :someId];
String c2 = (String) r.getSobject('field__r').get('fieldchild__c');

Error message:

FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object

deleted 7 characters in body
Source Link
Adrian Larson
  • 151.3k
  • 38
  • 247
  • 431

I have the lookup relationship.

if I just do the following SOQL it works fine:

Sobject c = [select filed__rfield__r.fieldchild__c from custom_obj where Id = :someId];
system.debug('c:' + c);

the above works and I get the result.

ERRORL if I do the following:

Sobject c = [select filed__rfield__r.fieldchild__c from custom_obj where Id = :someId];
String c2 = (String) r.getSobject('filed__r''field__r').get('fieldchild__c');

then I get the following error:

FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object

I have the lookup relationship.

if I just do the following SOQL it works fine:

Sobject c = [select filed__r.fieldchild__c from custom_obj where Id = :someId];
system.debug('c:' + c);

the above works and I get the result.

ERRORL if I do the following:

Sobject c = [select filed__r.fieldchild__c from custom_obj where Id = :someId];
String c2 = (String) r.getSobject('filed__r').get('fieldchild__c');

then I get the following error:

FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object

I have the lookup relationship.

if I just do the following SOQL it works fine:

Sobject c = [select field__r.fieldchild__c from custom_obj where Id = :someId];
system.debug('c:' + c);

the above works and I get the result.

if I do the following:

Sobject c = [select field__r.fieldchild__c from custom_obj where Id = :someId];
String c2 = (String) r.getSobject('field__r').get('fieldchild__c');

then I get the following error:

FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object

Source Link
Nick
  • 7.2k
  • 27
  • 147
  • 286

GetSObject is to retrieve data from a relationship

I have the lookup relationship.

if I just do the following SOQL it works fine:

Sobject c = [select filed__r.fieldchild__c from custom_obj where Id = :someId];
system.debug('c:' + c);

the above works and I get the result.

ERRORL if I do the following:

Sobject c = [select filed__r.fieldchild__c from custom_obj where Id = :someId];
String c2 = (String) r.getSobject('filed__r').get('fieldchild__c');

then I get the following error:

FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object