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
rapcorrearapcorrea 

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,
Best Answer chosen by rapcorrea
itzmukeshy7itzmukeshy7
Try this;
global class BatchUpdProdStagStat_RefPriceItem implements Database.Batchable<sObject>, Database.Stateful {

  global String listParam = '';
  /* Changed it to set string from string. */
  global Set<String> listParamfinal;
  global List<String> lstString = new List<String>();
  global Integer varCount = 1;
  global Integer varCountParent = 0;
  global String query = '';
  global String queryCondition = '';

  global Database.QueryLocator start(Database.BatchableContext scope) {
    listParamfinal = new Set<String>();

    List<Catalog_Item__c> record = [select External_Id__c from Catalog_Item__c where External_Id__c != null];
    List<Map<String, Object>> listTotal = new List<Map<String, Object>>();
    Integer recordsCount = record.size();
    for(Catalog_Item__c prd : record){
      listParam = String.valueof(prd.get('External_Id__c'));

      if(recordsCount > 1){
        if (varCount < recordsCount){
          /* Adding item to set. */
          listParamfinal.add(listParam);
        }else if (varCount == recordsCount){
          /* Adding item to set. */
          listParamfinal.add(listParam);
        }
      }else{
        /* Adding item to set. */
        listParamfinal.add(listParam);
      }

      varCount = varCount + 1;
      System.debug(varCount);
      System.debug('listParamfinal: ' + listParamfinal);
    }

    /* If set has any item then add the condition to query. */
    if(!listParamfinal.isEmpty()){
      queryCondition = ' AND Code__c IN :listParamfinal';
    }

    query = 'select Code__c, Record_Type__c from Products__c where Record_Type__c = \'Format\' and StatusProcess__c = \'Processed\' ' + queryCondition;
    system.debug(query);
    return Database.getQueryLocator(query);
  }

  /* no change below this line. */
  global void execute(Database.BatchableContext scope, List<Products__c> lstProducts ){
    List<Products__c> Prod2Update = new List<Products__c>();
    for (Products__c varProd2Update : lstProducts) {
      varProd2Update.StatusMessage__c = 'The staging record was created on Catalog Item as a Leaf and it should have at least one associated Price Item record with attribute Reference Price = TRUE';
      Prod2Update.add(varProd2Update);
      varCountParent = varCountParent + 1;
    }
    update Prod2Update;
  }

  global void finish(Database.BatchableContext scope ){
  }
}

 

All Answers

