Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
trigger runwaynew on Schedule__c (before insert, before update) {

 for ( Schedule__c sch :{
    if (sch.Runway__c != NULL)  {
        List<Schedule__c> scc = [Select Id From Schedule__c  WHERE  Runway__c  = :sch.Runway__c  and  Date__c = :sch.Date__c];

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


can you suggest me how to write test class on it...

share|improve this question

closed as unclear what you're asking by bigassforce, dphil, Sergey Utko, Bartley, Samuel De Rycke Apr 23 '14 at 19:58

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

Did you understand my answer ? – Samuel De Rycke Feb 19 '14 at 23:25

Never mind asking for a test class, lets start with your trigger itself.

for ( Schedule__c sch :{
    if (sch.Runway__c != NULL)  {
        List<Schedule__c> scc = [Select Id From Schedule__c  WHERE  Runway__c  = :sch.Runway__c  and  Date__c = :sch.Date__c];

Never Ever Ever put a database action (query or dml) inside an iteration. This would make so many separate calls to the database it would slow your code down tremendously, and require a lot more resources than required. This is why salesforce simply forces us to not do this with governor limits.

I am not gonna write the trigger for you, I want you to learn how to do that. So i'll explain how you can, or at least the way i would do it. Others may have different approaches.

  • you are comparing any new or updated Schedule with all excisting Schedules to not allow a duplicate.
  • as a workflow may still update your fields, i think this type of validation is better fitted for an after-update/insert trigger
  • You would start by iterating(looping) all your new records and collect their Runway and Date fields in 2 lists (lets say List<Runway__c> Runways and List<Date__c> SchedulesDates)
  • After your loop, you can query (once!) all Schedules where the Runway or Date is in either of the 2 lists.
  • You will want to limit your schedule_c query to schedules where the date_c is in the future. I'm gonna assume you never schedule something in the past anyway.
  • You now will want to cross match every of your new records with your results, for that you need to organise your queried possible (!) matches in a way they are easily searchable. I propose Map<Runway__c,Map<Date__c,Schedule__C>> SchedulesMappedByRunWayAndDate.
  • Now you loop over all your queried Schedules and add them to the map accordingly, if a 'subMap' for the specific Runway does not exist in SchedulesMappedByRunWayAndDate yet, you'll need to make it. For a given Runway, each date should be unique, I am expecting your data to be clean and this to be the case.
  • Now you can get to what you want to do, you can iterate over all your new inserted/updated Schedules, and look if a record already exist for their Runway (most likely=yes). If that is the case, you can look in the submap if a schedule already exists for the given Date. If this is the case, you can perform the addError(..); as in your code.
  • Additionally: sch.Runway__c != NULL You may want a validation rule to require a Runway to be selected, else that has to be in your code indeed.

(Additionally: Not to be rude, but I hope this is an exercise and you're not really making software for airports to organise their runway schedules and flights )

share|improve this answer
Excellent answer. Wish I could vote more than once. "Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime." – Chris Duncombe Feb 18 '14 at 12:07

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