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
darrelndarreln 

test class help - task trigger

Hello,
I have received great help with a trigger that displays a warning message when the "public" checkbox is not checked on the creation of a new task with a specific record type (contact object). Upon trying to deploy this, I ran in to the "No code coverage" message, and subsquently learned that a test class mus be written and the trigger must be tested before a deployment is possible. 

I am not a programmer, really not sure how I would tackle this. I am hopeful that someone here can help me with a test class for this trigger? I would really apprecaite it! Trigger follows:


trigger PublicFORBRYANTHC on Task (after insert,after Update) {
    Set<Id> ids=new Set<Id>();
    Set<Id> Conids=new Set<Id>();

    for(Task t:trigger.new){

    ids.add(t.id);
        if(t.WhoId != null && (((String)t.WhoId).substring(0,3) == '003') ) {
        conIds.add(t.WhoId);
        }
    }
    List<Contact> con=[Select id from contact where id in:conIds and RecordTypeId in (SELECT Id FROM RecordType WHERE Name = 'Bryant HC')];

    for(Task t:trigger.new){

        if(t.IsVisibleInSelfService<> true && con.size()>0 ) {

                    t.AddError('Public Checkbox must be checked!');

        }

    }

}
 
Best Answer chosen by darreln
Amit Chaudhary 8Amit Chaudhary 8
Please update your Trigger like below
trigger PublicFORBRYANTHC on Task (before insert,before Update) 
{
    Set<Id> Conids=new Set<Id>();
    for(Task t:trigger.new)
	{
        if(t.WhoId != null && ( ((String)t.WhoId).substring(0,3) == '003') ) 
		{
             conIds.add(t.WhoId);
        }
    }

	Id contRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Bryant HC').getRecordTypeId();
    Map<Id,Contact> mapCon = new Map<Id,Contact> ( [Select id from contact where id in:conIds and RecordTypeId =:contRecordTypeId ] );


    for(Task t:trigger.new)
	{
        if(t.IsVisibleInSelfService == false && mapCon.containsKey(t.WhoId) ) 
		{
			t.AddError('Public Checkbox must be checked!');
        }
    }
}

Test class like below
@isTest
public class PublicFORBRYANTHCTest
{
     @isTest Static void testForBryantHC()
	 {
         Account a = new Account(name='test'); 
		 insert a;
		 
         Contact c = new Contact(lastname='test', recordtypeId=Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Bryant HC').getRecordTypeId()); 
		 Insert c;
		 
         Task t = new task(ActivityDate=date.today(),subject='test', WhoId=c.Id, Status='Completed');
         Test.startTest();
			try{
             insert t;
			}
			Catch(Exception ee){
			}	
         Test.stopTest();

     }

}

Let us know if this will help you
 

All Answers

Nicole RebeloNicole Rebelo
Your code is not doing what you think it's doing. Per your requirement it should look like this:
trigger PublicFORBRYANTHC on Task (after insert,after Update) {

    Set<Id> Conids=new Set<Id>();
    Map<Id, Id> taskcontactmap = new Map<Id,Id>();
    for(Task t:trigger.new){
        if(t.WhoId != null && (((String)t.WhoId).substring(0,3) == '003') ) {
             conIds.add(t.WhoId);
             taskcontactmap.put(t.WhoId, t.Id);
        }
    }
    List<Contact> con=[Select id from contact where id in:conIds and RecordTypeId in (SELECT Id FROM RecordType WHERE Name = 'Bryant HC')];

Set<Id> tt = new Set<Id>();
for(Contact c: con){
    tt.add(taskcontactId.get(c.Id));
}

    for(Task t:trigger.new){

        if(t.IsVisibleInSelfService<> true && tt.contains(t.Id) ) {

                    t.AddError('Public Checkbox must be checked!');

        }

    }

}

@isTest
public class PublicFORBRYANTHCTest{
     @isTest Static void testForBryantHC(){
         Account a = new Account(name='test'); insert a;
         Contact c = new Contact(lastname='test', recordtypeId=Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Bryant HC').getRecordTypeId()); Insert c;
         Task t = new task(ActivityDate=date.today(),subject='test', WhoId=c.Id, Status='Completed');
         
         Test.startTest();
             insert t;
         Test.stopTest();

     }

}

This is not tested of course so let me know if you get any errors. you may need to adjust the test data to include any required fields.
darrelndarreln
Thank you Nicole, will test and advise! 
darrelndarreln
Nicole, get a compile error: Error: Compile Error: Variable does not exist: taskcontactId on line 15. 
Amit Chaudhary 8Amit Chaudhary 8
Please update your Trigger like below
trigger PublicFORBRYANTHC on Task (before insert,before Update) 
{
    Set<Id> Conids=new Set<Id>();
    for(Task t:trigger.new)
	{
        if(t.WhoId != null && ( ((String)t.WhoId).substring(0,3) == '003') ) 
		{
             conIds.add(t.WhoId);
        }
    }

	Id contRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Bryant HC').getRecordTypeId();
    Map<Id,Contact> mapCon = new Map<Id,Contact> ( [Select id from contact where id in:conIds and RecordTypeId =:contRecordTypeId ] );


    for(Task t:trigger.new)
	{
        if(t.IsVisibleInSelfService == false && mapCon.containsKey(t.WhoId) ) 
		{
			t.AddError('Public Checkbox must be checked!');
        }
    }
}

Test class like below
@isTest
public class PublicFORBRYANTHCTest
{
     @isTest Static void testForBryantHC()
	 {
         Account a = new Account(name='test'); 
		 insert a;
		 
         Contact c = new Contact(lastname='test', recordtypeId=Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Bryant HC').getRecordTypeId()); 
		 Insert c;
		 
         Task t = new task(ActivityDate=date.today(),subject='test', WhoId=c.Id, Status='Completed');
         Test.startTest();
			try{
             insert t;
			}
			Catch(Exception ee){
			}	
         Test.stopTest();

     }

}

Let us know if this will help you
 
This was selected as the best answer
darrelndarreln
Amit, this worked perfectly. I was able to test in sandbox, and then test & deploy in prod. Works great, I really appreacite your and Nicole's help!