+ Start a Discussion
uHaveOptionsuHaveOptions 

Test class help on trigger

I'm not that familiar create test class yet.  Hope you guys can help me with this.  Can you guys help me?
 
trigger primaryContactOwnership on MRG_Ownership__c (after insert,after update) 
{
    ownerTrigger();
    
    public void ownerTrigger(){
    List<Id> primaryOwnershipIdList = new List<Id>();
    List<Id> relatedPropertyIdList = new List<Id>();

    for(MRG_Ownership__c ownership : Trigger.New)
    {
        if(ownership.Primary_Contact__c == true && (Trigger.Old == null || Trigger.OldMap.get(ownership.Id) == null 
        || Trigger.OldMap.get(ownership.Id).Primary_Contact__c != true)) // To handle null scenario
        {
            primaryOwnershipIdList.add(ownership.Id);
            relatedPropertyIdList.add(ownership.Property__c);
            
            Property__c property= [select id, Primary_Contact__c from Property__c WHERE id =:ownership.Property__c limit 1];
            property.Primary_Contact__c=ownership.contact__c;
            update property;
        }
    }

    List<MRG_Ownership__c> relatedOwnershipList = [Select Id, Primary_Contact__c 
                                                    From MRG_Ownership__c 
                                                    Where Property__c in :relatedPropertyIdList
                                                    And Primary_Contact__c = true
                                                    And Id Not in :primaryOwnershipIdList];

    if(relatedOwnershipList.size() > 0)
    {
        for(MRG_Ownership__c ownership : relatedOwnershipList)
            ownership.Primary_Contact__c = false;

        update relatedOwnershipList;
    }
    
    
    }
}

 
Best Answer chosen by uHaveOptions
David ZhuDavid Zhu
You can modify the following simple code to cover the code. Even though it only tests AFTER Insert scenario, the code covarge should be the same as AFTER update.
You may also need to add code to do AFTER update and bulk operation and negative tests.
 
public testmethod static void testcase1()
{
	//test after insert;

	Contact c = new Contact(name = '....',field1 ='xxxx',...);
	insert c;

	property__c p = new property__c();
	p.xxxx= '...';
	insert p;
	

	MRG_Ownership__c  mo = new MRG_Ownership__c ();
	mo.xxx = '....';  //assign value to properties of mo.
	mo.yyy = '....'; 
	mo.primary_contact__c = true;
	mo.property__c = p;
	mo.contact__c = c;

	insert mo;


	system.assertequals(c.id,p.primary_contact__c.id); 

	MRG_Ownership__c  mo1 = new MRG_Ownership__c ();
	mo1.xxx = '....';  //assign value to properties of mo.
	mo1.yyy = '....'; 
	mo1.primary_contact__c = true;
	mo1.property__c = p;
	mo1.contact__c = c;
	insert mo1;

	mo = [select id,primary_contact__c from mrg_ownership__c where id :=mo.id];

	system.assertequals(false,mo.primay_contact__c);

}


 

All Answers

David ZhuDavid Zhu
You can modify the following simple code to cover the code. Even though it only tests AFTER Insert scenario, the code covarge should be the same as AFTER update.
You may also need to add code to do AFTER update and bulk operation and negative tests.
 
public testmethod static void testcase1()
{
	//test after insert;

	Contact c = new Contact(name = '....',field1 ='xxxx',...);
	insert c;

	property__c p = new property__c();
	p.xxxx= '...';
	insert p;
	

	MRG_Ownership__c  mo = new MRG_Ownership__c ();
	mo.xxx = '....';  //assign value to properties of mo.
	mo.yyy = '....'; 
	mo.primary_contact__c = true;
	mo.property__c = p;
	mo.contact__c = c;

	insert mo;


	system.assertequals(c.id,p.primary_contact__c.id); 

	MRG_Ownership__c  mo1 = new MRG_Ownership__c ();
	mo1.xxx = '....';  //assign value to properties of mo.
	mo1.yyy = '....'; 
	mo1.primary_contact__c = true;
	mo1.property__c = p;
	mo1.contact__c = c;
	insert mo1;

	mo = [select id,primary_contact__c from mrg_ownership__c where id :=mo.id];

	system.assertequals(false,mo.primay_contact__c);

}


 
This was selected as the best answer
uHaveOptionsuHaveOptions
This was very helpful. Thanks