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
ArunaAruna 

Apex batch dynamic query problem need help

Hi there,

 

I am writing a batch apex to  update the account and there related contact owners.

 

my problem is , i have some "xxx"  uniq external id which is Text data type.

 

"xxx"  uniq external id will be copied and pasted into text area , I am reading those id's and spliting by new line charector (\n) an then i am storing those in set .

 

Now I am trying to get the all the accoounts which are having the id's in  set.

 

since it is  a string i am getting error.

 

please look at my below code

 

public

list<string> splitIJET2AccountId{

 

get{

 

if(IJET2AccountIds!=null){

splitIJET2AccountId=IJET2AccountIds.split(

'\r\n');

}

 returnsplitIJET2AccountId;

}

set;

}

publicset<string> IJET2AccountIdSet(){

 set<string> IJET2AccountIdSet=newset<string>();

 for(Integer i=0;i<splitIJET2AccountId.size();i++){

IJET2AccountIdSet.add(splitIJET2AccountId[i]);

}

returnIJET2AccountIdSet;

}

global

Database.Querylocator start(Database.BatchableContext BC){

  fquery=

'select id,Name,OwnerId,IJET2_ACCOUNT_ID__C,AccountManager__c,Secondary_VP__c,AccountManager_Secondary__c from Account'+' where IJET2_ACCOUNT_ID__C IN :\''+IJET2AccountIdSet()+'\'';

 

 

returnDatabase.getQueryLocator(query);

}

 

when I am running this query i am getting below error , please suggest me what is the other way to do this

 

query=select id,Name,OwnerId,IJET2_ACCOUNT_ID__C,AccountManager__c,Secondary_VP__c,AccountManager_Secondary__c from Account where IJET2_ACCOUNT_ID__C=:{1038987, 1043669, 1045940, 1050546, 5465456}
System.QueryException: unexpected token: '{'
17:13:48.043 (43631000)|SYSTEM_METHOD_EXIT|[47]|Database.getQueryLocator(String)
17:13:48.043 (43685000)|FATAL_ERROR|System.QueryException: unexpected token: '{'
Best Answer chosen by Admin (Salesforce Developers) 
ArunaAruna

solution to this problem

 

the mistake i did was i declared set inside the method.

 

declare the set out side the method and just give the set refernce in the query

 

public

list<string> splitIJET2AccountId{

 

get{

 

if(IJET2AccountIds!=null){

splitIJET2AccountId=IJET2AccountIds.split(

'\r\n');

}

 returnsplitIJET2AccountId;

}

set;

}

 set<string> IJET2AccountIdSet=newset<string>();

 

publicset<string> IJET2AccountIdSet(){

 for(Integer i=0;i<splitIJET2AccountId.size();i++){

IJET2AccountIdSet.add(splitIJET2AccountId[i]);

}

returnIJET2AccountIdSet;

}

global

Database.Querylocator start(Database.BatchableContext BC){

  fquery=

'select id,Name,OwnerId,IJET2_ACCOUNT_ID__C,AccountManager__c,Secondary_VP__c,AccountManager_Secondary__c from Account'+' where IJET2_ACCOUNT_ID__C IN :IJET2AccountIdSet';

 

 

returnDatabase.getQueryLocator(query);

}

All Answers

dieffreidieffrei

Dynamic values to dynamic soql I think doesnt work.
But you can do this:

Map<Id, Account> nmap = new Map<Id, Account> ([Select Id, Name from Account LIMIT 10]);
List<Id> nmaps = new List<Id>();
nmaps.addall(nmap.keyset());
String s = '\'';
s += String.join(nmaps,'\',\'');
s += '\'';
 
Database.query('select id,Name,OwnerId from Account where Id IN (' + s + ')');

 


I hope that it help you!

ArunaAruna

Thank you for the replay,

I cannot do below

 

1) but in my case i canot put limit on the account query

2)I am not looking Account Id, I am looking for  "IJET2_ACCOUNT_ID__C" this is external uniq Text Id (Exam : 12345).

 

I have comapare  external Id(it is not realy salesforce Id or any id it is Text filed Id ) with acount  IJET2_ACCOUNT_ID__C

 

example select id,Name,IJET2_ACCOUNT_ID__C from account where IJET2_ACCOUNT_ID__C IN : External Text Id set.

 

 

 

sivaextsivaext

Hi 

 

remove '()' this brackets in query and try like this

 

'select id,Name,OwnerId,IJET2_ACCOUNT_ID__C,AccountManager__c,Secondary_VP__c,AccountManager_Secondary__c from Account'+' where IJET2_ACCOUNT_ID__C IN :\''+IJET2AccountIdSet+'\'';

ArunaAruna

how can i remove that ,I am calling method

public

