0

I have built dynamic query.but receiving below exception

 01:16:56.0 (15683245)|USER_DEBUG|[335]|DEBUG|SetOfParentId--->{}
  01:16:56.0 (15756602)|USER_DEBUG|[336]|DEBUG|Set1--->{}
  01:16:56.0 (15788512)|USER_DEBUG|[337]|DEBUG|SetOfGSSDuplicateCase--->{}
  01:16:56.0 (15816995)|USER_DEBUG|[338]|DEBUG|Set11--->{}
  01:16:56.0 (15864603)|USER_DEBUG|[404]|DEBUG|soqlString---->select id,Vmstar_Id__c,GSS_Duplicate_Case__c,Duplicate_Of_Case__c,GSS_Build_Number__c, 
   GSS_Product_Version__c from Case Where 
      01:16:56.0 (16227040)|EXCEPTION_THROWN|[405]|System.QueryException: 
         unexpected token: '<EOF>'
             01:16:56.0 (16357113)|FATAL_ERROR|System.QueryException: 
          unexpected token: '<EOF>'


**Dynamic query:**


 string soqlString = 'select id,Vmstar_Id__c,GSS_Duplicate_Case__c,Duplicate_Of_Case__c,GSS_Build_Number__c,GSS_Product_Version__c from Case';
        soqlString += ' Where '; 
        boolean whereClauseAdded = false;
        if(SetOfParentId.size()>0){
            whereClauseAdded = true;
            soqlString += ' Vmstar_Id__c IN : SetOfParentId';
        }
        if(SetOfGSSDuplicateCase.size()>0){
            if(whereClauseAdded)
                soqlString += ' OR Vmstar_Id__c IN : SetOfGSSDuplicateCase';
            else 
                soqlString += ' Vmstar_Id__c IN : SetOfGSSDuplicateCase';

            whereClauseAdded = true;
        }
        if(Set1.size()>0 ){

            if(whereClauseAdded)
                soqlString += ' OR GSS_Build_Number__c IN :  Set1';
            else 
                soqlString += ' GSS_Build_Number__c IN :  Set1';

            whereClauseAdded = true;


        }
        if(Set11.size()>0){

            if(whereClauseAdded)
                soqlString += ' OR GSS_Product_Version__c  IN : Set11';
            else 
                soqlString += ' GSS_Product_Version__c  IN : Set11';

            whereClauseAdded = true;
             }

        system.debug('soqlString---->'+soqlString);
        List<Case> requiredCases = Database.query(soqlString);


        if(requiredCases.size()>0){

        for(Case et : requiredCases){

            if(et.id!=null){
                MapOfParentId.put(et.Vmstar_Id__c,et.id);
            }if(et.GSS_Duplicate_Case__c!=null){
                MapOfGSSDuplicateCase.put(et.VMstar_Id__c,et.GSS_Duplicate_Case__c);              
            }if(et.Duplicate_Of_Case__c!=null){
                MapOfDuplicateOfCase.put(et.VMstar_Id__c,et.Duplicate_Of_Case__c);
            }if(et.GSS_Build_Number__c!=null){
                Set3.add(et.GSS_Build_Number__c);
            }if(et.GSS_Product_Version__c!=null){
                Set33.add(et.GSS_Build_Number__c);
            }

          }  
        }
3

You need add 'WHERE' to your conditions. Because query is ending with

... where

and nothing after it, the result is error.

soqlString += ' Where ';

is always added, even if nothing is after it.

  • 2
    +1 In addition, the space in : Set11 should also cause errors. – sfdcfox Apr 23 at 8:29
0

You are getting this issue because you are adding the WHERE clause with all of your sets size equal to 0. Hence your SOQL query is left with WHERE clause without any conditions which leaves your SOQL query incomplete and hence you are getting System.QueryException.

You will have to add the WHERE clause only when one of your set has any value in it. So, your code should be something like below

Also, make sure you don't have space between colon and the set variable name

string soqlString = 'select id,Vmstar_Id__c,GSS_Duplicate_Case__c,Duplicate_Of_Case__c,GSS_Build_Number__c,GSS_Product_Version__c from Case';
if(SetOfParentId.size()>0 || SetOfGSSDuplicateCase.size()>0 || Set1.size()>0 || Set11.size()>0){
    soqlString += ' Where ' 
}
boolean whereClauseAdded = false;
if(SetOfParentId.size()>0){
    whereClauseAdded = true;
    soqlString += ' Vmstar_Id__c IN :SetOfParentId';
}
if(SetOfGSSDuplicateCase.size()>0){
    if(whereClauseAdded)
        soqlString += ' OR Vmstar_Id__c IN :SetOfGSSDuplicateCase';
    else 
        soqlString += ' Vmstar_Id__c IN :SetOfGSSDuplicateCase';

    whereClauseAdded = true;
}

if(Set1.size()>0){
    if(whereClauseAdded)
        soqlString += ' OR Vmstar_Id__c IN :Set1';
    else 
        soqlString += ' Vmstar_Id__c IN :Set1';

    whereClauseAdded = true;
}

if(Set11.size()>0){
    if(whereClauseAdded)
        soqlString += ' OR Vmstar_Id__c IN :Set11';
    else 
        soqlString += ' Vmstar_Id__c IN :Set11';

    whereClauseAdded = true;
}

//query the records using Database.query method
List<Case> requiredCases = Database.query(soqlString);

for(Case et : requiredCases){
    if(et.id!=null){
        MapOfParentId.put(et.Vmstar_Id__c,et.id);
    }
    if(et.GSS_Duplicate_Case__c!=null){
        MapOfGSSDuplicateCase.put(et.VMstar_Id__c,et.GSS_Duplicate_Case__c);              
    }
    if(et.Duplicate_Of_Case__c!=null){
        MapOfDuplicateOfCase.put(et.VMstar_Id__c,et.Duplicate_Of_Case__c);
    }
    if(et.GSS_Build_Number__c!=null){
        Set3.add(et.GSS_Build_Number__c);
    }
    if(et.GSS_Product_Version__c!=null){
        Set33.add(et.GSS_Build_Number__c);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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