itzmukeshy7itzmukeshy7
Can you download to your local and then you will get the whole query in the debug, and finally you can share the query here, also if you can share the SOQL from apex that might also be helpful to fix it.
rapcorrearapcorrea
Hello!

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:
global Database.QueryLocator start(Database.BatchableContext scope) {       

        
//      List<Products__c> record = [select Code__c, Record_Type__c from Products__c where Record_Type__c = 'Format' and StatusProcess__c = 'Processed'];
//      Related_Price_Itens_Ref_True__c < 1 and
        List<Catalog_Item__c> record = [select External_Id__c from Catalog_Item__c where External_Id__c != null LIMIT 11];
        List<Map<String, Object>> listTotal = new List<Map<String, Object>>();
        
        for(Catalog_Item__c prd : record)
        {

            listParam = String.valueof(prd.get('External_Id__c'));

            if ( record.size() > 1){             
                if (varCount < record.size()){
                    
                    listParam = '\''+ String.escapeSingleQuotes(listParam) + '\', ';                    
                    listParamfinal = listParamfinal + listParam;

                    
                }else if (varCount == record.size()){
                    listParam = '\''+ String.escapeSingleQuotes(listParam) + '\' ';
                    listParamfinal = listParamfinal + listParam;

                    }
            }else{
                listParam = '\''+ String.escapeSingleQuotes(listParam) + '\'';
                listParamfinal = listParamfinal + listParam;


            }
            
            varCount = varCount + 1;
            System.debug(varCount);         
            System.debug(listParamfinal);

            
        }              
        
        lstString = listParamfinal.split(',');
//      System.debug('Lista: ' + lstString);

        if(varCount > 1){

            queryCondition = 'and Code__c IN '+ lstString;           
            
        }else 
        if(varCount == 1){
            queryCondition = 'and Code__c = '+ lstString;
        }else query = '';
        
        query = 'select Code__c, Record_Type__c from Products__c where Record_Type__c = \'Format\' and StatusProcess__c = \'Processed\'' +queryCondition;

        system.debug(query);
        
        return Database.getQueryLocator(query);

 
itzmukeshy7itzmukeshy7
In the query:
query = 'select Code__c, Record_Type__c from Products__c where Record_Type__c = \'Format\' and StatusProcess__c = \'Processed\'' +queryCondition;
Put an space before appending the queryCondition to the query.
 
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', ...)

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:
 
query = 'select Code__c, Record_Type__c from Products__c where Record_Type__c = \'Format\' and StatusProcess__c = \'Processed\' ' + queryCondition;

#soql
rapcorrearapcorrea
Hi Mukesh,

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?
itzmukeshy7itzmukeshy7
Really sorry to hear that, but I can't do anything until I see the whole query, as I said earlier can you please download the log file to local and get the complete query from the downloaded file and share that query here.
itzmukeshy7itzmukeshy7
To download the debug log go to developer console, execute the batch and then get the log for query and then download the same(right click on the debug log and download the same).
Download log from developer console.
rapcorrearapcorrea
Hi Mukesh,

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


 
rapcorrearapcorrea
I just tried the following:

I hardcoded the value for queryCondition variable as below to see if I could run it and no error was returned:
queryCondition = ' and Code__c IN (\'CAE3PDU49372PRG_BTCH2\', \'TEST_L0_81\', \'ROS-0004\', \'TestStagingNA1-5\', \'TestStagingNA3-3\', \'TEST_L0_90\', \'CAE3PDU49372PRG15TEST\', \'FAUST_12345\', \'CAE3\', \'CAE4\', \'CAE48\', \'CAE3PDU49372PRG_BTCH2\', \'TEST_L0_81\', \'ROS-0004\', \'TestStagingNA1-5\', \'TestStagingNA3-3\', \'TEST_L0_90\', \'CAE3PDU49372PRG15TEST\', \'FAUST_12345\', \'CAE3\', \'CAE4\', \'CAE48\')';
        query = 'select Code__c, Record_Type__c from Products__c where Record_Type__c = \'Format\' and StatusProcess__c = \'Processed\' ' +queryCondition;
        system.debug(query);
        
        return Database.getQueryLocator(query);

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.
rapcorrearapcorrea
Mukesh,

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?
lstString = listParamfinal.split(',');
		System.debug('Lista: ' + lstString);

        if(varCount > 1){

//            queryCondition = 'and Code__c IN '+ lstString;
            queryCondition = 'and Code__c IN ('+ listParamfinal + ')';
            
            
        }else 
        if(varCount == 1){
//            queryCondition = 'and Code__c = '+ lstString;
            queryCondition = 'and Code__c = '+ listParamfinal;          
        }else query = '';


 
itzmukeshy7itzmukeshy7
I am not sure why you are doing this all string concatations instead can't we just push all the in values in a set or list and bind that variable directly in the query.
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.
rapcorrearapcorrea
Thanks, Mukesh!
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....
 
global class BatchUpdProdStagStat_RefPriceItem implements Database.Batchable<sObject>, Database.Stateful {
    
    
    global String listParam = '';
    global String listParamfinal = '' ;
    global List<String> lstString = new List<String>();
    global Integer varCount =1;
    global Integer varCountParent = 0;
    global String query = '';
    global String queryCondition = '';
   
    global Database.QueryLocator start(Database.BatchableContext scope) {       

        

        List<Catalog_Item__c> record = [select External_Id__c from Catalog_Item__c where External_Id__c != null];
        List<Map<String, Object>> listTotal = new List<Map<String, Object>>();
        
        for(Catalog_Item__c prd : record)
        {

            listParam = String.valueof(prd.get('External_Id__c'));

            if ( record.size() > 1){             
                if (varCount < record.size()){
                    
                    listParam = '\''+ String.escapeSingleQuotes(listParam) + '\', ';                    
                    listParamfinal = listParamfinal + listParam;

                    
                }else if (varCount == record.size()){
                    listParam = '\''+ String.escapeSingleQuotes(listParam) + '\' ';
                    listParamfinal = listParamfinal + listParam;

                    }
            }else{
                listParam = '\''+ String.escapeSingleQuotes(listParam) + '\'';
                listParamfinal = listParamfinal + listParam;


            }
            
            varCount = varCount + 1;
            System.debug(varCount);         
            System.debug('listParamfinal '+listParamfinal);

            
        }              
        

        if(varCount > 1){

            queryCondition = 'and Code__c IN ('+ listParamfinal + ')';
            
            
        }else 
        if(varCount == 1){
            queryCondition = 'and Code__c = '+ listParamfinal;          
        }else query = '';
        


  
        query = 'select Code__c, Record_Type__c from Products__c where Record_Type__c = \'Format\' and StatusProcess__c = \'Processed\' ' +queryCondition;
        system.debug(query);
        
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext scope, List<Products__c> lstProducts ){
        
        List<Products__c> Prod2Update = new List<Products__c>();
        
        for (Products__c varProd2Update : lstProducts) {
            
            varProd2Update.StatusMessage__c = 'The staging record was created on Catalog Item as a Leaf and it should have at least one associated Price Item record with attribute Reference Price = TRUE';
            Prod2Update.add(varProd2Update);
            varCountParent = varCountParent + 1;
                                      
        }
        
        update Prod2Update;
        
 
    }    
    global void finish(Database.BatchableContext scope ){

    }    
}

 
itzmukeshy7itzmukeshy7
Try this;
global class BatchUpdProdStagStat_RefPriceItem implements Database.Batchable<sObject>, Database.Stateful {

  global String listParam = '';
  /* Changed it to set string from string. */
  global Set<String> listParamfinal;
  global List<String> lstString = new List<String>();
  global Integer varCount = 1;
  global Integer varCountParent = 0;
  global String query = '';
  global String queryCondition = '';

  global Database.QueryLocator start(Database.BatchableContext scope) {
    listParamfinal = new Set<String>();

    List<Catalog_Item__c> record = [select External_Id__c from Catalog_Item__c where External_Id__c != null];
    List<Map<String, Object>> listTotal = new List<Map<String, Object>>();
    Integer recordsCount = record.size();
    for(Catalog_Item__c prd : record){
      listParam = String.valueof(prd.get('External_Id__c'));

      if(recordsCount > 1){
        if (varCount < recordsCount){
          /* Adding item to set. */
          listParamfinal.add(listParam);
        }else if (varCount == recordsCount){
          /* Adding item to set. */
          listParamfinal.add(listParam);
        }
      }else{
        /* Adding item to set. */
        listParamfinal.add(listParam);
      }

      varCount = varCount + 1;
      System.debug(varCount);
      System.debug('listParamfinal: ' + listParamfinal);
    }

    /* If set has any item then add the condition to query. */
    if(!listParamfinal.isEmpty()){
      queryCondition = ' AND Code__c IN :listParamfinal';
    }

    query = 'select Code__c, Record_Type__c from Products__c where Record_Type__c = \'Format\' and StatusProcess__c = \'Processed\' ' + queryCondition;
    system.debug(query);
    return Database.getQueryLocator(query);
  }

  /* no change below this line. */
  global void execute(Database.BatchableContext scope, List<Products__c> lstProducts ){
    List<Products__c> Prod2Update = new List<Products__c>();
    for (Products__c varProd2Update : lstProducts) {
      varProd2Update.StatusMessage__c = 'The staging record was created on Catalog Item as a Leaf and it should have at least one associated Price Item record with attribute Reference Price = TRUE';
      Prod2Update.add(varProd2Update);
      varCountParent = varCountParent + 1;
    }
    update Prod2Update;
  }

  global void finish(Database.BatchableContext scope ){
  }
}

 
This was selected as the best answer
rapcorrearapcorrea
Thank you so much! It works perfectly and I got the point here!

Cheers!
itzmukeshy7itzmukeshy7
Yes, always try to bind the values in query instead of string concatenations that causes problem in most of the cases, and with the set/list collection we are very helpful with the IN conditions.

#soql #bind-variables #sql-injection