You need to sign in to do that
Don't have an account?
KevinRussell
Create test for Contact trigger.
New at this, so, I create a trigger to send email if certain Contact fields have been updated. To my surprise, I find I cannot deploy or create the trigger in the production company, I need to create a trigger test. So I find an example and experiment but I'm not getting it. Could someone help with how I can create an APEX Class test for this trigger, just enought so I get the idea to finish it?
Thanks if you can.
Kevin
This is the begginging of may last attempt at writing an APEX test.
@isTest private class ContactChangeCodeCoverageTestTest { //this section of the test should send no email because the field values //have not changed. static TestMethod void Test0_TestNoFieldChanges() { string OldEmail = 'OldEmail@test.com'; sendContactEmailAlertValue(OldEmail); } //this section of the test should send email because the field values //have changed. static TestMethod void Test1_TestWithFieldChanges() { string NewEmail = 'NewEmail@test.com'; sendContactEmailAlertValue(NewEmail); } private static void sendContactEmailAlertValue(String EmailValue) { if (EmailValue == 'OldEmail@test.com') { //don't send email } else { //send email } } }
trigger ContactChangeCodeCoverageTest on Contact (after update) { try { // Your code here //holds value before change date OldBirthdate; String OldEmail; String OldHomePhone; String OldMobilePhone; String OldPhone; String OldTitle; string OldMailingAddress; string OldOtherAddress; date NewBirthdate; String NewEmail; String NewHomePhone; String NewMobilePhone; String NewPhone; String NewTitle; string NewMailingAddress; string NewOtherAddress; String oldAccountName; String newAccountName; string GraduationYear; string FieldChanges; string ContactName; Boolean SendEmail; //I don't want to send the email unless the fields I'm checking for have changed. //In other words, non-significant Contact fields can change and I won't send Contact Changed spam. SendEmail = False; //create a set to hold all of the parent account ids that we need to get names for Set<Id> accIds=new Set<id>(); //create a set for contacts that need checked so we don't have to go through the whole list again List<Contact> checkMe = new List<Contact>(); //look at the data for (Contact con : trigger.new) { NewBirthdate = con.Birthdate; NewEmail = con.Email; NewHomePhone = con.HomePhone; NewMobilePhone = con.MobilePhone; NewPhone = con.Phone; NewTitle = con.Title; GraduationYear = con.Graduation_Year__c; ContactName = con.FirstName; ContactName = ContactName + ' ' + con.LastName; NewMailingAddress = con.MailingStreet + ', ' + con.MailingCity + ', ' + con.MailingState + ', ' + con.MailingCountry; NewOtherAddress = con.OtherStreet + ', ' + con.OtherCity + ', ' + con.OtherState + ', ' + con.OtherCountry; //This looks to see if the current Account ID matches the //previous Account ID if(con.AccountId != trigger.oldMap.get(con.Id).AccountId){ //add the new account to the set accIds.add(con.accountId); //add the old account to the set accIds.add(trigger.oldMap.get(con.Id).AccountId); //add this contact with a changed account to the list to be checked later on checkMe.add(con); //go get the accounts that we need names for and put them in a map that that is keyed by the accountID Map<Id, Account> accMap = new Map<Id,Account>([select id, Name from Account where id in :accIds]); for (Contact c : checkMe) { //get the old account id Id oldAcctID = trigger.oldMap.get(con.Id).AccountId; //get the name of the old account oldAccountName = accMap.get(oldAcctID).Name; //get the new account id Id newAcctID = c.AccountId; //get the new Account name newAccountName = accMap.get(newAcctID).Name; //do you comparison from here } } } for (Contact c : Trigger.old) { OldBirthdate = c.Birthdate; OldEmail = c.Email; OldHomePhone = c.HomePhone; OldMobilePhone = c.MobilePhone; OldPhone = c.Phone; OldTitle = c.Title; OldMailingAddress = c.MailingStreet + ', ' + c.MailingCity + ', ' + c.MailingState + ', ' + c.MailingCountry; OldOtherAddress = c.OtherStreet + ', ' + c.OtherCity + ', ' + c.OtherState + ', ' + c.OtherCountry; } FieldChanges = 'Alumni Contact Information for ' + ContactName + ' has changed:' + '<br>'; FieldChanges = FieldChanges + '<br>'; if (OldMailingAddress != NewMailingAddress) { FieldChanges = FieldChanges + 'Mailing Address: ' + OldMailingAddress + ' --> ' + NewMailingAddress + '<br>'; SendEmail = True; } if (OldOtherAddress != NewOtherAddress) { FieldChanges = FieldChanges + 'Other Address: ' + OldOtherAddress + ' --> ' + NewOtherAddress + '<br>'; SendEmail = True; } if (oldAccountName != newAccountName) { FieldChanges = FieldChanges + 'Account: ' + oldAccountName + ' --> ' + newAccountName + '<br>'; SendEmail = True; } if (OldTitle != NewTitle) { FieldChanges = FieldChanges + 'Title: ' + OldTitle + ' --> ' + NewTitle + '<br>'; SendEmail = True; } if (OldEmail != NewEmail) { FieldChanges = FieldChanges + 'Email: ' + OldEmail + ' --> ' + NewEmail + '<br>'; SendEmail = True; } if (OldPhone != NewPhone) { FieldChanges = FieldChanges + 'Phone: ' + OldPhone + ' --> ' + NewPhone + '<br>'; SendEmail = True; } if (OldBirthdate != NewBirthdate) { FieldChanges = FieldChanges + 'Birthdate: ' + OldBirthdate + ' --> ' + NewBirthdate + '<br>'; SendEmail = True; } if (OldHomePhone != NewHomePhone) { FieldChanges = FieldChanges + 'Home Phone: ' + OldHomePhone + ' --> ' + NewHomePhone + '<br>'; SendEmail = True; } if (OldMobilePhone != NewMobilePhone) { FieldChanges = FieldChanges + 'Mobile Phone: ' + OldMobilePhone + ' --> ' + NewMobilePhone + '<br>'; SendEmail = True; } if (GraduationYear <> null && SendEmail ) { // Send Email Notification Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {'testaddress@testaddress.com'}; mail.setToAddresses(toAddresses); mail.setReplyTo('emailaddress@test.com'); mail.setSenderDisplayName('My Name'); mail.setSubject('Alumn: ' + ContactName + ' Contact Information changed.'); mail.setBccSender(false); mail.setUseSignature(false); mail.setHtmlBody(FieldChanges); //Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } } catch (Exception e) { // Generic exception handling code here } }