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

Help Writing Test Class for Trigger
Hi,
We are not able to use the deploy function at our company because of a trigger on the production server that has 0% code coverage. I am an administrator and have never had to write test code before. Have read everything I can about how to do it, but I don't understand the logic. Have tried to write it in every way I could possibly think of. Could you please try to help me write a test class for this?
I REALLY appreciate all help I can get!
This is the trigger:
trigger subscription on subscription_update__c (after insert) {
Map<Id,Subscription_Update__c> subsToKill = new Map<Id,Subscription_Update__c>();
List<Contact> contactsToProcess = new List<Contact>();
for (Subscription_Update__c sub : Trigger.new) {
Contact contact = new Contact (
id=sub.contact__c,
Email = sub.email_address__c,
HasOptedOutOfEmail = sub.Unsubscribe_Me__c,
Subscription_Type__c = sub.Subscription_Type__c );
contactsToProcess.add(contact);
subsToKill.put(sub.id,sub);
}
if(contactsToProcess.size()>0){
try {
update contactsToProcess;
//delete subsToKill.values();
} catch (exception e) {
system.debug(e.getMessage());
}
}
}
Hi Jill Hedberg,
Can you try following code this may help you.
Error in previous code is "I think Name field is Autonumber or not writable for Subscription_Update__c"
So, thats why it giving error.
@IsTest
public class subscription_test{
static testmethod void MyUnitTest(){
Contact con = new Contact(Lastname='ABC');
insert con;
Subscription_Update__c sb =new Subscription_Update__c(contact__c=con.id,email_address__c='abc@xyz.com',Subscription_Type__c='ABC',Unsubscribe_Me__c=true);
test.StartTest();
insert sb;
test.StopTest();
}
}
If this is your solution then please mark it as a solution that will help any others
All Answers
Hi,
Please try below :-
@IsTest(SeeAllData=true)
public class subscription_test{
static testmethod void MyUnitTest(){
Contact con = new Contact(Lastname='ABC');
insert con;
Subscription_Update__c sb =new Subscription_Update__c(Name='Test',contact__c=con.id,email_address__c='abc@xyz.com',Subscription_Type__c='ABC',Unsubscribe_Me__c=true<------>);//Populate all the mandatory fields
insert sb;
}
}
Thank you for answering, I get an error trying to save though. It says:
Error: Compile Error: unexpected token:'<' at line 8 column 175
Were I supposed to add something in this code myself. I haven't learned coding yet, so please bare with me :)
Hi Jill Hedberg,
I guess you get the error in this line.
Subscription_Update__c sb =new Subscription_Update__c(Name='Test',contact__c=con.id,email_address__c='abc@xyz.com',Subscription_Type__c='ABC',Unsubscribe_Me__c=true<------>);//Populate all the mandatory fields
<------> ?
You just remove that , it is written to continue, and populate all the mandatory fields.
\If this answers your question, please mark it as solution.
Thanks
Agreed with Souvik,that was written to continue if you have nay more required fields on that object,if there is none remove it and you are good to go :)
I don't think there are more so I removed <------> and tried to save. But then another error message appears: Error: Compile Error: Field is not writeable: Subscription_Update__c.Name at line 8 column 64.
Subscription_Update__c is a lookup field if that explains anything.
Hi Jill Hedberg,
Can you try following code this may help you.
Error in previous code is "I think Name field is Autonumber or not writable for Subscription_Update__c"
So, thats why it giving error.
@IsTest
public class subscription_test{
static testmethod void MyUnitTest(){
Contact con = new Contact(Lastname='ABC');
insert con;
Subscription_Update__c sb =new Subscription_Update__c(contact__c=con.id,email_address__c='abc@xyz.com',Subscription_Type__c='ABC',Unsubscribe_Me__c=true);
test.StartTest();
insert sb;
test.StopTest();
}
}
If this is your solution then please mark it as a solution that will help any others
Woohoo, it works! You guys are awsome! Thank you so much for taking the time to help me.