1

We have a custom object calls Account Contact Roles that assigns role to contact within an account. Once the record is inserted, there is a WF rule to update a field on the contact record calls "Roles__c" with following formula:

IF ( INCLUDES ( Roles__c , "Primary Contact" ), "Primary Contact: ",null ) & 

IF ( INCLUDES ( Roles__c , "Key Contact" ), "Key Contact: ",null ) &

IF ( INCLUDES ( Roles__c , "Billing Contact" ), "Billing Contact: ",null ) & 

IF ( INCLUDES ( Roles__c , "Sponsor Contact" ), "Sponsor Contact: ",null )

What we want to do is the clear out the field value when the Account Contact Roles record is deleted. The Apex code works in the Sandbox environment but need to test it before deploying. The test code is below:

@isTest

public class EmptyContactRole
{

 static testMethod void validateEmptyContactRole()

 {

  //insert records

    Account a1 = new Account(name = 'test1', Account_Type__c='non-Member', Industry='Banking');

    insert a1;

    Contact c1 = new Contact(lastname = 'test', Nickname__c='test');

    insert c1;

    Account_Contact_Role__c R = new Account_Contact_Role__c (Account__c= a1.id, Contact__c = c1.id, Roles__c = 'Primary Contact')

 //Insert insert R

 ;

c1 = [SELECT Id, Roles__c FROM Contact WHERE Id=:c1.Id];

System.assertEquals ('Primary Contact:',c1.Roles__c);

 //Delete Account Contact Role delete R

 delete R;

c1 = [SELECT Id, Roles__c FROM Contact WHERE Id=:c1.Id];

      System.assertEquals ('',c1.Roles__c);

}
}

Error Message System.AssertException: Assertion Failed: Expected: Primary Contact:, Actual: null Stack Trace Class.EmptyContactRole.validateEmptyContactRole: line 15, column 1

Thanks for the help!

3
  • You will need to clean up your code formatting as it's hard to read if the record is actually being inserted or of that line is commented out.
    – greenstork
    Commented Oct 3, 2014 at 16:59
  • Any particular reason you're using a custom object when Contact Roles are a feature that the platform provides out of the box? AccountContactRole
    – Mark Pond
    Commented Oct 3, 2014 at 16:59
  • We want it to show up on the contact record as well and want a bit more functionaries.
    – Shawn
    Commented Oct 3, 2014 at 17:06

1 Answer 1

3

When testing workflow rules, you will need to wrap your insert DML in a startTest() and stopTest() method to ensure that the workflow rule has had time to finish processing prior to querying for the record. Since you can only have one startTest() and stopTest() method per unit test, you will likely need to breakout the delete scenario into a separate test method:

Test.startTest();
insert R;
Test.stopTest();

c1 = [SELECT Id, Roles__c FROM Contact WHERE Id=:c1.Id];
...
4
  • Test.startTest(); insert R; Test.stopTest(); c1 = [SELECT Id, Roles__c FROM Contact WHERE Id=:c1.Id]; System.assertEquals ('Primary Contact:',c1.Roles__c); Delete R; c1 = [SELECT Id, Roles__c FROM Contact WHERE Id=:c1.Id]; System.assertEquals ('null',c1.Roles__c); } } but got this error Error Message System.AssertException: Assertion Failed: Expected: null, Actual: null Stack Trace Class.EmptyContactRole.validateEmptyContactRole: line 19, column 1
    – Shawn
    Commented Oct 3, 2014 at 17:15
  • Take null out of quotes, you are looking for the string null and not the value null
    – greenstork
    Commented Oct 3, 2014 at 17:19
  • Did as you suggested and got this error System.AssertException: Assertion Failed: Expected: , Actual: null
    – Shawn
    Commented Oct 3, 2014 at 17:33
  • It should look like this, sorry I wasn't more clear: System.assertEquals (null,c1.Roles__c);
    – greenstork
    Commented Oct 3, 2014 at 17:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .