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
SteveOBSteveOB 

How to Create a TestMethod for My trigger?

Hello,

 

I have developed a trigger and tested successfully in the sandbox environment. Now I would like to deploy into production however, I ran into some issues providing the sufficient trigger code coverage needed in order to promote. I tried creating a public class where a static testmethod for the trigger is defined, but although the class saves without errors, my trigger is still unable to be promoted. Any help in writing the correct testmethod for my trigger would be greatly appreciated. Thanks in advance.

 

--Here is my trigger:

Trigger ClientHandOffUpdate on Client_Hand_Off__c(after insert, after update)
{
Client_Hand_Off__c[] CHOs = Trigger.new;
 
     for(Client_Hand_Off__c CHO:CHOs){

        Contract_Order_Line_Item__c[] updateCOLI = [SELECT Name FROM Contract_Order_Line_Item__c LIMIT 100];
            if (updateCOLI.size()>0)
            {
                for(Integer i = 0; i < updateCOLI.size(); i++)
                {
                    updateCOLI[i].Client_Hand_Off__c = null;
                    update updateCOLI;
                }
            } 
    }
 
    for(Client_Hand_Off__c CHO:CHOs){
 
        if (CHO.Contract_Order__c!= null){
 
        string ContactOrder = CHO.Contract_Order__c;

        Contract_Order_Line_Item__c[] updateCOLI = [SELECT Name FROM Contract_Order_Line_Item__c WHERE Contract_Order__r.Id = :ContactOrder LIMIT 100];
            if (updateCOLI.size()>0)
            {
                for(Integer i = 0; i < updateCOLI.size(); i++)
                {
                    updateCOLI[i].Client_Hand_Off__c = CHO.Id;
                    update updateCOLI;
                }
            }
        }   
    }
}

 

 

--Here is my Attempted Testmethod:

 

public with sharing class CHOtriggerUnitTest {
  
  public static testmethod void testClientHandOffTrigger()
    {
    
          Account acct = new Account(name='test account', Type='Prospect');
          insert acct;
          
          Contract_Order__c CO = new Contract_Order__c(Account__c=acct.Id,State_Fips__c = '01 AL');
          insert CO;
           
          Client_Hand_Off__c[] CHOstoCreate = new Client_Hand_Off__c[]{};
          for(Integer x=0; x<200;x++){
              Client_Hand_Off__c CHOS = new Client_Hand_Off__c(Client_Name__c=acct.Id,Contract_Order__c=CO.Id,Client_Personality__C ='Test Client Hand Off Form');
              insert CHOstoCreate;
          }
           
   }   

}

 

Coco_SydneyCoco_Sydney

@isTest
private class Test_ContactCallStatistic {

    static testMethod void myUnitTest() {

 

    }

}

super developersuper developer

private class Test_ContactCallStatistic {

    static testMethod void myUnitTest() {

  Contract_Order__c CO = new Contract_Order__c(Account__c=acct.Id,State_Fips__c = '01 AL');
          insert CO;

 Clearly explain what is your trigger is doing

 

    }

}

SteveOBSteveOB

Hi,

 

Thanks for your feedback. I have tried making the class private according to your example but Eclips will not allow me to update/deploy if the top level type is not of a public or global visibility. Let me know if I am missing something in your feedback. Also, I thought I would explain exactly what it is my trigger is doing.

 

My Trigger is intended to perform the following action:

 

Trigger fires on Object A(after insert, after update) to update records on Object C. Object C is related to Object B. Object A has a related lookup of Object B. Object C has a related lookup of Object A.

 

Upon insert/update of Object A, it checks the related lookup field of the Object B record and finds all associated Object C records. Then it updates those Object C records related lookup field for Object A with the Id of the Object A record it is firing from. I also made it such that it should fist check for any old associations and remove them before making new associations. This helps to resolve issues if the lookup of Object B on the Object A record is changed and needs to make  the correct Object C record associations.

 

Please let me know if this helps. Thanks again.

super developersuper developer

Hi Still i'm in confused state ..

 

Object A is parent to object B . and Object B is parent to Object C.  You said Again Object C is having lookup of Object A.

 

Do You want Update the corresponding childs of object B (or) direct childs of object A.

 

1.If you want Update corresponding childs of B Which is having Parent as Object A.

Test coverage is

 

