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
shakila Gshakila G 

Test Class Code Overeager ?

Hi  All,

Am getting code coverage error for my test class? Can anyone help me to get code coverage?

Trigger : 
trigger taskRestriction on task (Before insert,After update,Before Update) {
  
    list<Contact> clist =new list<Contact>();
    
    String userid=UserInfo.getUserId();
    
    String ProfileID=userinfo.getProfileId();
  
  
    
        for (Task t : Trigger.new) {
       
        if (t.WhoId !=Null  && userinfo.getProfileId()=='00e90000000O4VsAAK' )
        {
                
          clist= [select ID, ownerId ,Account_name__C, Account.Lock_record__C from Contact where 
          ID=:t.WhoId and Account.Lock_record__C!=True and  OwnerId !=:UserInfo.getUserId()]; 
                
            }
            
            IF(Clist.size()>0)
            {
            T.adderror('This contact is assigned to other user, Kindly Contact Sir/Madam to Change the Ownership');
            }
            }                   
}

Test Class:

@isTest

public class TaskBlockedTest
{
    @isTest
    public static void testLeadInsert ()
    
    {
             Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
             
            User u = new User(Alias = 'Test', Email='Test@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='shakila@gmail.com');
            
            insert u;
            
            Lead objLead = new Lead (LastName='Test2Dup',E_Mail_Additional__c='test3@duptest.com', Email='test2@duptest.com',LeadSource='Click India',
       MobilePhone='7452638196',Whatsapp_Mobile__c='7452638194' ,Mobile_Additional__c='7452638193' ,Phone='7452638192',
       Phone_Additional__c='7452638191', Status='Open',Customer_Type__c='Shop',Company='Test',Date__c= Date.newInstance(2018, 03, 23));
        
            insert objLead; 
            
            Account a = new Account(Name = 'ApprovalTest' ,Lock_record__C=FALSE ,ownerid=u.ID);
           insert a; 
           
           Contact con = new COntact();
            con.email='test@fmail.co';
            con.ACCOUNTID=a.ID;
            con.ownerid=a.ownerid;
            con.lastname='test' ;
            
            insert con;
    
     System.runAs(u) 
        { 
            Task testTTask= new Task(Subject = 'EventTest',  Description = 'Description',WhoId= con.Id ,
             ownerid=con.ownerid);
             Insert testTTask;
             
             
        Test.startTest();
        testTTask= new task(Subject = 'EventTest',  Description = 'Description',WhoId= con.Id ,
        ownerid=con.ownerid);
        Database.SaveResult result = Database.insert(testTTask, false);
           Test.stopTest();
           }
        
    }
    
    @isTest
    public static void testLeadUpdate ()
    {
        
             Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
             
            User u = new User(Alias = 'Test', Email='Test@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='shakila@gmail.com');
            
            insert u;
            
                Lead objLead = new Lead (LastName='Test2Dup',E_Mail_Additional__c='test3@duptest.com', Email='test2@duptest.com',LeadSource='Click India',
            MobilePhone='7452638196',Whatsapp_Mobile__c='7452638194' ,Mobile_Additional__c='7452638193' ,Phone='7452638192',
           Phone_Additional__c='7452638191', Status='Open',Customer_Type__c='Shop',Company='Test',Date__c= Date.newInstance(2018, 03, 23));
        
            insert objLead; 
            Account a = new Account(Name = 'ApprovalTest' ,Lock_record__C=FALSE ,ownerid=u.ID);
           insert a; 
           
           Contact con = new COntact();
            con.email='test@fmail.co';
            con.ACCOUNTID=a.ID;
            con.ownerid=a.ownerid;
            con.lastname='test' ;
            
            insert con;
         
           
           
        System.runAs(u) 
        { 
    
            Task testTTask= new Task(Subject = 'EventTest',  Description = 'Description',WhoId= con.Id ,
             ownerid=con.ownerid);
             Insert testTTask;
             
               List<Account> listact=New List<Account>{[select OwnerID from Account Where ownerID=:u.ID ]};
             
         List<task> taskstoupdate = New List<task>{[select Time_New__c, Task.Account.Name from Task where  ownerId =:u.id limit 1]};
             
              Task testTTask2= new Task(Subject = 'EventTest',  Description = 'Description',WhoId= con.Id ,
             ownerid=con.ownerid);
             
             Insert testTTask2;
             
             Task tk =[select Subject from Task where ID=:testTTask2.ID];
             
             tk.Subject ='Test';
             
             
             Update tk;
               
       
              
            
   
        
        }
         
        
       
    }
}
 
Best Answer chosen by shakila G
Amit Chaudhary 8Amit Chaudhary 8
I was forget to remove = sing form query

ID in =:setContId AND

Update your code like below
trigger taskRestriction on task (Before insert,After update,Before Update) {
  
	list<Contact> clist =new list<Contact>();
    String userid=UserInfo.getUserId();
    
    String ProfileID=userinfo.getProfileId();
	Profile prod = [Select Name,id from Profile where id=:ProfileID ];
  
	Set<Id> setContId = new Set<Id>();
	
	for (Task t : Trigger.new) {
		if (t.WhoId !=Null  && prod.Name=='Standard User')
		{
			setContId.add(t.WhoId);
		}
	}	
	
	String strUserId = UserInfo.getUserId();
	Map<Id,Contact> mapContact = new Map<Id,Contact>( [ select ID, ownerId ,Account_name__C, Account.Lock_record__C 
														FROM Contact  
														WHERE ID in :setContId AND 
														Account.Lock_record__C!=True AND  
														OwnerId !=:strUserId ] );
	for (Task t : Trigger.new) 
	{
		if (t.WhoId !=Null  && prod.Name=='Standard User')
		{
			IF( mapContact.containsKey(t.WhoId) )
			{
				T.adderror('This contact is assigned to other user, Kindly Contact Sir/Madam to Change the Ownership');
			}
		}
	}               
}

Let us know if this will help you

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Hi
I found two issue in your Trigger code
1) Issue is coming because of hardcoded ID in Trigger .
2) SOQL inside the for loop

