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
Michael Webb 5Michael Webb 5 

Need urgent help writing a test class

trigger PropOwned on Contact (after update) {
for (Contact c : Trigger.new){
McLabs2__Ownership__c ownNew= new McLabs2__Ownership__c();
Contact oldContact = Trigger.oldMap.get(c.id);
if (c.One_Prop_Owned__c != oldContact.One_Prop_Owned__c && c.One_Prop_Owned__c != null){
ownNew.McLabs2__Contact__c = c.id;
ownNew.McLabs2__Property__c = c.One_Prop_Owned__c;
insert ownNew;
}
}
}

This is the trigger, I need to move it into production. I was able to write a test class that created a new ownership, but then I realized that was not correct.  I have tried a million different things and none have worked.  This was my original, that obviously didn't work I realize it is because I need to create a contact record and then update it, just don't know how.

Thank you very much in advance.

@isTest
public class TestOwnership {
    static testMethod void createOwnership() {
     McLabs2__Ownership__c ownNew= new McLabs2__Ownership__c();
     ownNew.McLabs2__Contact__c = 'Michael Webb';
     ownNew.McLabs2__Property__c = '131 West 33rd Street';
     insert ownNew;
     }
    }

Thanks again!
Best Answer chosen by Michael Webb 5
Balaji BondarBalaji Bondar
Hi Michael,

You triggering point is contact update.Please try below code for test class and update it if needed:
@isTest
public class TestOwnership {
    static testMethod void createOwnership() {
		Account a =new Account(name='xyz');
		insert a; 

		McLabs2__Ownership__c ownNew= new McLabs2__Ownership__c();
		ownNew.McLabs2__Property__c = '131 West 33rd Street';
		insert ownNew;

		Contact newContact = new Contact(lastName='test1',AccountId=a.id);
		insert newContact;

		McLabs2__Ownership__c ownNew1= new McLabs2__Ownership__c();
		ownNew.McLabs2__Property__c = '140 West 33rd Street';
		insert ownNew1;

		Contact newContact1 = [select id,name from Contact where Id=:newContact.Id];
		newContact1.One_Prop_Owned__c = ownNew1.Id;
		update newContact1; 
	}
}

All Answers

Balaji BondarBalaji Bondar
Hi Michael,

You triggering point is contact update.Please try below code for test class and update it if needed:
@isTest
public class TestOwnership {
    static testMethod void createOwnership() {
		Account a =new Account(name='xyz');
		insert a; 

		McLabs2__Ownership__c ownNew= new McLabs2__Ownership__c();
		ownNew.McLabs2__Property__c = '131 West 33rd Street';
		insert ownNew;

		Contact newContact = new Contact(lastName='test1',AccountId=a.id);
		insert newContact;

		McLabs2__Ownership__c ownNew1= new McLabs2__Ownership__c();
		ownNew.McLabs2__Property__c = '140 West 33rd Street';
		insert ownNew1;

		Contact newContact1 = [select id,name from Contact where Id=:newContact.Id];
		newContact1.One_Prop_Owned__c = ownNew1.Id;
		update newContact1; 
	}
}
This was selected as the best answer
Michael Webb 5Michael Webb 5
When trying to run this I get the following error:

System.StringException: Invalid id: 131 West 33rd Street