Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.
trigger runwaynew on Schedule__c (before insert, before update) {

 for ( Schedule__c sch : trigger.new){
    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
    
Did you understand my answer ? –  Samuel De Rycke Feb 19 at 23:25
add comment

1 Answer

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

for ( Schedule__c sch : trigger.new){
    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
1  
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." –  sfdc_ninja Feb 18 at 12:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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