0

I am receiving the following error when running a test on my code. However, it seems to run fine in the debug line above it, so I'm confused regarding the source of the error:

System.QueryException: unexpected token: '<EOF>'

   public static void careStatusChangeRelated(String careStatusChangeRelatedSerialized) {
        // deserialize back into map
    Map<String, Set<Id>> oppEntry= (Map<String, Set<Id>>)JSON.deserialize(careStatusChangeRelatedSerialized, Map<String, Set<Id>>.class);
    List <CareProfileStatusChangeExit__mdt> allStatusCriteria = customMetadata;

    //Set up a Query to pull the needed fields from Opportunity group 
    String query = 'SELECT ';
    Set<String> queryfields = new Set<String>(); 
    for(CareProfileStatusChangeExit__mdt field: allStatusCriteria){
        //Add queryfields to update parent records
        if(field.RelatedObject__c != null  && field.RelatedObjectLookupFieldAPIName__c == null && field.APIFieldName__c != null){
            queryfields.add(field.APIFieldName__c + ','); }  
    }
    for(String statuskey: oppEntry.keySet()){
        if(childRecordStatus.contains(statuskey)){
            oppChanges.put(statuskey, oppEntry.get(statuskey));
        }                
    }

    If (queryfields.size()>0){
        for(String q: queryfields){
            query = query + q;}


        List<Id> Vals = new List<Id>();
        For(Set<Id>ids: oppEntry.values()){
        for(Id is : ids){
            Vals.add(is);
        } }
        //query = query.left(query.length() - 1);  
        query = query + ' Id FROM Opportunity WHERE Id IN: Vals';
        System.debug('QUERY Vals' + Vals);
        System.debug('QUERY:' + query);
         System.debug('QUERY:' + (List<Opportunity>) Database.query(query));}
  //Iterate over each Status to update related records based on metadata criteria 
 List<Opportunity> queryResults =(List<Opportunity>) Database.query(query);
1
  • Please share the actual end value of your query string.
    – Adrian Larson
    Feb 16, 2018 at 16:57

1 Answer 1

1

When you use dynamic SOQL, you can't have an intervening space between the : and the variable after it. Change your code as follows:

    query = query + ' Id FROM Opportunity WHERE Id IN :Vals';
2
  • I tried: query = query + ' Id FROM Opportunity WHERE Id IN:Vals'; and query = query + ' Id FROM Opportunity WHERE Id IN :Vals'; and see the same error on both. Feb 16, 2018 at 16:50
  • I also needed to add a null check on Vals Feb 16, 2018 at 17:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .