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
Nitish 73Nitish 73 

System.QueryException: unexpected token: ':'

Hi everyone, 

I am trying to build a dynamic SOQL Query and I am getting this error. If any of you can help, I'd be very thankful. 
Here is the code
 
SObjectType objToken = Schema.getGlobalDescribe().get('AC__c');
system.debug('<<<! objToken'+objtoken);
       DescribeSObjectResult objDef = objToken.getDescribe();
       Map<String, SObjectField> fields = objDef.fields.getMap(); 
system.debug('<<<!!!! fields'+fields);
       AC__c newline = new AC__c();

		newline.Is_it_for_Commercial_Use__c   = 'No';
		newLine.Movement_Type__c = 'Import';
		String var='';
	List<SObjectField> sfield = fields.values();

	for(sobjectfield c: sfield){
       
        var+=c+',';
    }
var = var.removeEnd(',');
String q = 'Select '+var+' FROM AC__c WHERE ID=\'' + 'a00r000000164eq' + '\'';

//ID=\'' + varID + '\''
system.debug('<<!!! AC Fields'+q);

List<sObject> sobjList = Database.query(q);
system.debug(' <<<!!! sobjList ACCCCCCCC'+sobjList);
system.debug('AC size '+sobjlist.size());




SObjectType objDM = Schema.getGlobalDescribe().get('Regulations_Association_Matrix__c');
       system.debug('<<< objDM'+objDM);
DescribeSObjectResult objDeff = objDM.getDescribe();
Map<String, SObjectField> DMfields = objDeff.fields.getMap(); 

system.debug('<<<<<<<<<DM Fields'+DMFields);
      // AC__c newline = new AC__c();
		//newline.Is_it_for_Commercial_Use__c   = 'No';
		String var2='';
	List<SObjectField> sfielddm = DMfields.values();

	for(sobjectfield c: sfielddm){
       
        var2+=c+',';
    }
var2 = var2.removeEnd(',');

system.debug('var2'+var2);
String[] varlist = var2.split(',');
system.debug('((((((((((((( varlist'+varlist);
String dmm = 'Select '+var2+' FROM Regulations_Association_Matrix__c';
system.debug('<<!!! DM Fields'+dmm);

List<sObject> sobjListDM = Database.query(dmm);
system.debug('<<<!!!! SobjListDMMMMMMMMMM'+SobjListDM);
system.debug('DM size '+sobjlistDM.size());

List<String> fingers = new List<String>();
List<SObjectField> lop = new List<SobjectField>();
String str='';
string first='';
Map<String, SObjectField> finalmap = new Map<String, SObjectField>();

    for (String d: varlist){
        if(fields.containsKey(d))
        { 	
            system.debug('@@@@@@@@d'+d);
            fingers.add(d);
            finalmap.put(d,fields.get(d));
         }
        
        for(sobjectfield s: finalmap.values()){
            lop.add(s);
        }
        
    }

	for(sobjectfield md: lop){
            str+=+'('+md+' = :'+newline+'.'+md+' OR '+md+' = '+'\'\'   '+');'+' AND ';
            
        } 
    
system.debug('<<<<<<<!!!!!!!!!!!! str '+str);
String kjax = 'Select '+var2+' FROM Regulations_Association_Matrix__c WHERE '+str;
system.debug('<<<! kjax'+kjax);

List<sObject> dmlist = Database.query(kjax);

Everything works fine. Even the Query string I formed in 'kjax' variable is also what I wanted. 

But when I execute, I am getting the error 

System.QueryException: unexpected token: ':' 

When I remove the ':'  , Iam getting this error

System.QueryException: expecting a colon, found 'AC__c' 


Can you guys guide me as to where am I going wrong on this. Would be really grateful


Thanks
Nitish
JethaJetha
At line number 78 in your code, please give a space after the ':', then It will work for sure.

Please mark as best answer if this resolves your issue.
Nitish 73Nitish 73
Hi Jetharam, 

Sorry but It didnt seem to work, still getting the same error
JethaJetha
have you got debug result of line number 84, if yes please paste it over here.

I need to check the dynamic query you formed.
venkat-Dvenkat-D
newline is an object. use string variable in filter criteria and then try it.