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 

Develop an Apex Trigger

Hello, I recently created my first apex trigger and Im not really sure what the procedure is for the testing and developing of said trigger and quite frankly i didnt understand much about the apex testing page.

 

My apex trigger checks that you cant create a junction object that already relates a contact to a specified program more than once. 

 

trigger insert_case on Affiliated_Programs__c (before upsert) {
       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');
              }
       }
}

 

Thank you very much

Sean TanSean Tan

Is there a reason why you can't just use a standard Salesforce "Unique" field and just populate it with the contact and program before inserts of the records? Granted the error message isn't as pretty as what you would get with the trigger but it prevents you from managing it yourself.

 

However, if you really want to handle it via the trigger you'll need to fix it to be bulk safe. Others may have a different approach then myself, but I would personally use dynamic soql to filter the query specifically to what I need like the following:

 

trigger insert_case on Affiliated_Programs__c (before insert) 
{
    String dynamicQuery = '';
    //map containing the programs relative to the Contact + Program
    Map<String, Affiliated_Programs__c> programsMap = new Map<String, Affiliated_Programs__c>{};
    
    for (Affiliated_Programs__c ci : trigger.new)
    {        
        if (ci.Contact__c != null && ci.Program__c != null)
        {
            if (dynamicQuery != '')
            {
                dynamicQuery += ' OR ';
            }
            dynamicQuery += '(Contact__c = \'' + ci.Contact__c + '\' AND Program__c \'' + ci.Program__c + '\')';
            programsMap.put('' + ci.Contact__c + ci.Program__c, ci);
        }
    }
    
    if (dynamicQuery != '')
    {
        for (Affiliated_Program__c item : Database.query('SELECT Id, Name, Contact__c, Program__c Affiliated_Programs__c WHERE ' + dynamicQuery))
        {
            Affiliated_Programs__c newProgram = programsMap.get('' + ci.Contact__c + ci.Program__c);
            System.debug('Found another copy ' + item.id + ' name is ' + item.Name);
            newProgram.addError('Duplicate record, a Contact is already related to that program');
        }
    }    
}