0

I have 2 custom object.

1 - namespace__object1__c
2 - namespace__object2__c

I have lookup relationship in custom object namespace__object2__c.

Now i want to query id record from object2 where object1 id = (provided parameter)

String s = 'a6T3k0000LMJcEAO';
string query = 'select ID, (select ID from namespace__object1__c where ID = :s  ) from namespace__object2__c';

SObject rObject = database.query(query);
system.debug(rObject);

Added:- defined relation

object1 namespace__object1__c   Lookup(object)

1 Answer 1

1

You don't need a parent-child query here, since what you want is actually the child object. You should simply filter on the relationship field on namespace__object2__c. You also do not need, and should not use, Dynamic SOQL here; "regular" SOQL works fine:

Id s = 'a6T3k0000LMJcEAO';
List<namespace__object2__c> objs = [
    SELECT Id
    FROM namespace__object2__c
    WHERE namespace__object1_id__c = :s
];

Note that s should be typed as an Id, not a String. namespace__object1_id__c should be the API name of the relationship field from Object 2 to Object 1.

2
  • getting error -- WHERE namespace__object1_id__c = :s ^ ERROR at Row:4:Column:11 No such column 'namespace__object1_id__c' on entity 'namespace__object2__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.
    – SSP
    May 6, 2020 at 16:18
  • Yes, as I stated above, you need to use the API name of the actual relationship field.
    – David Reed
    May 6, 2020 at 16:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .