When the trigger is called, I'm looking to pass the Trigger.new list into the ContactTriggerHandler class, and then filter on that list to be a certain record type and associated with an Account Contact Relationship (AccountContactRelation object) that has a certain role. This compiles but is not triggering any of the error codes. I passed con directly to the for loop and it works as intended (doesn't check relationships or record type however), so I'm thinking that it may be the SOQL query. When I run the test, I only get 63% code coverage (image below).
Trigger
trigger ContactTrigger on Contact (before update) {
if(trigger.isbefore){
if(trigger.isupdate){
ContactTriggerHandler.IsPricingLetter(Trigger.new);
}//End If IsUpdate
}//End If IsBefore
}//End Class
Class
public class ContactTriggerHandler {
public static void IsPricingLetter(List<Contact> con){
List<Contact> ContactLookup = [SELECT id,mailingstreet,mailingcity,mailingstate,mailingpostalcode,mailingcountry,email,active_contact__c
FROM contact WHERE id IN :con
AND id IN (SELECT Contactid
FROM accountcontactrelation
WHERE roles INCLUDES (:label.messer_US_Pricing_Letter) AND isactive=true)
AND Recordtypeid='0125b0000015OkJAAU'
];
for(Contact CheckContact : ContactLookup){
if(CheckContact.MailingStreet==null){
CheckContact.MailingStreet.addError(label.Messer_US_Contact_Street+' on a '+label.messer_US_Pricing_Letter+' Contact cannot be null.');
}//End If Mailing Street
if(CheckContact.MailingCity==null){
CheckContact.MailingCity.addError(label.Messer_US_Contact_City+' on a '+label.messer_US_Pricing_Letter+' Contact cannot be null.');
}//End If Mailing City
if(CheckContact.MailingState==null){
CheckContact.MailingState.addError(label.Messer_US_Contact_State+' on a '+label.messer_US_Pricing_Letter+' Contact cannot be null.');
}//End If Mailing State
if(CheckContact.MailingPostalCode==null){
CheckContact.MailingPostalCode.addError(label.Messer_US_Contact_Postal_Code+' on a '+label.messer_US_Pricing_Letter+' Contact cannot be null.');
}//End If Mailing Postal Code
if(CheckContact.MailingCountry==null){
CheckContact.MailingCountry.addError(label.Messer_US_Contact_Country+' on a '+label.messer_US_Pricing_Letter+' Contact cannot be null.');
}//End If Mailing Country
if(CheckContact.Email==null){
CheckContact.Email.addError(label.Messer_US_Contact_Email+' on a '+label.messer_US_Pricing_Letter+' Contact cannot be null.');
}//End If Email
if(CheckContact.Active_Contact__c==false){
CheckContact.active_contact__c.addError('A Contact with an active '+label.messer_US_Pricing_Letter+' relationship cannot be made inactive.');
}//End If Active
}//end for loop
}//end isPricingLetter method
}//End Class
Test Class
@isTest
public class ContactTriggerHandlerTest {
public static Account getAccount(){
ccrz__E_AccountGroup__c accountgroupObj = new ccrz__E_AccountGroup__c(Name = 'Messer Base',
CurrencyIsoCode = 'USD',
ccrz__PriceListSelectionMethod__c = 'Best Price');
insert accountgroupObj;
account a = new account(name='DevTest Account',Type='Prospect',Industry='Chemistry & Energy',
ccrz__E_AccountGroup__C=accountgroupobj.id);
insert a;
return a;
}//End getAccount
public static Contact getContact(){
Account a = getAccount();
contact c = new contact(firstname = 'TestFirstName',active_contact__c=true,lastname = 'TestLastName',mailingstreet = 'Test Street',mailingcity = 'Test City',
mailingState = 'Test State',mailingPostalCode = '99999',mailingCountry = 'US',email = 'test@test.com',accountid=a.id
,recordtypeid='0125b0000015OkJAAU');//CRM Contact US Record Type
insert c;
return c;
}//End getContact
static testmethod void updateStreetWithPricingLettersRelationship(){
Contact c = getContact();
Account a = getAccount();
accountcontactrelation acr = new accountcontactrelation(accountid=a.id,contactid = c.id,Roles = label.messer_US_Pricing_Letter,isActive=true);
insert acr;
try{
c.mailingstreet='';
update c;
}catch(DMLException e){
System.debug(e);
}finally{
System.debug('Mailing Street Checked!');
}
}//End updateStreetwithRelationship
static testmethod void updateCityWithPricingLettersRelationship(){
Contact c = getContact();
Account a = getAccount();
accountcontactrelation acr = new accountcontactrelation(accountid = a.Id,contactid = c.id,Roles = label.messer_US_Pricing_Letter,isActive=true);
insert acr;
try{
c.mailingcity='';
update c;
}catch(DMLException e){
System.debug(e);
}finally{
System.debug('Mailing City Checked!');
}
}//End updateCitywithRelationship
static testmethod void updateStateWithPricingLettersRelationship(){
Contact c = getContact();
Account a = getAccount();
accountcontactrelation acr = new accountcontactrelation(accountid = a.Id,contactid = c.id,Roles = label.messer_US_Pricing_Letter,isActive=true);
insert acr;
try{
c.MailingState='';
update c;
}catch(DMLException e){
System.debug(e);
}
finally{
System.debug('Mailing State Checked!');
}
}//End updateStatewithRelationship
static testmethod void updatePostalCodeWithPricingLettersRelationship(){
Contact c = getContact();
Account a = getAccount();
accountcontactrelation acr = new accountcontactrelation(accountid = a.Id,contactid = c.id,Roles = label.messer_US_Pricing_Letter,isActive=true);
insert acr;
try{
c.MailingPostalCode='';
update c;
}catch(DMLException e){
System.debug(e);
}
finally{
System.debug('Mailing Postal Code Checked!');
}
}//End updatePostalCodewithRelationship
static testmethod void updateCountryWithPricingLettersRelationship(){
Contact c = getContact();
Account a = getAccount();
accountcontactrelation acr = new accountcontactrelation(accountid = a.Id,contactid = c.id,Roles = label.messer_US_Pricing_Letter,isActive=true);
insert acr;
try{
c.MailingCountry='';
update c;
}catch(DMLException e){
System.debug(e);
}finally{
System.debug('Mailing Country Checked!');
}
}//End updateCountrywithRelationship
static testmethod void updateEmailWithPricingLettersRelationship(){
Contact c = getContact();
Account a = getAccount();
accountcontactrelation acr = new accountcontactrelation(accountid = a.Id,contactid = c.id,Roles = label.messer_US_Pricing_Letter,isActive=true);
insert acr;
try{
c.Email='';
update c;
}catch(DMLException e){
System.debug(e);
}finally{
System.debug('Email Checked!');
}
}//End updateEmailwithRelationship
static testmethod void DeactivateContactAttempt(){
Contact c = getContact();
Account a = getAccount();
accountcontactrelation acr = new accountcontactrelation(accountid = a.Id,contactid = c.id,Roles = label.messer_US_Pricing_Letter,isActive=true);
insert acr;
try{
c.Active_Contact__c=false;
update c;
}catch(DMLException e){
System.debug(e);
}finally{
System.debug('Active Contact? Checked!');
}
}//End updateEmailwithRelationship
}//End Class