• Eric_HDC
  • NEWBIE
  • 100 Points
  • Member since 2013

  • Chatter
    Feed
  • 4
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 8
    Replies

I am getting an error on one of the system assertEquals in my test class, however when I check the debug log the assert appears to have passed. I've included the code snippets from my controller class, test class, and the debug log.

 

Test Class Lines:

String testDebugSoqll = crc.debugSoqll;
System.assertEquals('select firstname, lastname, employer__c, state, owner.name from lead where isconverted = false order by lastname asc limit 20', testDebugSoqll);

 

Controller Lines:

//some other code here

public String sortDirl {
get { if (sortDirl == null) { sortDirl = 'asc'; } return sortDirl; }
set;
}

// the current lead field to sort by - defaults to last name
public String sortFieldl {
get { if (sortFieldl == null) { sortFieldl = 'lastName'; } return sortFieldl; }
set;
}

public String debugSoqll {

get { return soqll + ' order by ' + sortFieldl + ' ' + sortDirl + ' limit 20'; }
set;
}

public CrossReferenceController() {
soqll = 'select firstname, lastname, employer__c, state, owner.name from lead where isconverted = false';

//code continues

 

Debug Log Exception Details:

12:19:19.444 (6444732000)|EXCEPTION_THROWN|[56]|System.AssertException: Assertion Failed:

Expected: select firstname, lastname, employer__c, state, owner.name from lead where isconverted = false order by lastname asc limit 20,
Actual: select firstname, lastname, employer__c, state, owner.name from lead where isconverted = false order by lastName asc limit 20

 

I would greatly appreciate any help. I cannot figure out why the assertion is failing since from what I see, they seem to be exactly the same. Let me know if you need me to post the entire controller class, test class, and visualforce page.

 

Thank you!

 

Sam

I have a test class in sandbox which has passed, but when I take it into Production I get the below error, I don't understand what the problem is when its passed in Sandbox? 

 

TestCopyOpportunityProductFields.testCopy() Class 47 1

Failure Message: "System.AssertException: Assertion Failed: Expected: 2013-10-22 00:00:00, Actual: null", Failure Stack Trace: "Class.TestCopyOpportunityProductFields.testCopy: line 47, column 1"

 

I have copied in my test class below. 

 

@isTest(SeeAllData=true)
public class TestCopyOpportunityProductFields    {
    static testMethod void testCopy() {
    
        Product2 p = new Product2();
        p.Name = 'SFDC99 Rocks';
        p.Deferral_Percentage__c = 0;
        insert p;
        
        Pricebook2 pb = [SELECT Id FROM Pricebook2 where isStandard = true];
        PricebookEntry pbe = new PricebookEntry();
        pbe.Product2Id   = p.Id;
        pbe.UnitPrice    = 99;
        pbe.Pricebook2Id = pb.Id;
        pbe.IsActive     = true;
        insert pbe;
        
        Opportunity o = new Opportunity();
        o.CloseDate = Date.today() + 30;
        o.StageName = 'New';
        o.Name      = 'Follow me in Twitter';
        insert o;
        
        OpportunityLineItem oli = new OpportunityLineItem();
        oli.OpportunityId    = o.Id;
        oli.PricebookEntryId = pbe.Id;
        oli.Quantity         = 99;
        oli.UnitPrice        = 1;
        oli.Start_Date__c    = Date.today();
        oli.Duration__c      = 5;
        insert oli;
        
        Quote q = new Quote();
        q.OpportunityId = o.Id;
        q.Name          = '@dvdkliu';
        q.Pricebook2Id  = pb.Id;
        insert q;
        
        QuoteLineItem qli = new QuoteLineItem();
        qli.PricebookEntryId = pbe.Id;
        qli.QuoteId          = q.Id;
        qli.Quantity         = 99;
        qli.UnitPrice        = 1;
        insert qli;
        
        List<QuoteLineItem> qlis = [SELECT Id, Start_Date__c, Duration__c FROM QuoteLineItem WHERE Id = :qli.Id];
        System.assertEquals(oli.Start_Date__c, qlis[0].Start_Date__c);
        System.assertEquals(oli.Duration__c, qlis[0].Duration__c);
        
    }
}

 

 

Also, my trigger is saying this. 

 

CopyOpportunityProductFields       Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

 

trigger CopyOpportunityProductFields on QuoteLineItem (after insert) {

    // Query quote line items
    List<QuoteLineItem> qlis = [SELECT Id, PricebookEntry.Product2Id, Quote.OpportunityId FROM QuoteLineItem WHERE Id IN :Trigger.new];

    // Query opp line items
    Map<Id, OpportunityLineItem> productIdToOli = new Map<Id, OpportunityLineItem>();
    List<OpportunityLineItem> olis = [SELECT Id, PricebookEntry.Product2Id, Start_Date__c, Duration__c, End_Date__c FROM OpportunityLineItem WHERE OpportunityId = :qlis[0].Quote.OpportunityId];
    if (olis != null) {
        for (OpportunityLineItem oli : olis) {
            productIdToOli.put(oli.PricebookEntry.Product2Id, oli);
        }
    }

    // Iterate across quote line items
    for (QuoteLineItem qli : qlis) {
        OpportunityLineItem oli = productIdToOli.get(qli.PricebookEntry.Product2Id);   
        if (oli != null) {
            if (oli.Start_Date__c != null) { qli.Start_Date__c = oli.Start_Date__c; }
            if (oli.Duration__c   != null) { qli.Duration__c   = oli.Duration__c ;  }
        }
    }
    
    // Update 
    update qlis;
}

Hi Friends,

 

I have requirement to allow the user to save the contact  record only when the Date__c  field in Account record is in future.

 

How to achieve this.Pls help.

 

Thanks,

 

Vijay 

I have a Web2Lead form and on the SFDC side I have a trigger that prevents duplicate leads from being inserted (based on email address, lead source, and date).  In SFDC, when I create a duplicate, the addError message shows up and it's all good.

 

On the website (Web2Lead form), the duplicate submitted is not inserted (i.e. the trigger works fine), but the error message from the addError is displayed on the web page!

 

Is there a way I can force-redirect the web user to the regular thankyou page, or does that have to be done by the web developer in charge of the the landing page?

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);

           
    }   
}

 

 

