+ Start a Discussion
Alex MezaAlex Meza 

Apex Trigger Code Deployment

I was able to write two apex triggers that connect two objects(Contacts and a Custom Object) via a lookup field when they have the same first name, last name, and zip code. This was done in my sandbox environment and I was able to get this working perfectly there. I then, with help through the moudles and references, was able to write a small sample test class in order to have this deployed in my production environment, but i am only able to get 24 percent coverage. 

Please help as I am stuck, below is one of my triggers and my test class. 

 

trigger VoterID2Contact on Voter_File_TX__c (before insert,before update,after insert,after update) 

{ if(checkRecursive.runOnce){ return;}
{
    Set<String> set_Str = new Set<string>();
    Map<String,Contact> mp_ContactID;
    
    if(Trigger.isAfter && Trigger.isUpdate)
    {
        for(Contact ContactID : [Select ID,FirstName, LastName,MailingPostalCode, Voter_File_ID__c From Contact])
        {
                    if(mp_ContactID==null)
                        mp_ContactID = new Map<String,Contact>();
            
            mp_ContactID.put(ContactID.FirstName +''+ContactID.LastName +''+ContactID.MailingPostalCode,ContactID);

 
                    }
        
        for(Voter_File_TX__c VoterList : Trigger.new)
    {
        if(mp_ContactID!=null && mp_ContactID.containsKey(VoterList.First_Name__c+''+ VoterList.Last_Name__c+''+ VoterList.Zipcode__c))

        {

       mp_ContactID.get(VoterList.First_Name__c +''+ VoterList.Last_Name__c +''+ VoterList.Zipcode__c).Voter_File_ID__c = VoterList.id;

}

}
 
if(mp_ContactID!=null && mp_ContactID.values()!=null)
        checkRecursive.runOnce = true;
        update mp_ContactID.values();

    }
}
}

and this is my test class
 
@isTest
public class TestClass 
{
    static testMethod void testMethod1()
    {
        
        Voter_File_TX__c vf = new Voter_File_TX__c();
        vf.First_Name__c ='Test';
        vf.Last_Name__c ='Test';
        vf.RNC_ID__c = 'Test';
        vf.Zipcode__c = 'Test'; 
        
         insert vf;

        Contact cont = new Contact();
        cont.FirstName ='Test';
        cont.LastName = 'Test';
        cont.RNC_ID__c = 'Test';
        cont.MailingPostalCode = 'test';
    
        insert cont;
        
        vf.First_Name__c = cont.FirstName ;
        vf.Last_Name__c = cont.LastName ;
        vf.Zipcode__c = cont.MailingPostalCode ;
        vf.RNC_ID__c = cont.RNC_ID__c ;
        vf.Contact__c = cont.Id ;
        
        cont.FirstName = vf.First_Name__c ;
        cont.LastName = vf.Last_Name__c ;
        cont.MailingPostalCode = vf.Zipcode__c;
        cont.RNC_ID__c = vf.RNC_ID__c ;
        
       
    }

}

 
Best Answer chosen by Alex Meza
Amit Singh 1Amit Singh 1
You need to insert contact first, use below code for test class.
 
@isTest
public class TestClass 
{
    static testMethod void testMethod1()
    {
        Contact cont = new Contact();
        cont.FirstName ='Test';
        cont.LastName = 'Test';
        cont.RNC_ID__c = 'Test';
        cont.MailingPostalCode = 'test';
    
        insert cont;
		
        Voter_File_TX__c vf = new Voter_File_TX__c();
        vf.First_Name__c ='Test';
        vf.Last_Name__c ='Test';
        vf.RNC_ID__c = 'Test';
        vf.Zipcode__c = 'Test'; 
		vf.First_Name__c = cont.FirstName ;
        vf.Last_Name__c = cont.LastName ;
        vf.Zipcode__c = cont.MailingPostalCode ;
        vf.RNC_ID__c = cont.RNC_ID__c ;
        vf.Contact__c = cont.Id ;
        
        insert vf;
        
        cont.FirstName = vf.First_Name__c ;
        cont.LastName = vf.Last_Name__c ;
        cont.MailingPostalCode = vf.Zipcode__c;
        cont.RNC_ID__c = vf.RNC_ID__c ;
		update cont;
        
       
    }

}



Let me know if this helps :)
Thanks!
Amit Singh

All Answers

Amit Singh 1Amit Singh 1
You need to insert contact first, use below code for test class.
 
@isTest
public class TestClass 
{
    static testMethod void testMethod1()
    {
        Contact cont = new Contact();
        cont.FirstName ='Test';
        cont.LastName = 'Test';
        cont.RNC_ID__c = 'Test';
        cont.MailingPostalCode = 'test';
    
        insert cont;
		
        Voter_File_TX__c vf = new Voter_File_TX__c();
        vf.First_Name__c ='Test';
        vf.Last_Name__c ='Test';
        vf.RNC_ID__c = 'Test';
        vf.Zipcode__c = 'Test'; 
		vf.First_Name__c = cont.FirstName ;
        vf.Last_Name__c = cont.LastName ;
        vf.Zipcode__c = cont.MailingPostalCode ;
        vf.RNC_ID__c = cont.RNC_ID__c ;
        vf.Contact__c = cont.Id ;
        
        insert vf;
        
        cont.FirstName = vf.First_Name__c ;
        cont.LastName = vf.Last_Name__c ;
        cont.MailingPostalCode = vf.Zipcode__c;
        cont.RNC_ID__c = vf.RNC_ID__c ;
		update cont;
        
       
    }

}



Let me know if this helps :)
Thanks!
Amit Singh
This was selected as the best answer
Alex MezaAlex Meza
Hey thanks this worked and I was able to deploy to production however, I ran into an error message when I tried to run the trigger in my production environment. Could you help with that? The error message I get is Error:Apex trigger Contact2VoterID caused an unexpected exception, contact your administrator: Contact2VoterID: execution of AfterUpdate caused by: System.QueryException: Non-selective query against large object type (more than 200000 rows). Consider an indexed filter or contact salesforce.com about custom indexing. Even if a field is indexed a filter might still not be selective when: 1. The filter value includes null (for instance binding with a list that contains null) 2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times): Trigger.Contact2VoterID: line 9, column 1.
Amit Singh 1Amit Singh 1
Yes, that means it's taking too long.  A couple ways to get more CPU time are to put the code in an @future method (you get 60 seconds of CPU instead of just 10 seconds), or implement it as a Batch Apex job.  Since both of these methods are asynchronous, the trigger and associated DML will complete before the calls to the AccountServices methods are made - but they will be called eventually.  Would this work for you?

Thanks!
Amit Singh
Alex MezaAlex Meza
Thank you Amit! I just looked into it and turns out I have to write my code into a Batch Apex job since I am looking oer 15 million records!

Thanks for you help now I just have to figure out how to change my code into an batch class.