You need to sign in to do that
Don't have an account?
rapcorrea
Dinamic SOQL returning "expecting a colon, found '.'"
Hi there,
So, I'm working on a batch apex which is supposed to read from a Custom_Object_A__c according to a given criteria, and update another Custom_Object_B__c.
The deal is that a long result set is returned when I query Custom_Object_A__c and I'm mapping the query results from Custom_Object_A__c into a List <String> which I use to query Custom_Object_B__c and instantiate it, so I can update the records that I need.
When I first run my batch I actually noticed a strange error message on dev console:
19:59:08:034 EXCEPTION_THROWN [69]|System.QueryException: expecting a colon, found '.'
So I decided to put a debug on my code to check the soql statement that is generated to query Custom_Object_B__c and I realized that 3 dots (...) have been added to the end of the dinamically generated list of Ids, and that might be the cause for the error I mentioned above.
Plus, as a matter of trial to figure out the cause for the error, I have put a LIMIT clause on the first query which generates the list FROM Custom_Object_A__c. I started limiting for the first 20 records and still got the error. Then I changed it to limit by 11 records and so the code ran successfully:
USER_DEBUG [67]|DEBUG|select Code__c, Record_Type__c from Custom_Object_B__c where Record_Type__c = 'Format' and StatusProcess__c = 'Processed'and Code__c IN ('CAE3PDU49372PRG_BTCH2', 'TEST_L0_81', 'ROS-0004', 'TestStagingNA1-5', 'TestStagingNA3-3', 'TEST_L0_90', 'CAE3PDU49372PRG15TEST', 'FAUST_12345', 'CAE3', 'CAE4', ...)
The character length for the dinamically generated query is 304 so this should not be the problem as salesforce states that character length limit for SOQL statemets is up to 100,000.
Still according to them, I may use 4,000 characters on SOQL WHERE clause.
Can anybody shed some light on it? Am I violating any other SOQL limit that could led to this behaviour?
Thanks in advance,
So, I'm working on a batch apex which is supposed to read from a Custom_Object_A__c according to a given criteria, and update another Custom_Object_B__c.
The deal is that a long result set is returned when I query Custom_Object_A__c and I'm mapping the query results from Custom_Object_A__c into a List <String> which I use to query Custom_Object_B__c and instantiate it, so I can update the records that I need.
When I first run my batch I actually noticed a strange error message on dev console:
19:59:08:034 EXCEPTION_THROWN [69]|System.QueryException: expecting a colon, found '.'
So I decided to put a debug on my code to check the soql statement that is generated to query Custom_Object_B__c and I realized that 3 dots (...) have been added to the end of the dinamically generated list of Ids, and that might be the cause for the error I mentioned above.
Plus, as a matter of trial to figure out the cause for the error, I have put a LIMIT clause on the first query which generates the list FROM Custom_Object_A__c. I started limiting for the first 20 records and still got the error. Then I changed it to limit by 11 records and so the code ran successfully:
USER_DEBUG [67]|DEBUG|select Code__c, Record_Type__c from Custom_Object_B__c where Record_Type__c = 'Format' and StatusProcess__c = 'Processed'and Code__c IN ('CAE3PDU49372PRG_BTCH2', 'TEST_L0_81', 'ROS-0004', 'TestStagingNA1-5', 'TestStagingNA3-3', 'TEST_L0_90', 'CAE3PDU49372PRG15TEST', 'FAUST_12345', 'CAE3', 'CAE4', ...)
The character length for the dinamically generated query is 304 so this should not be the problem as salesforce states that character length limit for SOQL statemets is up to 100,000.
Still according to them, I may use 4,000 characters on SOQL WHERE clause.
Can anybody shed some light on it? Am I violating any other SOQL limit that could led to this behaviour?
Thanks in advance,
All Answers
Here's the part of the log file as you requested. As you can see below the SOQL statement is not fully loaded and the debugger throws these 3 dots which I'm afraid are being executed with the SOQL and driving to the error.
07:45:11.0 (68120084)|SYSTEM_METHOD_EXIT|[52]|String.split(String)
07:45:11.0 (68145940)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:48
07:45:11.0 (68159063)|VARIABLE_ASSIGNMENT|[52]|this.lstString|"List of size 11 too large to display"|0x5accd4e0
07:45:11.0 (68170048)|STATEMENT_EXECUTE|[55]
07:45:11.0 (68171533)|STATEMENT_EXECUTE|[57]
07:45:11.0 (68175101)|HEAP_ALLOCATE|[57]|Bytes:15
07:45:11.0 (68192753)|SYSTEM_METHOD_ENTRY|[57]|String.valueOf(Object)
07:45:11.0 (68211720)|HEAP_ALLOCATE|[57]|Bytes:175
07:45:11.0 (68225756)|SYSTEM_METHOD_EXIT|[57]|String.valueOf(Object)
07:45:11.0 (68232211)|HEAP_ALLOCATE|[57]|Bytes:190
07:45:11.0 (68245453)|VARIABLE_ASSIGNMENT|[57]|this.queryCondition|"and Code__c IN ('CAE (170 more) ..."|0x5accd4e0
07:45:11.0 (68253630)|STATEMENT_EXECUTE|[65]
07:45:11.0 (68255980)|HEAP_ALLOCATE|[65]|Bytes:114
07:45:11.0 (68263084)|HEAP_ALLOCATE|[65]|Bytes:304
07:45:11.0 (68273941)|VARIABLE_ASSIGNMENT|[65]|this.query|"select Code__c, Reco (284 more) ..."|0x5accd4e0
07:45:11.0 (68282293)|STATEMENT_EXECUTE|[67]
07:45:11.0 (68289127)|SYSTEM_METHOD_ENTRY|[67]|System.debug(ANY)
07:45:11.0 (68302297)|USER_DEBUG|[67]|DEBUG|select Code__c, Record_Type__c from Products__c where Record_Type__c = 'Format' and StatusProcess__c = 'Processed'and Code__c IN ('CAE3PDU49372PRG_BTCH2', 'TEST_L0_81', 'ROS-0004', 'TestStagingNA1-5', 'TestStagingNA3-3', 'TEST_L0_90', 'CAE3PDU49372PRG15TEST', 'FAUST_12345', 'CAE3', 'CAE4', ...)
07:45:11.0 (68312808)|SYSTEM_METHOD_EXIT|[67]|System.debug(ANY)
07:45:11.0 (68316337)|STATEMENT_EXECUTE|[69]
07:45:11.0 (68330530)|SYSTEM_METHOD_ENTRY|[69]|Database.getQueryLocator(String)
07:45:11.0 (68626936)|EXCEPTION_THROWN|[69]|System.QueryException: expecting a colon, found '.'
07:45:11.0 (68689565)|HEAP_ALLOCATE|[69]|Bytes:32
07:45:11.0 (68700798)|SYSTEM_METHOD_EXIT|[69]|Database.getQueryLocator(String)
07:45:11.0 (68767117)|FATAL_ERROR|System.QueryException: expecting a colon, found '.'
Class.BatchUpdProdStagStat_RefPriceItem.start: line 69, column 1
07:45:11.0 (195694677)|FATAL_ERROR|System.QueryException: expecting a colon, found '.'
Here's my code for your reference:
Here you can see 'Processed'and don't have space between StatusProcess__c = 'Processed' and and.
So the quick fix is replace the line number 52(in the above code snippet) with the below one:
#soql
I just tried that but didn't work either =/
Here's the query as it was generated now and as you can see there's a space between 'Processed' and 'AND':
USER_DEBUG [67]|DEBUG|select Code__c, Record_Type__c from Products__c where Record_Type__c = 'Format' and StatusProcess__c = 'Processed' and Code__c IN ('CAE3PDU49372PRG_BTCH2', 'TEST_L0_81', 'ROS-0004', 'TestStagingNA1-5', 'TestStagingNA3-3', 'TEST_L0_90', 'CAE3PDU49372PRG15TEST', 'FAUST_12345', 'CAE3', 'CAE4', ...)
Still getting the same error and those 3 dots at the end of the line are still there:
08:26:43:039 FATAL_ERROR System.QueryException: expecting a colon, found '.'
Any other thought?
The query I've previously sent is from the log file.. I cannot send the whole log because it's too long:
Find below the part of the log where the generated query is marked in BOLD:
08:26:43.0 (38227114)|USER_DEBUG|[47]|DEBUG|'CAE3PDU49372PRG_BTCH2', 'TEST_L0_81', 'ROS-0004', 'TestStagingNA1-5', 'TestStagingNA3-3', 'TEST_L0_90', 'CAE3PDU49372PRG15TEST', 'FAUST_12345', 'CAE3', 'CAE4', 'CAE48'
08:26:43.0 (38232630)|SYSTEM_METHOD_EXIT|[47]|System.debug(ANY)
08:26:43.0 (38237826)|SYSTEM_METHOD_ENTRY|[21]|system.ListIterator.hasNext()
08:26:43.0 (38244479)|HEAP_ALLOCATE|[21]|Bytes:5
08:26:43.0 (38249908)|SYSTEM_METHOD_EXIT|[21]|system.ListIterator.hasNext()
08:26:43.0 (38259598)|VARIABLE_ASSIGNMENT|[21]|prd|null|
08:26:43.0 (38265084)|STATEMENT_EXECUTE|[52]
08:26:43.0 (38268474)|HEAP_ALLOCATE|[52]|Bytes:1
08:26:43.0 (38283624)|SYSTEM_METHOD_ENTRY|[52]|String.split(String)
08:26:43.0 (38311107)|HEAP_ALLOCATE|[52]|Bytes:23
08:26:43.0 (38315500)|HEAP_ALLOCATE|[52]|Bytes:13
08:26:43.0 (38318387)|HEAP_ALLOCATE|[52]|Bytes:11
08:26:43.0 (38321270)|HEAP_ALLOCATE|[52]|Bytes:19
08:26:43.0 (38324016)|HEAP_ALLOCATE|[52]|Bytes:19
08:26:43.0 (38326649)|HEAP_ALLOCATE|[52]|Bytes:13
08:26:43.0 (38329532)|HEAP_ALLOCATE|[52]|Bytes:24
08:26:43.0 (38332144)|HEAP_ALLOCATE|[52]|Bytes:14
08:26:43.0 (38334682)|HEAP_ALLOCATE|[52]|Bytes:7
08:26:43.0 (38337388)|HEAP_ALLOCATE|[52]|Bytes:7
08:26:43.0 (38340419)|HEAP_ALLOCATE|[52]|Bytes:9
08:26:43.0 (38344162)|HEAP_ALLOCATE|[52]|Bytes:48
08:26:43.0 (38358068)|SYSTEM_METHOD_EXIT|[52]|String.split(String)
08:26:43.0 (38383857)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:48
08:26:43.0 (38396023)|VARIABLE_ASSIGNMENT|[52]|this.lstString|"List of size 11 too large to display"|0xc2c0fb9
08:26:43.0 (38403941)|STATEMENT_EXECUTE|[55]
08:26:43.0 (38405457)|STATEMENT_EXECUTE|[57]
08:26:43.0 (38408830)|HEAP_ALLOCATE|[57]|Bytes:15
08:26:43.0 (38424597)|SYSTEM_METHOD_ENTRY|[57]|String.valueOf(Object)
08:26:43.0 (38440722)|HEAP_ALLOCATE|[57]|Bytes:175
08:26:43.0 (38451072)|SYSTEM_METHOD_EXIT|[57]|String.valueOf(Object)
08:26:43.0 (38457440)|HEAP_ALLOCATE|[57]|Bytes:190
08:26:43.0 (38469189)|VARIABLE_ASSIGNMENT|[57]|this.queryCondition|"and Code__c IN ('CAE (170 more) ..."|0xc2c0fb9
08:26:43.0 (38475698)|STATEMENT_EXECUTE|[65]
08:26:43.0 (38477972)|HEAP_ALLOCATE|[65]|Bytes:115
08:26:43.0 (38484185)|HEAP_ALLOCATE|[65]|Bytes:305
08:26:43.0 (38493670)|VARIABLE_ASSIGNMENT|[65]|this.query|"select Code__c, Reco (285 more) ..."|0xc2c0fb9
08:26:43.0 (38499606)|STATEMENT_EXECUTE|[67]
08:26:43.0 (38505905)|SYSTEM_METHOD_ENTRY|[67]|System.debug(ANY)
08:26:43.0 (38515674)|USER_DEBUG|[67]|DEBUG|select Code__c, Record_Type__c from Products__c where Record_Type__c = 'Format' and StatusProcess__c = 'Processed' and Code__c IN ('CAE3PDU49372PRG_BTCH2', 'TEST_L0_81', 'ROS-0004', 'TestStagingNA1-5', 'TestStagingNA3-3', 'TEST_L0_90', 'CAE3PDU49372PRG15TEST', 'FAUST_12345', 'CAE3', 'CAE4', ...)
08:26:43.0 (38523481)|SYSTEM_METHOD_EXIT|[67]|System.debug(ANY)
08:26:43.0 (38526903)|STATEMENT_EXECUTE|[69]
08:26:43.0 (38538014)|SYSTEM_METHOD_ENTRY|[69]|Database.getQueryLocator(String)
08:26:43.0 (39144933)|EXCEPTION_THROWN|[69]|System.QueryException: expecting a colon, found '.'
08:26:43.0 (39255442)|HEAP_ALLOCATE|[69]|Bytes:32
08:26:43.0 (39267063)|SYSTEM_METHOD_EXIT|[69]|Database.getQueryLocator(String)
08:26:43.0 (39373854)|FATAL_ERROR|System.QueryException: expecting a colon, found '.'
Class.BatchUpdProdStagStat_RefPriceItem.start: line 69, column 1
08:26:43.0 (81310886)|FATAL_ERROR|System.QueryException: expecting a colon, found '.'
Class.BatchUpdProdStagStat_RefPriceItem.start: line 69, column 1
I hardcoded the value for queryCondition variable as below to see if I could run it and no error was returned:
Please, notice that I've tried that just to see if I'm not violating any SOQL limit, but I still have to fix the problem when the queryCondition value is dinamically generated.
Apparently, the split method is causing me this issue. I have replaced the lstString variable on queryCondition by listParamfinal, which is the String variable before having the split treatment applied and then the values assigned to List<String> lstString variable.
So now, I wanted to know what are the risks by using this parameter as String, without transforming it in a List<String>.
Could you share your thoughts on that?
And for the just create a class level property and bind that in query.
And the current code snippet don't have all the data, so it it fine for you to share whole class code, that I can review and suggest you to change.
Your help is really appreciated. I'm not familiar with pushing values in a set, and spliting the string by comma and pushing it inside a List<String> was the easier way I found, but it also led me to this issue. It would be nice to have some suggestion, so If you can have a look and let me know what changes would make this code better....
Cheers!
#soql #bind-variables #sql-injection