+ Start a Discussion
Angela SchloederAngela Schloeder 

How to get code coverage for a trigger

How can I get code coverage for this trigger?

trigger Countevent on event(after insert, after update) {
    
    public List<event> ltask1 = new List<event>();
    public id userids;
    public integer inp=0;   
    public integer inr=0;
    for(event t:Trigger.New){
        userids = t.ownerid;
      
        
    }
    ltask1 = [select id,Request_for_Meeting_Bonus__c from event where ownerid=:userids];

    
    
    for(event t:ltask1){
        if(t.Request_for_Meeting_Bonus__c==true){
            inp = inp+1;
        } else{
            inr = inr +1;
        }                
    }
    List<user> uopp = new List<user>(); 
    List<user> userop = [select id from user where id = :userids];

    for(user u: userop){
        u.Total_Requested__c = inp;
    
        uopp.add(u);  
    }
    if(uopp.size()>0){
        update uopp;
    }        
 
}
Raj VakatiRaj Vakati
@isTest
private class Countevent_test
{
    static testMethod void Counteventtest() {

    test.startTest();

	   // This code runs as the system user
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standard111111user@testorg.com');
			
			
	
    Account a = new Account (name = 'tester' );
    insert a;


    Event e = new Event (name = 'test', Request_for_Meeting_Bonus__c = true, WhatId = a.Id ,OwnerId=u.Id);  
    insert e;


    Event e1 = new Event (name = 'test1', Request_for_Meeting_Bonus__c = false, WhatId = a.Id ,OwnerId=u.Id);  
    insert e1;
	
	    test.stopTest();
		
		e1.Request_for_Meeting_Bonus__c = true ;
		update e1;
}


}

 
v varaprasadv varaprasad
Hi Angela,

Please check below sample code.
 
@isTest
public class CreateAccountTest
{
  static testMethod void testInsertAccount()
  {
  
	  Event evt = new Event(
	  Subject = 'Call',                                                                
	  StartDateTime = system.now(),  
	  EndDateTime = system.now()+1,                          
	  Ownerid = userinfo.getuserid()                                                       
	);
	insert evt;
  
  
  }
}

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com