function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nicholas FergusonNicholas Ferguson 

Invalid bind expression type of sObject does not match domain of foreign key

I'm attempting to write a trigger on 'before insert' and 'before update' for ObjectA__c. In my code I have a need to find related records based on a custom lookup field. 

ObjectA__c has a custom field we'll call ObjVersion__c.
ObjectB__c also has a custom field we'll also call ObjVersion__c.

Both of these lookup fields are related to a custom object 'Version__c'.

When ObjectA__c is inserted or updated, I'd like to look at the ObjVersion__c field on the update and find all the ObjectB__c that also have that relationship to that Version__c object.
 
List<ObjectB__c> areas = [
        SELECT Name, ObjVersion__c FROM ObjectB__c
        WHERE ObjVersion__c
        IN :Trigger.New];

However, I get an error stating:
Invalid bind expression type of ObjectA__c does not match domain of foreign key

What would be the proper way to accomplish this in a SOQL?
Sujeet PatelSujeet Patel
Hello Nicholas Ferguson,

I hope the below code will help you.
trigger ObjectA on ObjectA__c (before insert,before update){
	    List<String> versionId=new List<String>();
	    for(ObjectA__c obj:trigger.new){
	        versionId.add(obj.ObjVersion__c);
	    }
	    List<ObjectB__c> areas = [SELECT Name, ObjVersion__c FROM ObjectB__c
        WHERE ObjVersion__c IN :versionId];  //  This is your code where you need use some field which only contain the versionid list

	}
Thanks,
Sujeet 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Nicholas,

Can you explain your requirment clearly. What are you trying to update using trigger?

As per your query description, you can query the objectb records based on Version__c field as like below.
trigger ObjectA on ObjectA__c (before insert,before update){
	    List<id> versionId=new List<id>();
	    for(ObjectA__c obj:trigger.new){
	        versionId.add(obj.ObjVersion__c);
	    }
	    List<ObjectB__c> areas = [SELECT Name, ObjVersion__c FROM ObjectB__c
        WHERE ObjVersion__c IN :versionId]; 
		

	}



Thanks!!