I am getting an error on one of the system assertEquals in my test class, however when I check the debug log the assert appears to have passed. I've included the code snippets from my controller class, test class, and the debug log.

 

Test Class Lines:

String testDebugSoqll = crc.debugSoqll;
System.assertEquals('select firstname, lastname, employer__c, state, owner.name from lead where isconverted = false order by lastname asc limit 20', testDebugSoqll);

 

Controller Lines:

//some other code here

public String sortDirl {
get { if (sortDirl == null) { sortDirl = 'asc'; } return sortDirl; }
set;
}

// the current lead field to sort by - defaults to last name
public String sortFieldl {
get { if (sortFieldl == null) { sortFieldl = 'lastName'; } return sortFieldl; }
set;
}

public String debugSoqll {

get { return soqll + ' order by ' + sortFieldl + ' ' + sortDirl + ' limit 20'; }
set;
}

public CrossReferenceController() {
soqll = 'select firstname, lastname, employer__c, state, owner.name from lead where isconverted = false';

//code continues

 

Debug Log Exception Details:

12:19:19.444 (6444732000)|EXCEPTION_THROWN|[56]|System.AssertException: Assertion Failed:

Expected: select firstname, lastname, employer__c, state, owner.name from lead where isconverted = false order by lastname asc limit 20,
Actual: select firstname, lastname, employer__c, state, owner.name from lead where isconverted = false order by lastName asc limit 20

 

I would greatly appreciate any help. I cannot figure out why the assertion is failing since from what I see, they seem to be exactly the same. Let me know if you need me to post the entire controller class, test class, and visualforce page.

 

Thank you!

 

Sam

I have a Web2Lead form and on the SFDC side I have a trigger that prevents duplicate leads from being inserted (based on email address, lead source, and date).  In SFDC, when I create a duplicate, the addError message shows up and it's all good.

 

On the website (Web2Lead form), the duplicate submitted is not inserted (i.e. the trigger works fine), but the error message from the addError is displayed on the web page!

 

Is there a way I can force-redirect the web user to the regular thankyou page, or does that have to be done by the web developer in charge of the the landing page?

I have a test class in sandbox which has passed, but when I take it into Production I get the below error, I don't understand what the problem is when its passed in Sandbox? 

 

TestCopyOpportunityProductFields.testCopy() Class 47 1

Failure Message: "System.AssertException: Assertion Failed: Expected: 2013-10-22 00:00:00, Actual: null", Failure Stack Trace: "Class.TestCopyOpportunityProductFields.testCopy: line 47, column 1"

 

I have copied in my test class below. 

 

@isTest(SeeAllData=true)
public class TestCopyOpportunityProductFields    {
    static testMethod void testCopy() {
    
        Product2 p = new Product2();
        p.Name = 'SFDC99 Rocks';
        p.Deferral_Percentage__c = 0;
        insert p;
        
        Pricebook2 pb = [SELECT Id FROM Pricebook2 where isStandard = true];
        PricebookEntry pbe = new PricebookEntry();
        pbe.Product2Id   = p.Id;
        pbe.UnitPrice    = 99;
        pbe.Pricebook2Id = pb.Id;
        pbe.IsActive     = true;
        insert pbe;
        
        Opportunity o = new Opportunity();
        o.CloseDate = Date.today() + 30;
        o.StageName = 'New';
        o.Name      = 'Follow me in Twitter';
        insert o;
        
        OpportunityLineItem oli = new OpportunityLineItem();
        oli.OpportunityId    = o.Id;
        oli.PricebookEntryId = pbe.Id;
        oli.Quantity         = 99;
        oli.UnitPrice        = 1;
        oli.Start_Date__c    = Date.today();
        oli.Duration__c      = 5;
        insert oli;
        
        Quote q = new Quote();
        q.OpportunityId = o.Id;
        q.Name          = '@dvdkliu';
        q.Pricebook2Id  = pb.Id;
        insert q;
        
        QuoteLineItem qli = new QuoteLineItem();
        qli.PricebookEntryId = pbe.Id;
        qli.QuoteId          = q.Id;
        qli.Quantity         = 99;
        qli.UnitPrice        = 1;
        insert qli;
        
        List<QuoteLineItem> qlis = [SELECT Id, Start_Date__c, Duration__c FROM QuoteLineItem WHERE Id = :qli.Id];
        System.assertEquals(oli.Start_Date__c, qlis[0].Start_Date__c);
        System.assertEquals(oli.Duration__c, qlis[0].Duration__c);
        
    }
}

 

 

Also, my trigger is saying this. 

 

CopyOpportunityProductFields       Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

 

trigger CopyOpportunityProductFields on QuoteLineItem (after insert) {

    // Query quote line items
    List<QuoteLineItem> qlis = [SELECT Id, PricebookEntry.Product2Id, Quote.OpportunityId FROM QuoteLineItem WHERE Id IN :Trigger.new];

    // Query opp line items
    Map<Id, OpportunityLineItem> productIdToOli = new Map<Id, OpportunityLineItem>();
    List<OpportunityLineItem> olis = [SELECT Id, PricebookEntry.Product2Id, Start_Date__c, Duration__c, End_Date__c FROM OpportunityLineItem WHERE OpportunityId = :qlis[0].Quote.OpportunityId];
    if (olis != null) {
        for (OpportunityLineItem oli : olis) {
            productIdToOli.put(oli.PricebookEntry.Product2Id, oli);
        }
    }

    // Iterate across quote line items
    for (QuoteLineItem qli : qlis) {
        OpportunityLineItem oli = productIdToOli.get(qli.PricebookEntry.Product2Id);   
        if (oli != null) {
            if (oli.Start_Date__c != null) { qli.Start_Date__c = oli.Start_Date__c; }
            if (oli.Duration__c   != null) { qli.Duration__c   = oli.Duration__c ;  }
        }
    }
    
    // Update 
    update qlis;
}

I'm stumped!

 

The scenario: 

  • Object "ZenDesk Ticket" record is created, with a lookup field to an account, and another lookup field to a contact. 
  • The contact record didn't exist until the ZenDesk connector created the ticket record in SF, at which point it also creates the contact
  • ZenDesk should create the contact with the ticket's related Account also being the contact's related account. Instead, the contact is created as an orphan.
  • Not sure if this is relevant, bu the ZenDesk objects/fields/etc are part of a managed package on our org. 

 

 

Trigger goal:

  • Upon creation of ticket and contact, to identify the account related to the ticket, and update the related contact to also relate to this account. 

 

Method from my test class:

static testmethod void contact_with_no_account(){
    	
    Account testAccount1 = new Account(Name = 'test account',
                                       	  Type = 'Customer',
                                          Country__c = 'Albania');
        
    insert testAccount1;
                                          
    Contact testContact1 = new Contact(FirstName = 'Test',
                                           LastName = 'Contact',                                          
                                       	   City__c = 'Test city');
        
    insert testContact1;
        
    test.startTest();    
    Zendesk__Zendesk_Ticket__c testTicket1 = new Zendesk__Zendesk_Ticket__c(Zendesk__Organization__c = testAccount1.ID,
                                                                            	Zendesk__Requester__c = testContact1.ID);
    insert testTicket1;
    test.stopTest();
     
    System.assertEquals(testContact1.AccountId, testAccount1.ID);
}

 

Pretty basic - I make an account, an orphaned contact, and a ticket that relates to both of them, and insert them all. That should set off this trigger: 

 

(partial) Trigger code: 

 

trigger Zendesk_Ticket_Trigger on Zendesk__Zendesk_Ticket__c (after insert) {

    
    for (Zendesk__Zendesk_Ticket__c ticket1: [SELECT Id, Zendesk__Organization__c, Zendesk__Requester__r.AccountID FROM Zendesk__Zendesk_Ticket__c WHERE Id IN :Trigger.new
                                             AND Zendesk__Requester__r.AccountID = NULL]){
                                                                                         
        ticket1.Zendesk__Requester__r.AccountID = ticket1.Zendesk__Organization__c;                                                                                                 
        update ticket1.Zendesk__Requester__r;                                                                                               
    }

 

 

I wonder if my SOQL query is causing the problem by selecting only the tickets whose related Contact's account ID is NULL - can I fairly assume that to be true when a contact is inserted without an account specified?

 

 

The problem:

 

My system.assertEquals statement is returning the error, because the "requester" contact's account ID is null. Clearly it isn't updating after being assigned ticket1.Organization__c's ID. 

 

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);

           
    }   
}

 

 

Hello guys, i have two issues, exist, some alternative to test createdDate, and datetime.now() as a parameter.
Thx
Regards;

Matheus

Hi Friends,

 

I have requirement to allow the user to save the contact  record only when the Date__c  field in Account record is in future.

 

How to achieve this.Pls help.

 

Thanks,

 

Vijay