Criteria for 1 record associated with 3 forms

Criteria for 1 record associated with 3 forms

Hello! I have to manage event information, and I have been struggling for days on how to develop a script that pays contractors for each event in a certain way. I have 3 forms

ADD (contains info about event. Ex: date, type of event, contractors, etc)
Contractors (adds a new contractor)
Pay_Contractors (a form that pays the contractors for each event)

I created a custom action button for the record summary of ADD. I would like to openURL for each contractor that is assigned to the date of ADD, AND has not been paid for the event ADD.

Below are the lookup fields associated with each form.

ADD: multiselect lookup to Contractors (this let's me add as many contractors as I want)
Contractors: multiselect lookup to ADD; multiselect lookup to Pay_Contractors
Pay_Contractors: multiselect lookup to ADD; multiselect lookup to Contractors

So far this is what I have

  1. void Custom_Action(ADD ad)
  2. {
  3. for each contractor_record in Contractors [Event_History = ad.ID]
  4. {
  5. paid = Pay_Contractors [Date_of_Event = ad.ID];
  6.     if (Contractors[(Payment_History = paid.ID)].count()  =  0)
  7.     {
  8.         openUrl("#Form:Pay_Contractors?Contractor=" + contractor_record.ID + "&Date_of_Event=" + ad.ID, "popup window");
  9.     }
  10. }
  11. }

But it's not working. How do I openURL for each Contractor that is assigned to the ADD record and that also does NOT contain a Pay_Contractor record associated with the same ADD record?