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
brielea1984brielea1984 

Help Writing Test Class for Trigger

So I've gotten pretty good at writing triggers, by simply altering coding I've found on here and salesforce.com. So I understand how to write them, but not necessarily WHY they work. So because I'm a novice, I'm having a bit of trouble figuring out the test codes. I've read the Introduction to Apex Test Methods, but it's not quite clicking for me. I'm hoping this is a really easy trigger that someone can help me figure out how to write the test code for it? If it isn't too much work to write it for me, I can probably figure out all of the other triggers.

 

trigger AutoCreateGeneralIntake on Contact (after insert) {
    List<General_Intakes__c> GeneralIntake = new List<General_Intakes__c>();

    
    for (Contact newContact: Trigger.New) {
        if (newContact.Screening_ID__c != null) {
            GeneralIntake.add(new General_Intakes__c(
                        Name = '1',
                        Primary_Contact__c = newContact.ID,
                        Screening_ID__c = newContact.Screening_ID__c,
                        Primary_Household_ID__c = newContact.Household_ID_del__c));
        }
    }
    insert GeneralIntake;
}

 Thank you in advance for any help.

Best Answer chosen by Admin (Salesforce Developers) 
MoUsmanMoUsman

Hi brielea1984,

 

I have written a test class for you as shown below just copy and past into your org ,I believe it will work.

//Auther USman
public class TestClass {    
    /* Create New Contct Record */
    static testMethod void unitTestMethod()
    {    	
    	User mockUser = [SELECT User.Id FROM User WHERE User.Id =:UserInfo.getUserId()];
    	system.runAs(mockUser){
    		//Create an Account to Associate to the Contact
    		Account a = new Account();
	        a.Name = 'Test';
	        a.AccountNumber = 'X100';
	        insert a;
    		    		
    		//Create a Contact and A User for testing the trigger
			Contact contact = new Contact();
			contact.LastName = 'Test';
			contact.Screening_ID__c  = 'Put your Id here';
			contact.AccountId = a.Id;
			insert(contact);
			System.asserNotEquals(contact.Id , null);
    	}     
    
}

NOTE: In this test class you need to put value of the field "Screening_ID__c",If you need more clarification please let me know.

--

Thanks

Usman

 

All Answers

MoUsmanMoUsman

Hi brielea1984,

 

I have written a test class for you as shown below just copy and past into your org ,I believe it will work.

//Auther USman
public class TestClass {    
    /* Create New Contct Record */
    static testMethod void unitTestMethod()
    {    	
    	User mockUser = [SELECT User.Id FROM User WHERE User.Id =:UserInfo.getUserId()];
    	system.runAs(mockUser){
    		//Create an Account to Associate to the Contact
    		Account a = new Account();
	        a.Name = 'Test';
	        a.AccountNumber = 'X100';
	        insert a;
    		    		
    		//Create a Contact and A User for testing the trigger
			Contact contact = new Contact();
			contact.LastName = 'Test';
			contact.Screening_ID__c  = 'Put your Id here';
			contact.AccountId = a.Id;
			insert(contact);
			System.asserNotEquals(contact.Id , null);
    	}     
    
}

NOTE: In this test class you need to put value of the field "Screening_ID__c",If you need more clarification please let me know.

--

Thanks

Usman

 

This was selected as the best answer
brielea1984brielea1984

It worked! I had to add more for the parent records to the Contact record, but copied what you did for Accounts. It was so helpful to be able to see a test code for my actual coding to get an understand as to what has to happen, i.e. writing code that inserts all parent objects/required fields. Thank you so much! I really appreciate it!

MoUsmanMoUsman

Thanks brielea1984!!

brielea1984brielea1984

I was able to get 11 out of my 12 triggers covered, but it seems like Update needs something else. I tried copying what I'd done for all of the other triggers, but got 0% coverage each time. Any ideas?  Essentially all this trigger does is update the status on the Service_Plan_Indicator__c to have the same status as the Goals__c.

 

trigger updateSPPI on Goals__c (after update) {
    List < Id > GoalIds = new List < Id >();
    List < Id > SPPIIds = new List < Id >();
    for(Goals__c rem: Trigger.New) {    
        if(rem.Name != null){    
           List<Service_Plan_Indicator__c> proObj = [select p.Id,p.Client_Goal__c from Service_Plan_Indicator__c p where p.Client_Goal__c =:rem.Id];
           if(proObj.size() > 0){
               for(Service_Plan_Indicator__c pc: proObj ){
                   pc.Goal_Status__c = rem.Goal_Status__c;
                   update pc;
               }
           }          
        }      
    }
}

 

MoUsmanMoUsman

Create a record of  Goals__c,Service_Plan_Indicator__c sobjects and Client_Goal__c field should be Id of the Goals__c try to update in test method it will cover your complete code.

Note : Record should be created as per your logic written in the trigger on Goals__c.

 

--

Thanks

Usman