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
sunny@99-chgsunny@99-chg 

To write Query as in string format

Hai ,

 

   I need some help related to write Query as in string format using IN operator.

 

global class autoCompleteController1
{
@RemoteAction
global static SObject[] findSObjects(string obj, string qry, string addFields,string profilename)
{
/* More than one field can be passed in the addFields parameter
Split it into an array for later use */
List<String> fieldList=new List<String>();
if (addFields != '')
fieldList = addFields.split(',');
// list<o2bc__Item__c> oItem=new list<o2bc__Item__c>();
set<id> setId=new set<id>(); // Here i taken the ID's into set,and writing 'IN' operator in Query
for(o2bc__Item__c oitem:[select id from o2bc__Item__c where o2bc__Type__c=:'FreePhones'])
{
setId.add(oitem.id);

}
system.debug('@@@@@@@'+setId);
/* Check whether the object passed is valid */
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
Schema.SObjectType sot = gd.get(obj);
if (sot == null)
{
return null;
}

/* Creating the filter text */
String filter = ' like \'' + String.escapeSingleQuotes(qry) + '%\'';


/* Begin building the dynamic soql query */
String soql = 'SELECT Name';

/* If any additional field was passed, adding it to the soql */
if (fieldList.size()>0)
{
for (String s : fieldList)
{
soql += ', ' + s;
}
}

/* Adding the object and filter by name to the soql */
soql += ' from ' + obj + ' where name' + filter;
string pl='new';

if(profilename!='')
{
//profile name and the System Administrator are allowed
soql += ' and Profile.Name like \'%' + String.escapeSingleQuotes(profilename) + '%\'';
system.debug('Profile:'+profilename+' and SOQL:'+soql);
}

soql += ' and o2bc__Item__c IN (\''+setid+'\')' ; // here i used IN operator,I think it is wrong,can any one tell me how to                        

                                                                                write IN operator in query of string format..I want to retrive all the values  w        

                                                                                   with the Id's in the set.now i am not retriving the values.

soql += 'and o2bc__Status__c=\''+Pl+'\' ';

/* Adding the filter for additional fields to the soql */
/* if (fieldList != null)
{
for (String s : fieldList)
{
soql += ' or ' + s + filter;
}
} */

soql += ' order by Name limit 20';

system.debug('Qry: '+soql);

List<sObject> L = new List<sObject>();
try
{
L = Database.query(soql);
}
catch (QueryException e)
{
system.debug('Query Exception:'+e.getMessage());
return null;
}
return L;
}

}

SFDC_EvolveSFDC_Evolve

I believe you are missing the ":"thing .  . Whenever use the variable in the where clause . We need to put the Colon there . . 

 

 

Please Mark it sovle if this Solve Your issue   :)

sunny@99-chgsunny@99-chg

Hai Thanx for ur reply,

That is not direct a query.

The query is in string format

Starz26Starz26

In dynamic SOQL you can use the bind syntax just fine.

 

Set<ID> theIDs = New Set<ID>();

 

//populate set

........

 

 

Striny qry = 'Select ID From Account where ID IN :theIDs';

 

Account[] a = database.query(qry);

 

 

will work just fine.