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
LFILFI 

Each trigger must have at least 1% code coverage error

Hello,
I am trying to deploy my first trigger to production and getting this error. Can someone assist? Below is the code for the trigger:

trigger rosterTrigger on Roster__c  (after insert) {
    
        public set<String> strTypes = new set<String>();
        public set<String> strYears = new set<String>();
        public list<Contact> lstContact = new list<Contact>();
        public list<Contact_to_Roster_Association__c> lstContactAssociations = new list<Contact_to_Roster_Association__c> ();
        
         for(Roster__c objRoster : trigger.new)
         {
             strTypes.add(objRoster.Program_Type__c);
             strYears.add(objRoster.Program_Year__c);
         }
                 
         for(Contact objContact : [Select Id ,Alumni_Type__c,  Alumni_Year__c from Contact where Alumni_Type__c IN:  strTypes OR Alumni_Year__c IN : strYears])
         {
             
             if(objContact.Alumni_Type__c !=null && objContact.Alumni_Year__c !=null)
             {
                 lstContact.add(objContact);
             }
             
         }
        
         for(Roster__c objC : trigger.new)
         {
             for(Contact objContact : lstContact)
            {
                Contact_to_Roster_Association__c objRA = new Contact_to_Roster_Association__c();
                objRA.Contact__c = objContact.Id;
                objRA.Roster__c = objC.Id;
                lstContactAssociations.add(objRA);
            }
         }
        
        if(!lstContactAssociations.isEmpty())
        {
            insert lstContactAssociations;
        }
}

More info about the trigger here: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000B2ug 
Best Answer chosen by LFI
sandeep sankhlasandeep sankhla
Hi LFI,

To cover the code for this you can create one test class and insert below code there it will cover your code...

Contact objContact = new Contact(LastName='Tst');
objContact.Alumni_Type__c = 'testType';
objContact.Alumni_Year__c  = '2002';
insert objContact;

Roster__c objR = new Roster__c ();
objR.Program_Type__c = 'testType';
objR.Program_Year__c = '2002';

insert objR;


P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
 

All Answers

sandeep sankhlasandeep sankhla
Hi LFI,

To cover the code for this you can create one test class and insert below code there it will cover your code...

Contact objContact = new Contact(LastName='Tst');
objContact.Alumni_Type__c = 'testType';
objContact.Alumni_Year__c  = '2002';
insert objContact;

Roster__c objR = new Roster__c ();
objR.Program_Type__c = 'testType';
objR.Program_Year__c = '2002';

insert objR;


P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
 
This was selected as the best answer
LFILFI
Getting error: Error: Compile Error: Field is not writeable: Contact.Alumni_Type__c at line 6 column 5​

@isTest 
private class HelloWorldTestClass {

static testMethod void validateHelloWorld() {
    Contact objContact = new Contact(LastName='Tst');
    objContact.Alumni_Type__c= 'LFI';
    objContact.Alumni_Year__c= '2002';
    insert objContact;

    Roster__c objR = new Roster__c ();
    objR.Program_Type__c = 'testType';
    objR.Program_Year__c = '2002';

    insert objR;
    }
}
LFILFI

I got it, below is the final code:
@isTest 
private class TriggerTestClass {

static testMethod void validateTrigger() {
    
    Contact objContact = new Contact(LastName='Tst', User_ID__c='11111111');
    objContact.LFI__c= 'LFI';
    objContact.LFI_Year__c= '2005';
    insert objContact;

    Roster__c objR = new Roster__c ();
    objR.Program_Type__c = 'LFI';
    objR.Program_Year__c = '2005';

    insert objR;
    }
}

Biggest thanks to Sandeep for getting me started!

 
sandeep sankhlasandeep sankhla
Hi LFI,

It was too late here so I signed off for the day...Good that you were able to solve the errors....

Thanks LFI!!