Tell me more ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

This question already has an answer here:

I have a set of IDs I wanted excluded from a Task query. I need to use dynamic sql for this. I try:

Set <ID> unwantedIds = ...  // ids I don't want.
String query = 'select Id, ThirdPartyId__c, whatId FROM Task where ThirdPartyId__c not in :' + objectiveIds;
... 

I get System.QueryException: unexpected token '{'

Any ideas?

share|improve this question
 
what is objectiveIds ? –  Prady Mar 7 at 11:49
 
the code you posted doesn't have a { at all –  PJC Mar 7 at 11:52

marked as duplicate by PJC, metadaddy Mar 7 at 15:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

You could try this

Set <ID> unwantedIds = ...  // ids I don't want.
String query = 'select Id, ThirdPartyId__c, whatId FROM Task where ThirdPartyId__c not in :unwantedIds ' ;

Not sure what you are looking for .. If you want to build a dynamic query to exclude the unwantedids this should work

share|improve this answer

I was able to fix this by just building up a string of IDs...

String idList = '';
for (Id id : objectiveIds) {
   idList = idList + ',\'' + id + '\'';
}

and appending that to the dynamic soql as opposed to using a variable. I guess it makes sense that bound variables should not work with dynamic soql and it was thus a bit of a stupid question.

share|improve this answer
 
actually it is better to go with @Prady's response, his reduces the amount of script statements used –  PJC Mar 7 at 12:14
 
Use Prady's solution, it is much better. –  mast0r Mar 7 at 12:21
 
I am going to delete this question. It's a mess. Sorry my fault –  dublintech Mar 7 at 12:22

Why use dynamic SOQL at all in this case? If this is in Apex, and your example above is accurate, you shouldn't need to. Think you'd be better off with something like:

List<Task> myTasks = [Select Id,ThirdPartyId__c,WhatId FROM Task where ThirdPartyId__c not in :objectiveIds];

I just tried the following via workbench, and it worked as intended:

Set<Id> contacts = new Set<Id>();
for(Contact c : [Select Id From Contact Limit 10]){
    contacts.add(c.Id);
}
List<Contact> allButFirst10Contacts = [Select Id From Contact Where Id Not In :contacts];
System.debug(allButFirst10Contacts.size());
share|improve this answer

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