function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Eric_HDCEric_HDC 

Change set won't validate

We have a Web2lead type setup that creates contacts.  My trigger will prevent duplicate records from being inserted. The key is based on email address, lead source, and creation date.

 

I have a contact trigger "AvoidWebleadDuplicatesTrigger" and corresponding test class "AvoidWebleadDuplicates_Test" that work fine in the sandbox, but when I upload the change set to Production and validate it, I get the following error message.


Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, This contact already exists: john_vendor@mailinator.com - Fitzgerald Website: []", Failure Stack Trace: "Class.eventsController.Setup: line 20, column 1 Class.eventsControllerTests.myPage_Test: line 13,...

 

I have attached my trigger and test classes and also the "eventsController" and "eventsControllerTests" classes below.

 

trigger AvoidWebleadDuplicatesTrigger on Contact (before insert)
{
    set <string> setEmail = new set<string>();
    set <string> setLeadSource = new set<string>();

    list <contact> currentcontacts = new list<contact>();

    for(contact acc:trigger.new)
    {
        setEmail.add(acc.Email);
        setLeadSource.add(acc.LeadSource);
    }   

    currentcontacts =   [select Email,LeadSource,CreatedDate,id
                        from contact
                        where Email in:setEmail and LeadSource in:setLeadSource and CreatedDate = TODAY];

    for(contact acc:trigger.new)  
    {
        if( currentcontacts.size() > 0 )
            acc.adderror('This contact already exists: ' + acc.Email + ' - ' + acc.LeadSource );
    }

}

 

Test class

@istest
class AvoidWebleadDuplicates_Test
{
    static testmethod void test()
    {
        //Create the conditions to meet the steps taken by your class
        //Create 4 contacts (2 unique ones and 2 duplicates)

        Account a = new Account(name='Test');
        insert a;
       
        List<Contact> contacts = new List<Contact>();
       
        Contact c1 = new Contact (FirstName = 'joe', LastName='smith1', Email = 'joesmith1@gmail.com', LeadSource = 'Fitzgerald Website', AccountId = a.Id);
        Contact c2 = new Contact (FirstName = 'joe', LastName='smith2', Email = 'joesmith2@gmail.com', LeadSource = 'Fitzgerald Test', AccountId = a.Id);
        Contact c3 = new Contact (FirstName = 'joe', LastName='smith1', Email = 'joesmith1@gmail.com', LeadSource = 'Fitzgerald Website', AccountId = a.Id);
        Contact c4 = new Contact (FirstName = 'joe', LastName='smith2', Email = 'joesmith2@gmail.com', LeadSource = 'Fitzgerald Test', AccountId = a.Id);       

        contacts.add(c1);
        contacts.add(c2);
        contacts.add(c3);
        contacts.add(c4);
       
        insert contacts;
    }
}

 

Referenced interfering class:

