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
tsritsri 

Test Class code coverage for after update trigger

Hi,
I have trigger, which inserts a new record in a custom Obj  caled "Audits" and in the "Audits related list" on Product detail page whenever some fields are changed on Products as shown below:

trigger Producthistory on Product2  (After update)
{
    If(CheckRecursive.runOnce())
    {
        Map<Id,User> userMap = new Map<Id,User>([Select Id,Name from User]);
        List<Audits__c> AuditInsert = new List<Audits__c>();
        for(Product2 prod : trigger.new)
        {
        if(prod.Base_List_Price__c!= null && prod.Base_List_Price__c != trigger.oldMap.get(prod.id).Base_List_Price__c)
        {

       
        Audits__c audit1 = new Audits__c(Name = Prod.Name,Field_changed__C = 'Base List Unit Price', New_value__C = ''+prod.Base_List_Price__c, Old_value__C= ''+trigger.oldmap.get(prod.id).Base_List_Price__c,
                                                          who_made__C = userMap.get(prod.LastModifiedById).Name, what_time__c=''+prod.LastModifiedDate,Product__c=prod.Id);
       
        AuditInsert.add(audit1);

        }
        if(prod.Maintenance_Rate__c!= null && prod.Maintenance_Rate__c != trigger.oldMap.get(prod.id).Maintenance_Rate__c)
        {
       
        Audits__c audit1 = new Audits__c(Name = Prod.Name,Field_changed__C = 'Maintenance Rate', New_value__C = ''+prod.Maintenance_Rate__c, Old_value__C= ''+trigger.oldmap.get(prod.id).Maintenance_Rate__c,who_made__C = userMap.get(prod.LastModifiedById).Name, what_time__c=''+prod.LastModifiedDate,Product__c=prod.Id );
         
        AuditInsert.add(audit1);

        }

        if(prod.Sold_Annually__c!= null && prod.Sold_Annually__c!= trigger.oldMap.get(prod.id).Sold_Annually__c)
        {
        Audits__c audit1 = new Audits__c(Name = Prod.Name,Field_changed__C = 'Sold Annualy', New_value__C = ''+prod.Sold_Annually__c, Old_value__C= ''+trigger.oldmap.get(prod.id).Sold_Annually__c,
                                                             who_made__C = userMap.get(prod.LastModifiedById).Name, what_time__c=''+prod.LastModifiedDate,Product__c=prod.Id );
        AuditInsert.add(audit1);
        }
}      insert AuditInsert;
    }
}


And below is my test class:
@istest
private class producthistorytest{

static testmethod void productinsert()
{

Product2  prod = new Product2 ();
Volume_Discount__c vol  = new Volume_Discount__c();

prod.Name = 'test';
prod.Base_List_Price__c = 400.00;
prod.Maintenance_Rate__c = 60;
prod.Sold_Annually__c=True;
prod.Sold_Perpetually__c=True;
prod.IsActive=True;
prod.APAC_Product_Only__c= False;
prod.New_Academic_Model__c=True;
prod.Do_not_Renew__c=true;
prod.Product_Types__c='Applications';
prod.New_Product_Type__c='Applications';
prod.Maintenance_Levels__c = 'Gold';
prod.Minimum_Qty__c = 1;
prod.CAM_Volume_Discount__c = vol.id;
insert prod;

System.debug('Product Detail==>'+prod);
/*ProductAudits__c prodaudit = new productaudits__C(name = 'test1');
insert Prodaudit;*/

prod.Base_List_Price__c = 600.00;
prod.Maintenance_Rate__c = 80;
prod.Sold_Annually__c=True;
prod.Sold_Perpetually__c=False;
prod.IsActive=False;
prod.APAC_Product_Only__c= True;
prod.New_Academic_Model__c=False;
prod.Do_not_Renew__c=false;

update prod;

System.debug('Updated Prod'+prod);
}

}
Gaurav NirwalGaurav Nirwal
You can use this simple process 
trigger Insert_Registration_on_member_status_change on CampaignMember (after update) {
List<miiEventV1__Registration__c> reg = new List<miiEventV1__Registration__c>();
    for (CampaignMember cm : Trigger.new)
         if (CampaignMember.Status != null){
                 reg.add (new miiEventV1__Registration__c(
                     miiEventV1__Client__c = cm.Account_ID__c,
                     miiEventV1__Presentation__c = cm.Related_Presentation_Id__c,
                     miiEventV1__Presentation_Ticket_Type__c =  cm.Create_Ticket_Id__c,
                     miiEventV1__Status__c = 'Registered',
                     miiEventV1__Active__c = TRUE));   
         }
   insert reg;
}




Gaurav NirwalGaurav Nirwal
private class CampaignMemberInsertTest {
    private static testmethod void testUpdateCM() {
        //create & insert test data 
        //make sure to include all required fields and that it will pass any active Validation Rules
        Account acct = new Account (Name = 'Acme, Inc.');
        insert acct;

        Contact con = new Contact(
                          FirstName = 'Robin',
                          LastName = 'Koehler'
                          AccountId = acct.Id);
        insert con;   

        Campaign camp = new Campaign(
                            Name = 'Test',
                            IsActive = TRUE);            
        insert camp;

        CampaignMember m1 = new CampaignMember(
            Contact = con.Id,
            Campaign = camp.Id,
            Account_ID__c = acct.Id );
        insert ( m1 );

        // perform action that runs trigger
        m1.Status = 'Facilitator';
        update m1;

        //assert that data is as we expect it after trigger has run
        List<miiEventV1__Registration__c> regs = [SELECT
             miiEventV1__Client__c, miiEventV1__Status__c, miiEventV1__Active__c 
             FROM miiEventV1__Registration__c
             WHERE miiEventV1__Client__c = acct.Id AND miiEventV1__Active__c = TRUE AND miiEventV1__Status__c = 'Registered']
         System.assert(!regs.isEmpty());
     }
 }

tsritsri
Hi,
Thanks for your quick reply. But, in the Audits object fields,  we need to capture the old values of the Product fields which are changed. For that reason I am using old map in my trigger. Any clue on where am I going wrong in the test class?