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
Chad MoutesChad Moutes 

Apex Test Class For My Tigger Not Working

I have an Apex Trigger listed below:
 
trigger ActiveAssignedBDMsTrigger on Assigned_BDM__c (after insert, after update, after delete) { 
    set<Id> set_id = new set<Id>();
    
    List<Project__c>prj_list = new List<Project__c>();
    
    if(trigger.isInsert || trigger.isUpdate) {
        for(Assigned_BDM__c bdm: trigger.new) {
            set_id.add(bdm.CampaignX__c);
        }
    }
    else if(trigger.isDelete) {
        for(Assigned_BDM__c bdm: trigger.old) {
            set_id.add(bdm.CampaignX__c);
        }
    }
    
    if(trigger.isAfter && (trigger.isUpdate || trigger.isInsert || trigger.isDelete)) {
        prj_list = [SELECT id, Number_Of_Active_BDMs__c, (SELECT id,name FROM Assigned_BDMs1__r WHERE Active__c = TRUE) FROM Project__c WHERE id IN :set_id];
        
        for(Project__c prj: prj_list) {
            if(prj.Assigned_BDMs1__r.size() > 0) 
                prj.Number_Of_Active_BDMs__c = prj.Assigned_BDMs1__r.size();
            else
                prj.Number_Of_Active_BDMs__c = 0;
        }
        if(!prj_list.isEmpty())
            update prj_list;
    }
}

And I am trying to write a test class for it to get full code coverage, because my production org is currently at 70% so i need to bring the average up.

Can anyone give me any ideas?
Best Answer chosen by Chad Moutes
Bhanu MaheshBhanu Mahesh
Hi Chad,

Try this test class

@isTest
                                    
public class ActiveAssignedBDMsTriggerTest{

    static testmethod void testInsert(){
        Project__c proj = new Project__c(Name = 'Test'); //Add all the required fields
        insert proj;
        Assigned_BDM__c assgnBDM = new Assigned_BDM__c(Name = 'test', CampaignX__c = proj.Id, Active__c = true);//Add all the required fields
        insert assgnBDM;
        Project__c proj1 = [SELECT Id, Number_Of_Active_BDMs__c FROM Project__c WHERE Id = :proj.Id];
        System.assertEquals(proj1.Number_Of_Active_BDMs__c, 1);
        delete assgnBDM;
        Project__c proj1 = [SELECT Id, Number_Of_Active_BDMs__c FROM Project__c WHERE Id = :proj.Id];
        System.assertEquals(proj1.Number_Of_Active_BDMs__c, 0);
    }
}

Regards,
Bhanu Mahesh

All Answers

Bhanu MaheshBhanu Mahesh
Hi Chad,

Try this test class

@isTest
                                    
public class ActiveAssignedBDMsTriggerTest{

    static testmethod void testInsert(){
        Project__c proj = new Project__c(Name = 'Test'); //Add all the required fields
        insert proj;
        Assigned_BDM__c assgnBDM = new Assigned_BDM__c(Name = 'test', CampaignX__c = proj.Id, Active__c = true);//Add all the required fields
        insert assgnBDM;
        Project__c proj1 = [SELECT Id, Number_Of_Active_BDMs__c FROM Project__c WHERE Id = :proj.Id];
        System.assertEquals(proj1.Number_Of_Active_BDMs__c, 1);
        delete assgnBDM;
        Project__c proj1 = [SELECT Id, Number_Of_Active_BDMs__c FROM Project__c WHERE Id = :proj.Id];
        System.assertEquals(proj1.Number_Of_Active_BDMs__c, 0);
    }
}

Regards,
Bhanu Mahesh
This was selected as the best answer
Chad MoutesChad Moutes
Im getting an error, Field is not writeable: Assigned_BDM__c.Name at line 12 column 67. Which is weird because that is an object not a field.

Any Ideas?

Oh and one more thing, in my production org there are about 4 ID fields that are required to create both a project and an Assigned BDM, they both lookup to multiple other Objects. How am I to handle that?
Bhanu MaheshBhanu Mahesh
HI Chad,

Might be Name field on Assigned_BDM__c is an Auto Number. So we cannot populate it. So remove Name from 
Assigned_BDM__c assgnBDM = new Assigned_BDM__c( CampaignX__c = proj.Id, Active__c = true);//Add all the required fields

To populate the required lookup fields, first you ned to insert those records and populate those Ids in the respective lookup fields.

Otherwise you can use @isTest(seeAllData = true)
Query all the required data in your test class and try to insert Assigned_BDM__c. But if the records are not alredy present in the org, it will fail.
It is better to insert all the records in the test class. So that it won't fail in any of the sandboxes or production as data is available in test class.

Regards,
Bhanu Mahesh

 
Chad MoutesChad Moutes
Thanks for your help, I got it to work!