Update your trigger like below
trigger taskRestriction on task (Before insert,After update,Before Update) {
  
	list<Contact> clist =new list<Contact>();
    String userid=UserInfo.getUserId();
    
    String ProfileID=userinfo.getProfileId();
	Profile prod = [Select Name,id from Profile where id=:ProfileID ];
  
	Set<Id> setContId = new Set<Id>();
	
	for (Task t : Trigger.new) {
		if (t.WhoId !=Null  && prod.Name=='Standard User')
		{
			setContId.add(t.WhoId);
		}
	}	
	
	Map<Id,Contact> mapContact = new Map<Id,Contact>( [ select ID, ownerId ,Account_name__C, Account.Lock_record__C 
														FROM Contact  
														WHERE ID in =:setContId AND 
														Account.Lock_record__C!=True AND  
														OwnerId !=:UserInfo.getUserId() ] );
	for (Task t : Trigger.new) 
	{
		if (t.WhoId !=Null  && prod.Name=='Standard User')
		{
			IF( mapContact.containsKey(t.WhoId) )
			{
				T.adderror('This contact is assigned to other user, Kindly Contact Sir/Madam to Change the Ownership');
			}
		}
	}               
}
NOTE:- Please change the profile according to requirement


Update your test class like below
@isTest
public class TaskBlockedTest
{
    @isTest
    public static void testLeadUpdate()
    {
		Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
		User u = new User(Alias = 'Test', Email='Test@testorg.com', 
		EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
		LocaleSidKey='en_US', ProfileId = p.Id, 
		TimeZoneSidKey='America/Los_Angeles', UserName='shakila@gmail.com');
		insert u;
		
		Account a = new Account(Name = 'ApprovalTest' ,Lock_record__C=FALSE);
		insert a; 
	   
		Contact con = new COntact();
		con.email='test@fmail.co';
		con.ACCOUNTID=a.ID;
		con.lastname='test' ;
		insert con;
           
           
        System.runAs(u) 
        { 
			try{
				Task testTTask= new Task(Subject = 'EventTest',  Description = 'Description',WhoId= con.Id );
				Insert testTTask;
            }
			Catch(Exception ee){
			}
        }
    }
}


Let us know if this will help you
shakila Gshakila G
Hi Amit,

Thanks for the update.

Am getting following error ;

while saving ypour Trigger :

Error: Compile Error: Unexpected token '<'. at line 18 column 8
Amit Chaudhary 8Amit Chaudhary 8
Try to update your code like below
trigger taskRestriction on task (Before insert,After update,Before Update) {
  
	list<Contact> clist =new list<Contact>();
    String userid=UserInfo.getUserId();
    
    String ProfileID=userinfo.getProfileId();
	Profile prod = [Select Name,id from Profile where id=:ProfileID ];
  
	Set<Id> setContId = new Set<Id>();
	
	for (Task t : Trigger.new) {
		if (t.WhoId !=Null  && prod.Name=='Standard User')
		{
			setContId.add(t.WhoId);
		}
	}	
	
	String strUserId = UserInfo.getUserId();
	Map<Id,Contact> mapContact = new Map<Id,Contact>( [ select ID, ownerId ,Account_name__C, Account.Lock_record__C 
														FROM Contact  
														WHERE ID in =:setContId AND 
														Account.Lock_record__C!=True AND  
														OwnerId !=:strUserId ] );
	for (Task t : Trigger.new) 
	{
		if (t.WhoId !=Null  && prod.Name=='Standard User')
		{
			IF( mapContact.containsKey(t.WhoId) )
			{
				T.adderror('This contact is assigned to other user, Kindly Contact Sir/Madam to Change the Ownership');
			}
		}
	}               
}

Let us know if this will help you
 
shakila Gshakila G
Am facing the same Issue. Error: Compile Error: Unexpected token '
Amit Chaudhary 8Amit Chaudhary 8
I was forget to remove = sing form query

ID in =:setContId AND

Update your code like below
trigger taskRestriction on task (Before insert,After update,Before Update) {
  
	list<Contact> clist =new list<Contact>();
    String userid=UserInfo.getUserId();
    
    String ProfileID=userinfo.getProfileId();
	Profile prod = [Select Name,id from Profile where id=:ProfileID ];
  
	Set<Id> setContId = new Set<Id>();
	
	for (Task t : Trigger.new) {
		if (t.WhoId !=Null  && prod.Name=='Standard User')
		{
			setContId.add(t.WhoId);
		}
	}	
	
	String strUserId = UserInfo.getUserId();
	Map<Id,Contact> mapContact = new Map<Id,Contact>( [ select ID, ownerId ,Account_name__C, Account.Lock_record__C 
														FROM Contact  
														WHERE ID in :setContId AND 
														Account.Lock_record__C!=True AND  
														OwnerId !=:strUserId ] );
	for (Task t : Trigger.new) 
	{
		if (t.WhoId !=Null  && prod.Name=='Standard User')
		{
			IF( mapContact.containsKey(t.WhoId) )
			{
				T.adderror('This contact is assigned to other user, Kindly Contact Sir/Madam to Change the Ownership');
			}
		}
	}               
}

Let us know if this will help you
This was selected as the best answer
shakila Gshakila G
Thanks, Amith.Its Working Fine