function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ricardo OrtegaRicardo Ortega 

Help with a trigger test

I have a junction object with to MD-relationships, one for contact and one for a custom object named program. The trigger works fine in the sandbox and i have to test it to use it in production. I tried writing the test code but it doesn't  work. Can someone please help me, what code do I need to write for the testing of this trigger and where do I have to place it?

 

trigger insert_case on Affiliated_Programs__c (before insert, before update) {
   for (Affiliated_Programs__c ci : trigger.new) {

   //Checks the database for all affiliated programs with the same program and contact
      List<Affiliated_Programs__c> resultList = [SELECT id, Name FROM Affiliated_Programs__c
      WHERE Contact__c = :ci.Contact__c
      AND Program__c = :ci.Program__c];

   // Raise the error back if any records found
       if (!resultList.isEmpty()) {
          System.debug('Found another copy ' + resultList[0].id + ' name is ' + resultList[0].Name);
          ci.addError('Duplicate record, a Contact is already related to that program');
       }
   }
}

JHayes SDJHayes SD

Are the relationships (Contact__c and Program__c on Affiliated_Programs__c) master-detail or lookup?

Ricardo OrtegaRicardo Ortega

Master-detail