1

The aim of this code is to retrieve the ticket information of the two record ids in the List recordids = new List(); How can i write this code for best for practice.

public Static List<String> getrecordidlist(String textsearch)
{
    String newtextsearch = '%'+textsearch+'%';
    list<String> listid = new List<String>();
    for(Contact con : [Select Id from Contact where Name like :newtextsearch])
    {
        listid.add(String.valueOf(con.Id));    
    }
    return listid;
}
 Public Static list<Ticket__c> getTicketRecord(String textsearch)
{
    List<String> listid = getrecordidlist(textsearch);
    List<Ticket__c> ticketinfo ;
    for(integer i = 0; i<=listid.size()-1; i++)
    {
       ticketinfo = [Select Ticket_ID__c,Booker__c,Ticket_holder__c from Ticket__c where Booker__c=:listid[i]]; 
    }
    return ticketinfo;
}
  • 1
    quick suggestion. NEVER hardcode Id's and NEVER use SOQL/SOSL queries in loops. Hardcoding Id's will break the deployment of code and using queries in loops will hit the SF limits. – sam_s Aug 18 '15 at 4:05
  • Tnx for the suggestion sam. Now i edit my code so that you can see and suggest how to impelement my codes for retrieving data. – Danryl Tigol Carpio Aug 18 '15 at 4:18
1

Please try this below code as per your above code design:

public Static List<String> getrecordidlist(String textsearch)
{
String newtextsearch = '%'+textsearch+'%';
list<String> listid = new List<String>();
   for(Contact con : [Select Id from Contact where Name like :newtextsearch])
  {
    listid.add(String.valueOf(con.Id));    
   }
    return listid;
}
 public static list<Ticket__c> getTicketRecord(String textsearch)
{
    return [Select Ticket_ID__c,Booker__c,Ticket_holder__c from 
            Ticket__c where Booker__c IN :getrecordidlist(textsearch)]; 
}

SOQL statements can't exceed 20,000 characters. For SOQL statements that exceed the maximum length of characters, then the API returns a MALFORMED_QUERY exception code ; no result rows are returned.

Let me know in case of any other help!

2

Another approach is using Sets

public Static set<Id> getrecordIdSet(String textsearch)
{
  String newtextsearch = '%'+textsearch+'%';
  set<Id> contactIds = new set<Id>();
  for(Contact con : [Select Id from Contact where Name like :newtextsearch])
  {
    contactIds.add(con.Id);    
  }
  return contactIds;
}
Public Static list<Ticket__c> getTicketRecord(String textsearch)
{
 set<Id> contactIdSet = getrecordIdSet(textsearch);
 List<Ticket__c> ticketinfo = [Select Ticket_ID__c,Booker__c,Ticket_holder__c from Ticket__c where Booker__c IN : contactIdSet]; 
 return ticketinfo;
}
1

You can use IN clause. Example:

List<String> listId = new List<String>();
listId.add('003xcc00000322');
listId.add('003xcc00000988');


ticketinfo = [Select id from Ticket__c where id IN:listId];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.