list<string> splitIJET2AccountId{

 

get{

 

if(IJET2AccountIds!=null){

splitIJET2AccountId=IJET2AccountIds.split(

'\r\n');

}

 returnsplitIJET2AccountId;

}

set;

}

publicset<string> IJET2AccountIdSet(){

 set<string> IJET2AccountIdSet=newset<string>();

 for(Integer i=0;i<splitIJET2AccountId.size();i++){

IJET2AccountIdSet.add(splitIJET2AccountId[i]);

}

returnIJET2AccountIdSet;

}

global

Database.Querylocator start(Database.BatchableContext BC){

  fquery=

'select id,Name,OwnerId,IJET2_ACCOUNT_ID__C,AccountManager__c,Secondary_VP__c,AccountManager_Secondary__c from Account'+' where IJET2_ACCOUNT_ID__C IN :\''+IJET2AccountIdSet()+'\'';

 

 

returnDatabase.getQueryLocator(query);

}

 

I was also doing other way 

 

set

<string> IJET2AccountIdSet=newset<string>();

 

publiclist<string> splitIJET2AccountId{

 

get{

 

if(IJET2AccountIds!=null){

splitIJET2AccountId=IJET2AccountIds.split(

'\r\n');

}

system.debug('*************splitIJET2AccountId='+splitIJET2AccountId);

 

returnsplitIJET2AccountId;

}

set;

}

publicset<string> IJET2AccountIdSet(){

 

system.debug('******* in Ijet2account id et method splitIJET2AccountId='+splitIJET2AccountId);

 

for(Integer i=0;i<splitIJET2AccountId.size();i++){

IJET2AccountIdSet.add(splitIJET2AccountId[i]);

}

system.debug('**************IJET2AccountIdSet='+IJET2AccountIdSet);

 

returnIJET2AccountIdSet;

}

globalDatabase.Querylocator start(Database.BatchableContext BC){

 

system.debug('*************start IJET2AccountIdSet()='+IJET2AccountIdSet());

 

query=

'select id,Name,OwnerId,IJET2_ACCOUNT_ID__C,AccountManager__c,Secondary_VP__c,AccountManager_Secondary__c from Account'+

 

' where IJET2_ACCOUNT_ID__C IN :\''+IJET2AccountIdSet+'\'';

 

system.debug('**********query='+query);

 

//}returnDatabase.getQueryLocator(query);

}

but no use i got below exception

 

09:09:14.215 (215061000)|USER_DEBUG|[43]|DEBUG|**********query=select id,Name,OwnerId,IJET2_ACCOUNT_ID__C,AccountManager__c,Secondary_VP__c,AccountManager_Secondary__c from Account where IJET2_ACCOUNT_ID__C IN :'{1019312, 1019354, 1019635, 1036687, 1038987, 1041710, 1043124, 1043669, 1045940, 1050546, ...}'

09:09:14.215 (215070000)|SYSTEM_METHOD_EXIT|[43]|System.debug(ANY)

09:09:14.215 (215091000)|SYSTEM_METHOD_ENTRY|[47]|Database.getQueryLocator(String)

09:09:14.215 (215468000)|EXCEPTION_THROWN|[47]|System.QueryException: unexpected token: '{1019312, 1019354, 1019635, 1036687, 1038987, 1041710, 1043124, 1043669, 1045940, 1050546, ...}'

09:09:14.215 (215532000)|SYSTEM_METHOD_EXIT|[47]|Database.getQueryLocator(String)

09:09:14.215 (215610000)|FATAL_ERROR|System.QueryException: unexpected token: '{1019312, 1019354, 1019635, 1036687, 1038987, 1041710, 1043124, 1043669, 1045940, 1050546, ...}'

 

Please can any one help me on this problem

ArunaAruna

solution to this problem

 

the mistake i did was i declared set inside the method.

 

declare the set out side the method and just give the set refernce in the query

 

public

list<string> splitIJET2AccountId{

 

get{

 

if(IJET2AccountIds!=null){

splitIJET2AccountId=IJET2AccountIds.split(

'\r\n');

}

 returnsplitIJET2AccountId;

}

set;

}

 set<string> IJET2AccountIdSet=newset<string>();

 

publicset<string> IJET2AccountIdSet(){

 for(Integer i=0;i<splitIJET2AccountId.size();i++){

IJET2AccountIdSet.add(splitIJET2AccountId[i]);

}

returnIJET2AccountIdSet;

}

global

Database.Querylocator start(Database.BatchableContext BC){

  fquery=

'select id,Name,OwnerId,IJET2_ACCOUNT_ID__C,AccountManager__c,Secondary_VP__c,AccountManager_Secondary__c from Account'+' where IJET2_ACCOUNT_ID__C IN :IJET2AccountIdSet';

 

 

returnDatabase.getQueryLocator(query);

}

This was selected as the best answer