public with sharing class eventsController {

    public boolean displayPopup {get; set;}
    
    public Pagereference Setup(){

        // Create account
        PageReference pageRef = new PageReference('/apex/EventsSuccess');

        {
          //Create Sample Event Vendor Account and Contact
          Account a1 = new Account(Name = 'Test-Vendor', type = 'Event Vendor', phone = '(512) 555-1234',
          ShippingStreet = '123 Main St.', ShippingCity = 'Austin', ShippingState = 'TX', ShippingPostalCode = '78701',
          ShippingCountry = 'USA');
          insert a1;

          Contact c1 = new Contact(FirstName = 'John', LastName = 'Vendor', AccountId = a1.Id, Email = 'john_vendor@mailinator.com',
          LeadSource = 'Fitzgerald Website', Phone = '(512) 555-1234', Title = 'Event Coordinator', MailingStreet = '123 Main St.', MailingCity = 'Austin',
          MailingState = 'TX', MailingPostalCode = '78701', MailingCountry = 'USA');
          insert c1;

          //Create Sample Customer Account and Contacts
          Account a2 = new Account(Name = 'Test-Customer', type = 'Customer - Direct', phone = '(512) 555-5678',
          ShippingStreet = '321 6th St.', ShippingCity = 'Austin', ShippingState = 'TX', ShippingPostalCode = '78701',
          ShippingCountry = 'USA');
          insert a2;

          Contact c2 = new Contact(FirstName = 'Jane', LastName = 'Customer', AccountId = a2.Id, Email = 'jane_customer@mailinator.com',
          LeadSource = 'Fitzgerald Website', Phone = '(512) 555-5678', Title = 'Tech Manager', MailingStreet = '321 6th St.', MailingCity = 'Austin',
          MailingState = 'TX', MailingPostalCode = '78701', MailingCountry = 'USA');       
          insert c2;
            
          Contact c3 = new Contact(FirstName = 'Jeff', LastName = 'Customer', AccountId = a2.Id, Email = 'jeff_customer@mailinator.com',
          LeadSource = 'Fitzgerald Website', Phone = '(512) 555-5678', Title = 'CEO', MailingStreet = '321 6th St.', MailingCity = 'Austin',
          MailingState = 'TX', MailingPostalCode = '78701', MailingCountry = 'USA');
          insert c3;
            
          //Create sample venue and rooms
          Venue__c v1 = new Venue__c(Name = 'Test Convention Center', Street_Address_1__c= '500 E Cesar Chavez St', 
          City__c = 'Austin', State__c='TX', Postal_Code_Zip__c='78701', Country__c='USA');
          insert v1;
         
          Room__c r1 = new Room__c(Name = 'Main Hall',Venue__c=v1.Id);
          insert r1;              
          Room__c r2 = new Room__c(Name = 'Classroom A',Venue__c=v1.Id);
          insert r2;
          Room__c r3 = new Room__c(Name = 'Classroom B',Venue__c=v1.Id);
          insert r3;                            
          
          Date sDate= System.today().addDays(30);
          Date eDate= System.today().addDays(31);
             
          //Create sample Event, Tracks , Sessions and Speakers
          Event__c ev = new Event__c(Name = 'Test Event', Event_Manager__c=UserInfo.getUserId(), Venue__c=v1.Id,               
          Event_Description__c='This is a test event', Event_Type__c='User Conference', Event_Vendor_Account__c =a1.Id, 
          Event_Website__c='www.dreamforce.com', Primary_Vendor_Contact__c=c1.Id, Region__c='Worldwide',
          Venue_Status__c='Confirmed', Event_Start_Date__c=sDate, Event_End_Date__c=eDate, Maximum_Registration__c=10,
          Targeted_Attendance__c='Users and Developers of our products');
          insert ev; 
              
          Track__c t1 = new Track__c(Name = 'Business', Event__c=ev.Id, Track_Status__c='Finalized',Track_Category__c='Category #1');
          insert t1;
          Track__c t2 = new Track__c(Name = 'Technical', Event__c=ev.Id, Track_Status__c='Finalized',Track_Category__c='Category #2');
          insert t2;              
          
          Session__c se1 = new Session__c(Name = 'Opening Keynote (Biz)', All_Speakers_Identified__c='Yes', Room__c=r1.Id,
          Room_Setup__c = 'Theater', Track__c = t1.Id, Level__c='All', Session_Format__c= 'Keynote', Session_Date__c=sDate,
          Session_Time__c = '8:00 AM', Session_Status__c = 'Ready to Publish');
          insert se1;
          Session__c se2 = new Session__c(Name = 'Business Today', All_Speakers_Identified__c='Yes', Room__c=r2.Id,
          Room_Setup__c = 'Rounds', Track__c = t1.Id, Level__c='Beginner', Session_Format__c= 'Presentation with customer(s)',
          Session_Date__c=sDate, Session_Time__c = '9:00 AM', Session_Status__c = 'Abstract - Submitted');
          insert se2;
          Session__c se3 = new Session__c(Name = 'Opening Keynote (Tech)', All_Speakers_Identified__c='Yes', Room__c=r1.Id,
          Room_Setup__c = 'Theater', Track__c = t2.Id, Level__c='All', Session_Format__c= 'Keynote', Session_Date__c=eDate,
          Session_Time__c = '8:00 AM', Session_Status__c = 'Dry Run - Complete');
          insert se3;
          Session__c se4 = new Session__c(Name = 'Intro to Programming', All_Speakers_Identified__c='Yes', Room__c=r2.Id,
          Room_Setup__c = 'Classroom', Track__c = t2.Id, Level__c='Intermediate', Session_Format__c= 'Hands-on training',
          Session_Date__c=eDate, Session_Time__c = '9:00 AM', Session_Status__c = 'Slides - Locked');
          insert se4;
              
          Speaker__c sp1 = new Speaker__c(Speaker_Contact__c=c1.Id, Session__c=se1.Id, Account__c=a1.Id, Track__c=t1.Id,
          Speaker_Status__c='Ready to Publish');
          insert sp1;
          Speaker__c sp2 = new Speaker__c(Speaker_Contact__c=c3.Id, Session__c=se2.Id, Account__c=a2.Id, Track__c=t1.Id,
          Speaker_Status__c='Ready to Publish');
          insert sp2;
          Speaker__c sp3 = new Speaker__c(Speaker_Contact__c=c1.Id, Session__c=se3.Id, Account__c=a1.Id, Track__c=t2.Id,
          Speaker_Status__c='Request - accepted');
          insert sp3;
          Speaker__c sp4 = new Speaker__c(Speaker_Contact__c=c2.Id, Session__c=se4.Id, Account__c=a2.Id, Track__c=t2.Id,
          Speaker_Status__c='Request - sent');
          insert sp4;                                                      
            
          return pageRef;
        }

    }

    public void closePopupOK() {
        
        RemoveData();
        displayPopup = false;
    }

    public void closePopupCancel() {
        displayPopup = false;
    }
 
    public void showPopup() {
        displayPopup = true;
    }
    
    private void RemoveData(){

      try {
        List<Account> ac1= [SELECT Id From Account Where Name = 'Test-Vendor'];
        delete ac1;
        }
      catch (exception e) {        //Likely the record does not exist
        }  

      try {
        List<Account> ac2= [SELECT Id From Account Where Name = 'Test-Customer'];
        delete ac2;                                                   
        }
      catch (exception e) {        //Likely the record does not exist
        }
                
      try {
        List<Venue__c> ven1= [SELECT Id From Venue__c Where Name = 'Test Convention Center'];
        delete ven1; 
        }
      catch (exception e) {        //Likely the record does not exist
        }
                  
      try {
        List<Event__c> event1 = [select Id from Event__c Where Name = 'Test Event'];
        delete event1;
        }
      catch (exception e) {        //Likely the record does not exist        
      }   
      
      try {
      List<Contact> con1 = [select Id from Contact Where Email = 'john_vendor@mailinator.com'];
      delete con1;
      }
      catch(exception e) { // Likely the reord does not exist
      }

      try {
      List<Contact> con2 = [select Id from Contact Where Email = 'jane_customer@mailinator.com'];
      delete con2;
      }
      catch(exception e) { // Likely the reord does not exist
      }
      
      try {
      List<Contact> con3 = [select Id from Contact Where Email = 'jeff_customer@mailinator.com'];
      delete con3;
      }
      catch(exception e) { // Likely the reord does not exist
      }
      //return pageRef;

    }    

}

 Referenced test class

