0

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?

2
  • what is objectiveIds ?
    – Prady
    Mar 7, 2013 at 11:49
  • the code you posted doesn't have a { at all
    – pjcarly
    Mar 7, 2013 at 11:52

3 Answers 3

2

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

0

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.

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

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());

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