trigger runwaynew on Schedule__c (before insert, before update) {

    Map<string,List<Schedule__c>> ScheduleMap = new Map<string,List<schedule__c>>();
    set<Date> DateSet = new set<date>();
    set<id> RunwaySet = new set<id>();

    for ( Schedule__c sch : trigger.new) {

    for(Schedule__c sch : [
            Select Id,date__c,Runway__c
            From Schedule__c
            WHERE  Runway__c IN:runwaySet and Date__c IN:DateSet
            ]) {
        // Combo the date + Runway
        string key = sch.runway__c+Sch.Date__c;
            ScheduleMap.put(key,new List<schedule__c>());


    for ( Schedule__c sch : trigger.new) {
        if (sch.Runway__c != NULL && Sch.Date__c!=null) {
            string key = sch.runway__c+Sch.Date__c;

            if (ScheduleMap.get(key).size() > 0) {
                sch.addError('Flight Already Scheduled on the Runway');

the above trigger is used for validation error. now, i having runway field contains track 1 and track 2 (picklist field) and date field contains datetime. now i will trigger for new record... if it contains same value as per existing saved record it will give error.

now, the main issue is i want to make dynamic query which will retrieve all records and the thing with old query is its retrieving all unwanted records. pls suggest to make the query dynamic and correct way...

  • Dynamic SOQL query is just string used as argument in method. But to help you I need more data - please provide me with what you need/what you want achieve? Feb 21, 2014 at 15:17
  • You could do what you are doing in the trigger by creating a field designated unique and then using a workflow rule to concatenate the runway and schedule date and do a field update to the unique field. You will be blocked by the system from creating it. Feb 21, 2014 at 16:04
  • here runway and date both are fields on schedule object.
    – sumit
    Feb 21, 2014 at 17:20
  • here runway and date both are fields on schedule object. i used the above trigger to show the validation error. runway is the picklist field having track 1 and track2 value. and date is datetime. i have created diff diff records like...track1 and any date..track2 and any date... so,if i enter the same data like..same track1 and same datetime..trigger will fire... i used soql query and i want to make it dynamic...pls tell me how i will approach this?
    – sumit
    Feb 21, 2014 at 17:27
  • string query = 'Select Id,date__c,Runway__c From Schedule__c WHERE Runway__c IN :runwaySet and Date__c IN :DateSet'; List <schedule__c> = database.query(query); please check this dynamic query which i created...tell me what i am missing...?
    – sumit
    Feb 21, 2014 at 17:29

2 Answers 2


Here's a Salesforce doc on Dymanic SOQL:


Here's the idea: First, write you're SOQL query as a string. Start with something simple.

string queryString = 'SELECT id,Name,Account.Name FROM Contact LIMIT 3';

To get the result, use the database.query method. Note that the method returns a list<sObject>.

list<sObject> sContactList = database.query(queryString);

// or cast to what you want to return 

list<Contact> contactList = (list<Contact>)database.query(queryString);

Now, let's do a more complicated SOQL query:

 map<id,Contact> contactMap = new map<id,Contact>(contactList);

 // using standard soql
 list<Contact> theSameContactList = [SELECT id,Name,Account.Name
                                     FROM Contact
                                     WHERE id IN :contactMap.keySet()];

 // but this doesn't work so straitforwardlike in dynamic SOQL
 string queryThatDoesntWork = 'SELECT id,Name,Account.Name FROM Contact WHERE id IN:contactMap.keySet()';

 // queryThatDoesntWork doesn't work because you are reference a method of an object.
 // In dynamic SOQL, you can only reference an object -- but not any of its methods

 set<id> contactKeySet = contactMap.keySet();
 string queryStringThatDOESWork = 'SELECT id,Name,Account.Name FROM Contact WHERE id IN:contactKeySet';

 list<Contact> theSameContactList = (list<Contact>)database.query(queryStringThatDOESWork);

I Used the approach (SOQL Builder) it worked for me its an awesome. Its an Easy to use Library functions to build Query Dynamically. the following utility classes can be used through out all implementaion

Mark this as an answer if it helps you.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .