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
VFVF 

Trigger Test class

Hello,

 

 

I have an issue in Apex triggers can any one help me out: I have a lraedy made two post but didnot get any reply .please help me out as it is very urjent for me.

 

Here is the code of my Apex trigger test class: 

 

public class sampleTest{

static testMethod void WCSFTrigger(){

    Map<String, Individual_Email_Results__c> lgaMap = new Map<String, Individual_Email_Results__c>();

    Individual_Email_Results__c objInsert = new Individual_Email_Results__c();

    objInsert .Hard_Bounce__c = 1; 

    objInsert .Abuse__c = '1';

    objInsert .GlobalUnsubscribe__c = '1';

    objInsert .UniversalUnsubscribe__c = '1';

    objInsert .Unsubscribe__c = '1';

    objInsert .Lead__c = '00Q90000001S4fL';

    objInsert .Contact__c = '00390000001bZ1V';

   

    insert objInsert ;

 

//Here the triggers fires on updation 

    objInsert . Unsubscribe__c = '3'; 

    update objInsert;

 

    Decimal newValue = objInsert .Hard_Bounce__c;//obj.Hard_Bounce__c;

    Decimal oldValue = 2; 

    String contactID = objInsert.Contact__c;//'00390000001bZ1V';

    String leadID = objInsert.Lead__c; 

    

    system.debug(objInsert.Contact__c);

    if(newValue != oldValue)

    {

    

     system.debug(contactID);

    

    if(contactID  != null)

    {

    

         Contact contact = [Select Id, HasOptedOutOfEmail from contact WHERE Id=: contactID ];

         system.debug(contact);

         system.debug(contact.HasOptedOutOfEmail);

         if(contact.HasOptedOutOfEmail == true)

               {

                contact.HasOptedOutOfEmail = false;

                   //system.assertEquals(contact.HasOptedOutOfEmail , false); 

               }

               else

               {

                contact.HasOptedOutOfEmail = true;

                   //system.assertEquals(contact.HasOptedOutOfEmail , true);

               }

               update contact;

    }

    

    if(objInsert.Lead__c!= null)

    {

        Lead lead = [Select Id, HasOptedOutOfEmail from lead WHERE Id=:objInsert.Lead__c];

         if(lead .HasOptedOutOfEmail == true)

               {

                 system.assertEquals(lead .HasOptedOutOfEmail , true); 

               }

               else

               {

                  system.assertEquals(lead .HasOptedOutOfEmail , true); 


               }

               update lead ;

    }

    

     if( (objInsert.Abuse__c != '2') || (objInsert.GlobalUnsubscribe__c != '2') || (objInsert.UniversalUnsubscribe__c != '2') || (objInsert.Unsubscribe__c != '2'))

        {

            if(objInsert.Contact__c!= null)

            {  

               Contact contact = [Select Id, EmailBouncedDate , EmailBouncedReason from contact WHERE Id=:objInsert.Contact__c];

               system.assertEquals( contact.EmailBouncedReason , 'WhatCounts Hard Bounce'); 

                

               update contact;

                   

            }

            if(objInsert.Lead__c!= null)

            {              

               Lead lead= [Select Id, EmailBouncedDate , EmailBouncedReason from lead WHERE Id=:objInsert.Lead__c];

               system.assertEquals( lead.EmailBouncedReason , 'WhatCounts Hard Bounce'); 

               

               update lead;

            }

         

        }

 

 

I have a problem in writing test class for  code which has bold font .Please let me know if there is any issue.

 

 

 

The trigger is:

 

trigger WCSFTrigger on Individual_Email_Results__c (after update) {

Map<String, Individual_Email_Results__c> lgaMap = new Map<String, Individual_Email_Results__c>();

       for (Individual_Email_Results__c objUpdate: System.Trigger.New) {

       

        Individual_Email_Results__c obj= [Select id,Contact__c,Lead__c,Hard_Bounce__c,Abuse__c,GlobalUnsubscribe__c,UniversalUnsubscribe__c,Unsubscribe__c,Hard_Bounce_Event_Date__c  from Individual_Email_Results__c where id= :objUpdate.Id];

        string contactID = obj.Contact__c;

        string leadID = obj.Lead__c;

        Decimal newValue = trigger.new[0].Hard_Bounce__c;

        Decimal oldValue = trigger.old[0].Hard_Bounce__c;

       

        if(newValue != oldValue )

        {

            if(contactID != null)

            {  

               Contact contact = [Select Id, HasOptedOutOfEmail from contact WHERE Id=:contactID ];

               if(contact.HasOptedOutOfEmail == true)

               {

                  contact.HasOptedOutOfEmail = false;

               }

               else

               {

                  contact.HasOptedOutOfEmail = true;

               }

               update contact;

                   

            }

            if(leadId != null)

            {              

               Lead lead = [Select Id, HasOptedOutOfEmail from lead WHERE Id=: leadId];

               if(lead.HasOptedOutOfEmail == true)

               {

                  lead.HasOptedOutOfEmail = false;

               }

               else

               {

                   lead.HasOptedOutOfEmail = true;

               }

               update lead;

            }

             

        }        

        if( (trigger.new[0].Abuse__c != trigger.old[0].Abuse__c) || (trigger.new[0].GlobalUnsubscribe__c != trigger.old[0].GlobalUnsubscribe__c) || (trigger.new[0].UniversalUnsubscribe__c != trigger.old[0].UniversalUnsubscribe__c) || (trigger.new[0].Unsubscribe__c != trigger.old[0].Unsubscribe__c))

        {

            if(contactID != null)

            {  

               Contact contact = [Select Id, EmailBouncedDate , EmailBouncedReason from contact WHERE Id=:contactID ];

               contact.EmailBouncedReason = 'WhatCounts Hard Bounce';

               contact.EmailBouncedDate = datetime.valueOf(obj.Hard_Bounce_Event_Date__c);

               update contact;

                   

            }

            if(leadId != null)

            {              

               Lead lead= [Select Id, EmailBouncedDate , EmailBouncedReason from lead WHERE Id=:leadID ];

               lead.EmailBouncedReason = 'WhatCounts Hard Bounce';

               lead.EmailBouncedDate =  datetime.valueOf(obj.Hard_Bounce_Event_Date__c);

               update lead;

            }

         

        }

        }

       

 

 

Please let me know if any changes has to be done.

I have followed the APEX DOCS TO WRITE THE CODE. I have already made two posts regarding this issue but didnot get any reply.

 

Thanks

 

homer671homer671

I would refactor your code to move the large if statements within the trigger.* and place it in another class.

 

Maybe call it:

 

Individual_Email_ResultController();

 

Place a static class that the trigger can see easily.

 

IndividualEmailResultsController.hasContactID();

 

This way you can test that method ... without having to test the full trigger.

 

etc ... and then write unit tests around that ... its a lot easier then doing all the code within the trigger.  And a lot easier to test to move it to another class.   At least from what I've been testing.

 

I hope that helps.