+ Start a Discussion
Sujendran Sundarraj 8Sujendran Sundarraj 8 

dymanically generate SOQL query using list<string>

User-added imageI have a requirement to select an object from the list and then select the required fields. Once I click query it has to generate an SOQL query for that select fields. I did it but it is coming with brackets like in the above image. Please help me to fix the issue. 

My Method is: 
  public void query(){
    map<integer,string> strl = new map<integer,string>();
    for(integer i=0 ; i<getfields.size(); i++){
    strl.put(i,getfields[i]);
    }
    system.debug('strl===>' +strl);
    inputrec = 'select   '+strl.values()+' from ' +strObjectName;
    }


Thank you in advance. 
Regards, 
Sujendran. 
 
Best Answer chosen by Sujendran Sundarraj 8
Shaik Naga janiShaik Naga jani
Hi Sujendran,
Check below code once
public void query(){
    map<integer,string> strl = new map<integer,string>();
    for(integer i=0 ; i<getfields.size(); i++){
        strl.put(i,getfields[i]);
    }
    system.debug('strl===>' +strl);
    inputrec = 'SELECT '+String.join(strl.values(), ', ')+' FROM ' +strObjectName;
}
Kindly mark this as solved if the reply was helpful.
Thanks
Shaik

All Answers

Shaik Naga janiShaik Naga jani
Hi Sujendran,
Check below code once
public void query(){
    map<integer,string> strl = new map<integer,string>();
    for(integer i=0 ; i<getfields.size(); i++){
        strl.put(i,getfields[i]);
    }
    system.debug('strl===>' +strl);
    inputrec = 'SELECT '+String.join(strl.values(), ', ')+' FROM ' +strObjectName;
}
Kindly mark this as solved if the reply was helpful.
Thanks
Shaik
This was selected as the best answer
Sujendran Sundarraj 8Sujendran Sundarraj 8
It worked, Thank you Shaiks :)