static testMethod void myUnitTest() {

 

  Contract_Order__c CO = new Contract_Order__c(Account__c=acct.Id,State_Fips__c = '01 AL');
          insert CO;(I think This Object A).

ObjectB B=new objectB(Name='',Lookup of A=co.id);

insert b;

ObjectC C1=new objectC(name='1',Look Up B=b.id);

insert c1;

ObjectC C2=new objectC(name='2',Look Up B=b.id);

insert c2;

update CO;

 

 

    }

 

 

 

2. You want Update directly Object c Records.

 

static testMethod void myUnitTest() {

 

  Contract_Order__c CO = new Contract_Order__c(Account__c=acct.Id,State_Fips__c = '01 AL');
          insert CO;(I think This Object A).

ObjectC C1=new objectC(name='1',Look Up A=co.id);

insert c1;

ObjectC C2=new objectC(name='2',Look Up A=co.id);

insert c2;

update CO;

 

 

    }

 

SteveOBSteveOB

Hi, Thanks again. I actually did some more investigating and research and was able to modify my testmethod as follows. But first to answer your question, Object A = Client_Hand_Off__C, Object B = Contract_Order__c and Object C = Contract_Order_Line_Item__c. Now my new issue is that when i test the trigger in my production environment, it produces the follwoing error: "System.LimitException: Too many DML rows..."

 

Here is my testmethod which produced 50% coverage and allowed me to deploy my trigger:

@isTest
private class CHOtriggerUnitTest {
  
  static testMethod void myUnitTest()
    {    
      Account acct = new Account(name='test account', Type='Prospect');
          insert acct;
          
        Contract_Order__c CO1 = new Contract_Order__c(Account__c=acct.Id,State_Fips__c = '01 AL');
          insert CO1;
          
        Contract_Order__c CO2 = new Contract_Order__c(Account__c=acct.Id,State_Fips__c = '01 AL');
          insert CO2;
          
        Client_Hand_Off__c[] chosList = [select Name from Client_Hand_Off__c LIMIT 1];
          for (Client_Hand_Off__c c : chosList) {c.Contract_Order__c =CO2.Id;
          }
        Client_Hand_Off__c newCHO = new Client_Hand_Off__c(Client_Name__c=acct.Id,Contract_Order__c=CO2.Id,Client_Personality__C ='Test Client Hand Off Form');
        chosList.add(newCHO);
        try {upsert chosList;
        } catch (DmlException e) {
          // Process exception here
          }
          
        Contract_Order_Line_Item__c COLI1 = new Contract_Order_Line_Item__c (Account__c=acct.Id, Contract_Order__c=CO1.Id);
          insert COLI1;
          
        Contract_Order_Line_Item__c COLI2 = new Contract_Order_Line_Item__c (Account__c=acct.Id, Contract_Order__c=CO1.Id);
          insert COLI2;
          
        Contract_Order_Line_Item__c COLI3 = new Contract_Order_Line_Item__c (Account__c=acct.Id, Contract_Order__c=CO2.Id);
          insert COLI3;
                            
   }
}

 

Here is the trigger Once more which i deployed successfully (Produces System.LimitException: Too many DML rows when i try to update a record):

trigger SB_updateAssociatedCOLIwhenClientHandoffFormIsCreatedorUpdated on Client_Hand_Off__c (after insert, after update) {

Client_Hand_Off__c[] CHOs = Trigger.new;
 
     for(Client_Hand_Off__c CHO:CHOs){

        Contract_Order_Line_Item__c[] updateCOLI = [SELECT Name FROM Contract_Order_Line_Item__c LIMIT 100];
            if (updateCOLI.size()>0)
            {
                for(Integer i = 0; i < updateCOLI.size(); i++)
                {
                    updateCOLI[i].Client_Hand_Off__c = null;
                    update updateCOLI;
                }
            }
    }
 
    for(Client_Hand_Off__c CHO:CHOs){
 
        if (CHO.Contract_Order__c!= null){
 
        string ContactOrder = CHO.Contract_Order__c;

        Contract_Order_Line_Item__c[] updateCOLI = [SELECT Name FROM Contract_Order_Line_Item__c WHERE Contract_Order__r.Id = :ContactOrder LIMIT 100];
            if (updateCOLI.size()>0)
            {
                for(Integer i = 0; i < updateCOLI.size(); i++)
                {
                    updateCOLI[i].Client_Hand_Off__c = CHO.Id;
                    update updateCOLI;
                }
            }
        }   
    }
}