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
Wes Reed 27Wes Reed 27 

Need help writing tests for Apex Triggers

I have these two apex triggers that I need help writing test for. As a newbie to salesforce devlopment, it took some time to finally get these triggers working. Thanks for your help. - Wes
 
trigger IndvidualEngagementScore on EngagementContact__c(after insert, after update, after delete) {

    Contact objContact;
    String getAccountId;
    Decimal sumPoints = 0;
    Decimal countRecords = 0;

    //Set initial values based on current engagement record's contact and trigger action:
    if (Trigger.isDelete) {
        For(EngagementContact__c delEngagement: trigger.old) {
            objContact = [SELECT id, AccountID FROM Contact WHERE id =: delEngagement.Contact__c];
            getAccountId = objContact.AccountId;
            if (delEngagement.ParticipationPoints__c == NULL) {
                sumPoints = 0;
                countRecords = 0;
            }
            Else {
                sumPoints = sumPoints - delEngagement.ParticipationPoints__c;
                countRecords = countRecords - 1;
            }
        }

    }
    Else {
        For(EngagementContact__c objEngagement: trigger.new) {
            objContact = [SELECT id, AccountID FROM Contact WHERE id =: objEngagement.Contact__c];
            getAccountId = objContact.AccountId;
            sumPoints = sumPoints + objEngagement.ParticipationPoints__c;
            countRecords = countRecords + 1;
            if (Trigger.isUpdate) {
                For(EngagementContact__c oldEngagement: trigger.old) {

                    if (oldEngagement.ParticipationPoints__c == NULL) {
                        sumPoints = 0;
                        countRecords = 0;
                    }
                    Else {
                        sumPoints = sumPoints - oldEngagement.ParticipationPoints__c;
                        countRecords = countRecords - 1;
                    }


                }
            }
        }
    }

    //Get other contacts related to same account and sum their engagement points:   
    Account objAccount = [SELECT id FROM Account WHERE id =: getAccountId];
    List < Contact > contactList = [SELECT id, EngagementPoints__c, EngagementRecords__c FROM Contact WHERE AccountId =: objAccount.Id];
    For(Contact i: contactList) {
        sumPoints = sumPoints + i.EngagementPoints__c;
        countRecords = countRecords + i.EngagementRecords__c;
    }

    //Update the account's engagement score and commit change:
    objAccount.IndividualEngagementScore__c = sumPoints;
    objAccount.IndividualEngagementRecords__c = countRecords;
    Update objAccount;

}

trigger IndividualEngagmentOrgRecalc on Contact(After update, After delete) {


    String getOldAccountId;
    String getNewAccountID;
    Decimal sumPoints = 0;
    Decimal countRecords = 0;

    For(Contact objOldContact: trigger.old) {
        //Get other contacts related to same OLD account and sum their engagement points:   
        Account objOldAccount = [SELECT id FROM Account WHERE id =: objOldContact.AccountID];
        List < Contact > contactOldList = [SELECT id, EngagementPoints__c, EngagementRecords__c FROM Contact WHERE AccountId =: objOldAccount.Id];
        For(Contact i: contactOldList) {
            sumPoints = sumPoints + i.EngagementPoints__c;
            countRecords = countRecords + i.EngagementRecords__c;
        }
        //Update the OLD account's engagement score and commit change:
        objOldAccount.IndividualEngagementScore__c = sumPoints;
        objOldAccount.IndividualEngagementRecords__c = countRecords;
        Update objOldAccount;
    }


    //Reset initial values
    sumPoints = 0;
    countRecords = 0;


    if (Trigger.isDelete) {}
    ELSE {
        For(Contact objNewContact: trigger.New) {
            //Get other contacts related to same OLD account and sum their engagement points:   
            Account objNewAccount = [SELECT id FROM Account WHERE id =: objNewContact.AccountId];
            List < Contact > contactNewList = [SELECT id, EngagementPoints__c, EngagementRecords__c FROM Contact WHERE AccountId =: objNewAccount.Id];
            For(Contact i: contactNewList) {
                sumPoints = sumPoints + i.EngagementPoints__c;
                countRecords = countRecords + i.EngagementRecords__c;
            }

            //Update the OLD account's engagement score and commit change:
            objNewAccount.IndividualEngagementScore__c = sumPoints;
            objNewAccount.IndividualEngagementRecords__c = countRecords;
            Update objNewAccount;
        }
    }
}


 
LBSLBS
You shall try to understand the basic concepts of writing a Test Class. It is straightforward and fun when you get in to the game. Please go through following blog posts for basic concepts. At the end of day it was you who knows the logic of your code!
https://lahirustechtalk.wordpress.com/2016/04/17/apex-test-classes-best-practices/

Make it a best answer if your issue is resolved
Cheers,
Mudi