You need to sign in to do that
Don't have an account?

Test classes for the following trigger
Hi I'm stuck up in writing the test classes for the following triggers to achieve code coverage while deployment
What would be the test class for the following trigger
trigger 1
trigger checktheLeadCheckBox on Contact (after update)
{
for(Contact c : Trigger.New)
{
List<Lead> li = [select Id, Customer_of_PubKCyber__c,Email,Customer_of_PubKLaw__c from Lead where Email =: c.Email AND IsConverted =: false];
for(Lead l : li)
{
If(l.Id != null)
{
l.Customer_of_PubKCyber__c=c.Customer_of_PubKCyber__c;
l.Customer_of_PubKLaw__c = c.Customer_of_PubKLaw__c;
update l;
}
}
}
}
Trigger 2
trigger updatecontactcheckbox on Opportunity (after insert, after update)
{
for(Opportunity o : Trigger.New)
{
if( o.Contact__c != null)
{
Contact c = [select Id, Customer_of_PubKCyber__c,Email,Customer_of_PubKLaw__c from Contact where Id =: o.Contact__c ];
if((o.Subscription_Start_Date__c <= System.Today()) && (o.Subscription_End_Date__c >= System.Today()) && (o.Product_Name__c == 'PubKCyber Newsletter'))
{
c.Customer_of_PubKCyber__c = True;
}
else if(o.Product_Name__c == 'PubKCyber Newsletter')
{
c .Customer_of_PubKCyber__c = false;
}
if((o.Subscription_Start_Date__c <= System.Today()) && (o.Subscription_End_Date__c >= System.Today()) && (o.Product_Name__c == 'PubKLaw Newsletter'))
{
c.Customer_of_PubKLaw__c= True;
}
else if(o.Product_Name__c == 'PubKLaw Newsletter')
{
c.Customer_of_PubKLaw__c = false;
}
update c;
}
}
}
What would be the test class for the following trigger
trigger 1
trigger checktheLeadCheckBox on Contact (after update)
{
for(Contact c : Trigger.New)
{
List<Lead> li = [select Id, Customer_of_PubKCyber__c,Email,Customer_of_PubKLaw__c from Lead where Email =: c.Email AND IsConverted =: false];
for(Lead l : li)
{
If(l.Id != null)
{
l.Customer_of_PubKCyber__c=c.Customer_of_PubKCyber__c;
l.Customer_of_PubKLaw__c = c.Customer_of_PubKLaw__c;
update l;
}
}
}
}
Trigger 2
trigger updatecontactcheckbox on Opportunity (after insert, after update)
{
for(Opportunity o : Trigger.New)
{
if( o.Contact__c != null)
{
Contact c = [select Id, Customer_of_PubKCyber__c,Email,Customer_of_PubKLaw__c from Contact where Id =: o.Contact__c ];
if((o.Subscription_Start_Date__c <= System.Today()) && (o.Subscription_End_Date__c >= System.Today()) && (o.Product_Name__c == 'PubKCyber Newsletter'))
{
c.Customer_of_PubKCyber__c = True;
}
else if(o.Product_Name__c == 'PubKCyber Newsletter')
{
c .Customer_of_PubKCyber__c = false;
}
if((o.Subscription_Start_Date__c <= System.Today()) && (o.Subscription_End_Date__c >= System.Today()) && (o.Product_Name__c == 'PubKLaw Newsletter'))
{
c.Customer_of_PubKLaw__c= True;
}
else if(o.Product_Name__c == 'PubKLaw Newsletter')
{
c.Customer_of_PubKLaw__c = false;
}
update c;
}
}
}
For the first trigger I would do something like this:
This should execue the after update trigger
For the second one I would do something similar. Create Opportunities and after insert and update they should call the trigger. Depending on your records values, it would go inside your code or not.
Hope this helps as start poing
im extremely new test class
Thanks in advance, it would be great if yu help me out
To cover second trigger:
Hope this helps.
Note: Writing a test class with as many assertions as possible is the robust way. watsay?
In fact, a test is to check that your code is working as expected.
So Sangeetha, for instance, for second trigger test, after each opportunity insertion you can check if the contact value is the expected one.
Something like this