I am writing my first trigger & class. I need a field to sync between 2 objects: Account (parent) & Opportunity (child). The field is a picklist named "Brand__c" on the Account tab, and "Subtype__c" on the Opportunity tab. I have created a trigger on each object. The triggers work perfectly when I test them in the sandbox, but when I try to run the test class, I'm getting the error "System.AssertException: Assertion Failed: Expected: InnLink, Actual: InnVite". So I don't think my triggers are working in the test class. Any help would be much appreciated. Also, my code coverage is 75% on Trigger 3 if I take the "update" part out of my test class, but it's 0% when I add that piece back in. Any help would be much appreciated!
trigger Trigger2 on Opportunity (after update){
if(checkRecursive.runOnce())
{
for(Opportunity opp : Trigger.New)
{
//Trigger Action Logic
List<Account> accList = [Select Brand__c from Account where id =: opp.AccountId];
for(Account acc : accList)
{
acc.Brand__c = opp.Subtype__c;
}
update accList;
}
}
}
trigger Trigger3 on Account (after insert, after update)
{
if(checkRecursive.runOnce())
{
for(Account acc : Trigger.New)
{
//Trigger Action Logic
List<Opportunity> listOpp = [Select Subtype__c from Opportunity where AccountId =: acc.id];
for(Opportunity opp : listOpp)
{
opp.Subtype__c = acc.Brand__c;
}
update listOpp;
}
}
}
@isTest public class TestOpportunityUpdate{
static testmethod void insertOpportunity() {
Account a = new Account();
a.Name = 'Mexico Villa Resort';
a.Brand__c = 'InnVite';
a.Lead_Source__c = 'AAHOA';
a.Lead_source_info__c = 'test';
a.Property_Segment__c = 'Unknown';
a.Website = 'www.yahoo.com';
insert a;
Opportunity o1 = new Opportunity(AccountId = a.Id, Name = 'Test123', Subtype__c = 'Chain', StageName = 'Negotiation/Review', CloseDate = system.today());
insert o1;
// update the Opportunity SubType.
o1.Subtype__c = 'InnLink';
update( o1 );
//check to see if Opportunity field updated.
o1 = [SELECT Id, Subtype__c FROM Opportunity
WHERE Id = :o1.Id];
System.assertEquals('InnLink', o1.Subtype__c);
//check to see if Account field updated.
a = [SELECT Id, Brand__c FROM Account
WHERE Id = :a.Id];
System.assertEquals('InnLink', a.Brand__c);
// update the Account SubType.
a.Brand__c = 'PMS';
update( a );
//check to see if Account field updated.
a = [SELECT Id, Brand__c FROM Account
WHERE Id = :a.Id];
System.assertEquals('PMS', a.Brand__c);
//check to see if Opportunity field updated.
o1 = [SELECT Id, Subtype__c FROM Opportunity
WHERE Id = :o1.Id];
System.assertEquals('PMS', o1.Subtype__c);
}
}