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
ArchieTechie6ArchieTechie6 

SOQL Query using IN Operator returns error : Invalid bind expression type of ProcessInstance for Id field of SObject

Hello All,

I want to fetch items which are pending approvals. For this, I am querying from ProcessInstance.
Once I get the Ids from ProcessInstance, I want to query fields from my object by passing these IDs.

Below is my code:
public List<ProcessInstance> draftKBIds          {get;set;}
public List<Knowledge__kav> pendingArticles  {get;set;}

//Below query returns expected results from ProcessInstance
draftKBIds  = [select Id from ProcessInstance where TargetObjectId IN(select Id from Knowledge__KAV where PublishStatus='Draft')];
//Knowledge__KAV is the name of my Knowledge Base Object.
 pendingArticles  = [Select id, Title
                                from Knowledge__KAV ​where id IN: draftKBIds ];

The above highlighted query returns error: 

 Invalid bind expression type of ProcessInstance for Id field of SObject Knowledge__kav

Please suggest where I am going wrong. 
Prady01Prady01
Hi there, Try the below.

pendingArticles  = [Select id, Title from Knowledge__KAV ​where id IN=: draftKBIds ];

Thank you
PSM
ArchieTechie6ArchieTechie6
I have tried the suggested code provided by you. Now, I get this error:

Compile Error: line 39:60 no viable alternative at character '​' at line 39 column 60

Please help! Thank you!
Prady01Prady01
Hi there apologies,  "=:" this is wrong but i have ran the below code in console and it works pleas try.
 
List<Id> atIds = new List<Id>();
List<Article__kav> ArticleLst= new List<Article__kav>([select id from Article__kav LIMIT 10]);
for(Article__kav at: ArticleLst){
    atIds.add(at.id);
}
List<Article__kav> ArLst = new List<Article__kav>();
ArLst = [select id from Article__kav where id in:atIds];
system.debug('ArLst '+ ArLst);

Thank you
PSM 
Prady01Prady01
Hooooo ok understood your problem. Ha-ah: 

 Invalid bind expression type of ProcessInstance for Id field of SObject Knowledge__kav ==> This is error in the where condition below is why.

draftKBIds  = [select Id from ProcessInstance where TargetObjectId IN(select Id from Knowledge__KAV where PublishStatus='Draft')];

The above query holds id's of ProcessInstance and not the Knowledge__KAV. That's the reason why in the second query its not recognisizng.

 pendingArticles  = [Select id, Title from Knowledge__KAV ​where id IN: draftKBIds ]; Here we will have to pass the Knowledge__KAV ID'S.

Thank you
PSM

 
ArchieTechie6ArchieTechie6
I am doing the following declarations:

public List<ProcessInstance> draftKBIds          {get;set;}
public List<Knowledge__kav> pendingArticles  {get;set;}

If I dont give public List<ProcessInstance> draftKBIds for draftKBIds, that particular SOQL query goes wrong. 
Please suggest here ---