You need to sign in to do that
Don't have an account?
LFI
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
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
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
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
@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;
}
}
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!
It was too late here so I signed off for the day...Good that you were able to solve the errors....
Thanks LFI!!