0

I'm getting this error "Invalid Bind expression type of Class_Detail__c for id field SObject Account"

Class_Detail__c.Student__c is a Master-Detail relationship field to Account. Basically I want to select a list of students from Account object using the ids (Student__c) from the Class_Detail__c custom object. What am I doing wrong?

id classid = lstOfPI[0].iClassID;
id teacherid =  lstOfPI[0].iTeacherid;


List<Class_Detail__c>  lstClass= [Select student__c from Class_Detail__c WHERE ClassID__c = :classid];

List<Account> lstStudentProfilesToUpdate = [SELECT Id,Teacher__c FROM Account WHERE id in :lstClass];
  • Can you sketch the data model and explain what you want in a more precise maner.The explanation you have is not clear and this post might get down votes . – Mohith Shrivastava Sep 30 '18 at 19:08
1

The value that you're providing in your Apex bind expression is of the wrong type.

List<Account> lstStudentProfilesToUpdate = [
    SELECT Id, Teacher__c 
    FROM Account 
    WHERE id in :lstClass
];

Here, lstClass is a List<Class_Detail__c>. Your bind variable for the Id column on Account must be a collection of Id values.

You need to iterate over your collection of Class_Detail__c records to accumulate their associated Student__c field values, which if I understand you correctly is the lookup field pointing to Account.

List<Class_Detail__c>  lstClass= [Select Student__c from Class_Detail__c WHERE ClassID__c = :classid];

Set<Id> accountIds = new Set<Id>();
for (Class_Detail__c s : lstClass) {
    accountIds.add(s.Student__c);
}

Then bind to accountIds.

Alternately, if you don't need lstClass for some other purpose, you can simply use an IN subquery here, because you're matching columns of type Id that refer to the same object:

List<Account> lstStudentProfilesToUpdate = [
    SELECT Id, Teacher__c 
    FROM Account 
    WHERE Id in (
        SELECT Student__c 
        FROM Class_Detail__c 
        WHERE ClassID__c = :classid
    )
];
  • As a side note, if your WHERE clause were to filter on the Id value of the same object you were querying, binding the List<Class_Detail__c> would have worked. I'm not 100% sure if you would be able to use a List<Account> to bind to ClassId__c or not, but the big point here is that if you're binding a List<SObject>, the subtype of SObject needs to match the SObject that your relationship value points to. – Derek F Sep 30 '18 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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