[SalesForce] System.QueryException: unexpected token: :

I have a class where I get 2 variables from a lightning component as a string which works fine. When I try to pass these variables from my testclass I get the error System.QueryException: unexpected token: :

I did a debug on the Line which throws this error which is:

List<SOBject> lstObj = Database.query(query); 

My debug for (query) gives me:

SELECT Type FROM Account:{Name=testM1, Type=Option1, RecordTypeId=01258000000MNyHAAW, Id=0019E00000YsyLrQAJ}

I expect to only have the string part:

SELECT Type FROM Account

How can I achieve this?

class

    public class picklistController {
        @AuraEnabled
        public static String getPicklistData(String objectName,String fieldName){          
            List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();
            Map<String,String> objectMap = new Map<String,String>();
            for(Schema.SObjectType f : gd)
            {
                 objectMap.put(f.getDescribe().getKeyPrefix(), f.getDescribe().getName());
            }
            String query = 'SELECT '+fieldName+' FROM '+objectName; 
            List<SOBject> lstObj = Database.query(query);        
            String selVal =  String.valueOf(lstObj[0].get(fieldName)) ; 
            Schema.SObjectField sobjField = Schema.getGlobalDescribe().get(objectName).getDescribe().Fields.getMap().get(fieldName) ;
            Schema.DescribeFieldResult fieldResult = sobjField.getDescribe() ; 
             List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

    ......
    }
}

Test

   @isTest 
    private class picklistControllerTest { 
        static testMethod void test0() { 

            Id recTypeId = [select Id,Name from RecordType where Name = 'Bedrijf/Gemeente/Overheid' limit 1].Id;
            Account acc = new Account(Name = 'testM1', Type ='Option1', RecordTypeId=recTypeId); 
            insert acc; 

            String objectName = String.valueOf(acc);
            String fieldName =  'Type';

            picklistController.getPicklistData(objectName , fieldName );

            System.assertEquals( 'string', picklistController.getPicklistData(objectName , fieldName ));  
       }
.....
    }

Best Answer

This is because your objectName is actually a stringified version of the entire object.

In your picklistControllerTest you should probably have String objectName = 'Account'

Related Topic