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
ksnyderksnyder 

Simple test for field update trigger fails ... but works in app

Hello,

I have a trigger that updates a custom checkbox field in Accounts, "Workshop" (Workshop__c)  to "true" when a contact that is associated with the Account attends a workshop (yes, ideally, I will have an if statement to get this trigger to fire only once when the first attendance is logged ... ).

I have setup that trigger, tested it with data and it works fine:

Code:
trigger Workshop_Check on Workshop_Attendance__c (after insert) {
List<Account> updatedAccounts = new List<Account>();
Set<Id> OrgId = new Set<Id>();

for (Workshop_Attendance__c w : trigger.new)(
    OrgID.add(w.OrgID__c));
for (Account a : [SELECT id, Workshop__c FROM Account WHERE id IN :OrgID]){
    a.Workshop__c = true;
    updatedAccounts.add(a);
    }
update updatedAccounts;  
}

Now, after chasing my tail somewhat, I have the syntax seems to work for the test...however the test fails - the result when I run it shows that the expected value is false.

Code:
public class Check_Workshop {
    static testMethod void testCheck_Workshop() {


//test data - add new contact with account

Account acc = new Account(Name='Test Org');
insert acc;
Contact con = new Contact(AccountId=acc.id, Lastname = 'Doe', Firstname = 'Jane');
insert con;
Workshop_Attendance__c wkshp = new Workshop_Attendance__c(Contact_Name__c=con.id);
insert wkshp;

//confirm results to see that the workshop field is true for the corresponding account
Account[] accQuery = [SELECT Workshop__c FROM Account WHERE Id = :acc.id];
System.assertEquals(true, acc.Workshop__c);
    }
}

 Any ideas? Is it  not getting the right Account record?

Many thanks.





ksnyderksnyder
I think I solved this problem thanks to another posting.

It turns out that I needed to reload the object before testing the field values.

Code:
public class Check_Workshop {
    static testMethod void testCheck_Workshop() {


//test data - add new contact with account

Account acc = new Account(Name='Test Org');
insert acc;
Contact con = new Contact(AccountId=acc.id, Lastname = 'Doe', Firstname = 'Jane');
insert con;
Workshop_Attendance__c wkshp = new Workshop_Attendance__c(Contact_Name__c=con.id);
insert wkshp;

//confirm results to see that the workshop field is true for the corresponding account
//reload the recordset
Account acc2 = [SELECT Workshop__c FROM Account WHERE Id = :acc.id]; System.assertEquals(true, acc2.Workshop__c); } }

 



Message Edited by ksnyder on 10-07-2008 08:00 PM