public class eventsControllerTests {

static testMethod void myPage_Test()
  {
  //Test converage for the myPage visualforce page
  PageReference pageRef = Page.Events_Home;
  Test.setCurrentPageReference(pageRef);
  // create an instance of the controller
  eventsController myPageCon = new eventsController();
  //try calling methods/properties of the controller in all possible scenarios to get the best coverage.

  myPageCon.Setup();
  String efSetupPage = myPageCon.Setup().getUrl();
  System.assertEquals('/apex/EventsSuccess',efSetupPage);

  myPageCon.closePopupOK();

  }
  
    static testMethod void testRegistrationsTrigger(){
        
        //First, prepare 200 contacts for the test data
        
        String contactLastName = 'Apex';
        String contactFirstName = 'Joe';
        String contactTitle = 'Manager';
        String contactEmail = 'apexjoe@yahoo.com';
        String contactLeadSource = 'Fitzgerald Website';
        
        Contact ct = new Contact(LastName=contactLastName, FirstName=contactFirstName, Title=contactTitle, Email=contactEmail, LeadSource=contactLeadSource);
        insert ct;
        
        ct = [SELECT Name, Title from Contact WHERE Id = :ct.Id];
        
        Event__c ev = new Event__c(name = 'My Event');
        insert ev;

        Event_Registration__c reg = new Event_Registration__c(Contact__c=ct.Id,Event__c=ev.Id);
        insert reg;

        reg = [SELECT Name_on_Badge__c, Title_On_Badge__c from Event_Registration__c WHERE Id = :reg.Id];
        System.assertEquals(ct.Name, reg.Name_on_Badge__c);
        System.assertEquals(ct.Title, reg.Title_on_Badge__c);

           
    }   
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Eric_HDCEric_HDC
I uninstalled the offending package, which took care of the issue.

All Answers

Subhani_SFDCSubhani_SFDC

Hi Eric,

 

It's clearly mentioned that there is a validation rule which is not allowing to move the code to production instance. It seems like you need to change the other test classes information for allowing to validate the code.

 

Please check the follow link also which gives some good understanding about your question.

 

http://boards.developerforce.com/t5/Apex-Code-Development/Test-case-failing-on-validation-rule-that-should-pass/td-p/262815

 

http://boards.developerforce.com/t5/Apex-Code-Development/CANNOT-INSERT-UPDATE-ACTIVATE-ENTITY/td-p/262489

 

http://boards.developerforce.com/t5/forums/forumtopicprintpage/board-id/apex/message-id/85395/print-single-message/false/page/1

 

Please mark my answer as a solution if this works for you. If it was helpful so it is available to others as a proper solution

Thanks
Subhani
Salesforce Certified Developer
www.mydbsync.com

 

 

Eric_HDCEric_HDC

The validation rule and error are within the AvoidWebleadDuplicates_Test class I posted, but I don't see how the other 2 classes are violating this test class.  That is the reason I'm posting my question.

 

I've gone through the other test classes and made sure the contacts created had an email address and lead source, so I don't understand why these wouldn't pass the validation.  What am I missing?

Eric_HDCEric_HDC
I uninstalled the offending package, which took care of the issue.
This was selected as the best answer