-1
  for(AggregateResult hier:hierarchy) {
    fld1.add((String)hier.get(Field1));
    fld2.add((String)hier.get(Field2));
    fld3.add((String)hier.get(Field3));
           }

        string Soqlstr2='Select Name FROM' + val + ' WHERE ' + Field1 + ' IN :\'' + fld1 + '\' AND ' +
            Field2 + ' IN :\'' + fld2 + '\' AND ' +
            Field3 + ' IN :\'' + fld3 + '\' AND ' +'\'';

I am getting an error :

Unexpected Token Where .

3

Your query is missing a space after FROM. The resulting query would look like

SELECT Name FROMAccount WHERE ...

This is a very easy error to make, and is why I always recommend using String.format() to build Dynamic SOQL queries, as I mentioned on one of your previous questions.

You are also using Apex binding incorrectly. An Apex bind takes the form :VARIABLE, as in WHERE My_Field__c IN :collectionOfValues. Your query adds the literal string value of the variable after the binding colon, which will produce confusing errors at run time.

I strongly recommend that you take the time to read through the entirety of Salesforce's SOSL and SOQL Reference. Your questions indicate that you are working on a project involving a lot of complex querying. You need to establish a foundation in how these query languages work in order to do so effectively.

0

Whenever you create a dynamic query via string concatenation there is always a scope for such error. A better way to do this will be to use String.format which gives you templated string that you can replace. It makes your query more readable.

String Soqlstr2='SELECT Name FROM {0} WHERE {1} = :{2} AND  {3} = :{4} ';

String SOQLqueryFinal = String.format(Soqlstr2, new List<String>{
                            'ID','accountIDList','Name','accountNameList'
                        });

Src: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm#apex_System_String_format

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.