You need to sign in to do that
Don't have an account?
Randombard2
First error: Only variable references are allowed in dynamic SOQL/SOSL.
Hi All,
I am trying to start a batch process from a trigger, but cant get the where cluase to pass in the query string.
Now I know its passing the ID's because if I remove the : I get the error:
Any ideas?
R
First error: expecting a colon, found 'anidishere'
I am trying to start a batch process from a trigger, but cant get the where cluase to pass in the query string.
BuildIBCall reassign = new BuildIBCall(); reassign.email='notmyemail@idontworkhere.com'; String sql = 'select Account__c, from OB__c Where ID IN:'; String idString= '\''+String.join(IBToUpdate,'\',\'')+'\''; Sql+= idString; reassign.query= Sql; ID batchprocessid = Database.executeBatch(reassign, 30);
Now I know its passing the ID's because if I remove the : I get the error:
Any ideas?
R
First error: expecting a colon, found 'anidishere'
I went with a combination of the 2
Thanks both for all of your help.
All Answers
You can simple use set in the dynamic query instead of forming comma separated string and then using it:
String sql = 'select Account__c, from OB__c Where ID IN: setIds';
Thanks, but then I get:
First error: Variable does not exist: IBToUpdate
It clearly does in the trigger, and I can make it public (not global as its a trigger not a class) but it still fails.
R
First error: unexpected token: '{'
Can you share your complete code with me please?
I am not sure I can sanitize it enough while leaving it meaningful.
Thanks for all of the help so far.
R
IBToUpdate as a list
IBToUpdate as a set
Converting IBToUpdate to a string before adding it to the end of the query- First error: Only variable references are allowed in dynamic SOQL/SOSL.
Using + IBToUpdate at the end of the query -First error: unexpected token: '{'
Putting IBToUpdate into the query - First error: Variable does not exist: IBToUpdate
Is all of this because I am trying to do this from a trigger?
R
Don't if you using array of Ids if that is situation add those ids to a set using for loop.
set<Id> ids = new set<Id>();
ids.add(IBToUpdate)
String Sql = 'select Account__c, from OB__c Where ID IN: ' +ids;
Or If you are using single Id
String Sql = 'select Account__c, from OB__c Where ID =: ' +IBToUpdate;
I am using an array of ID's from your first method.
R
Replace your code with my code, you forget to give space after IN: ' instead of you have given IN:'
once post your entire code please the I can give accurate answer
Please use below mentioned code. I have converted your list to set since list cannot be referenced within dynamic soql so we must first convert this to set and then use it.
BuildIBCall reassign = new BuildIBCall(); reassign.email='notmyemail@idontworkhere.com'; Set<Id> setIds = new Set<Id>(); setIds.addAll(IBToUpdate); String sql = 'select Account__c, from OB__c Where ID IN: setIds'; reassign.query= Sql; ID batchprocessid = Database.executeBatch(reassign, 30);
Let us know if you find any issues.
I am afraid:
First error: Variable does not exist: setIds
R
First error: Variable does not exist: a0z9E000000AkdTnA0
I got this problem. You will also have to pass the set to batch class since we have declared set in trigger and soql is complied in batch apex. Please add one more parameter in batch apex of type set<Id> and then execute the batch.
please can you show me what you mean.
Thanks
R
You will have to create a variable of type set<Id> in batch class and then initialize it with the set(i.e. setIds) at the time of creating an instance(reassign).
I meant what does that look like int eh batch class please.
I assume something like
Global set setIds;
R
I went with a combination of the 2
Thanks both for all of your help.