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
anvesh@force.comanvesh@force.com 

How to get trigger code coverage for this simple trigger?

When i  deployed my trigger from sanbox to production , the deployement is failed , i  found that  at least 1% code coverage should need to have in order to deploy...but this trigger have 0% code coverage?  what should i do? hw to increses code coverage for this trigger?

 

 

 

trigger UpdateEventRating on Event (before insert, before update) {
for(Event E : Trigger.new){
if(E.Rating__c=='-Excellent Meeting (committed to business within 3 weeks)'){
E.Rating_Value__c=5;
}
else
if(E.Rating__c=='-Good Meeting (Resulted in client meeting agreed to/set or booked a seminar)'){
E.Rating_Value__c=4;
}
else
if(E.Rating__c=='-Productive Meeting (Client specific illustration requested or next meeting scheduled)'){
E.Rating_Value__c=3;
}
else
if(E.Rating__c=='-Average Meeting (Agreed to continued discussions, no client specific activity)'){
E.Rating_Value__c=2;
}
else
if(E.Rating__c=='-Poor Meeting (No potential for future business, no further activity needed)'){
E.Rating_Value__c=1;
}
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Sri549Sri549

Hi Anvesh

No need of writing any apex classes just write a test class which i have provided that mean that we are inserting dummy values as per the condition and sending to production 

that test class is for trigger.

 

we will write test class for Apex classes and Apex triggers before moving it to production.

 

If you have any doubt regarding solution just ping me.

 

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Srinivas
SFDC Certified Developer

All Answers

Sri549Sri549

Hi Anvesh,

@ isTest
 public class Test_UpdateEventRating 
 {
  public static testmethod void Unittest()
  {
   Event e=new Event();
   e.ActivityDateTime=datetime.newInstance(2008, 12, 1, 12, 30, 2);
   e.DurationInMinutes=2;
   e.Rating__c='-Excellent Meeting (committed to business within 3 weeks)';
   e.Rating_Value__c='5';
    Insert e;
    
    
   Event e1=new Event();
   e1.ActivityDateTime=datetime.newInstance(2008, 12, 1, 12, 30, 2);
   e1.DurationInMinutes=2;
   e1.Rating__c='-Good Meeting (Resulted in client meeting agreed to/set or booked a seminar)';
   e1.Rating_Value__c='4';
    Insert e1;  
    
   Event e2=new Event();
   e2.ActivityDateTime=datetime.newInstance(2008, 12, 1, 12, 30, 2);
   e2.DurationInMinutes=2;
   e2.Rating__c='-Productive Meeting (Client specific illustration requested or next meeting scheduled)';
   e2.Rating_Value__c='3';
    Insert e2;
     Event e3=new Event();
   e3.ActivityDateTime=datetime.newInstance(2008, 12, 1, 12, 30, 2);
   e3.DurationInMinutes=2;
   e3.Rating__c='-Average Meeting (Agreed to continued discussions, no client specific activity)';
   e3.Rating_Value__c='2';
    Insert e3;
     Event e4=new Event();
   e4.ActivityDateTime=datetime.newInstance(2008, 12, 1, 12, 30, 2);
   e4.DurationInMinutes=2;
   e4.Rating__c='-Poor Meeting (No potential for future business, no further activity needed)';
   e4.Rating_Value__c='1';
    Insert e4; 
                          
  } 
 }

This gives you full coverage for your trigger else ping me with bit clear requirement.

 

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Srinivas
SFDC Certified Developer

 

Avidev9Avidev9

I am going to give away the code here, I expect you to use the Idea

 

@isTest
private class MyUnitTest{

  static testmethod void test1{
  	Even e = new Event(); //fill in all the required fields
    e.Rating__c ='-Excellent Meeting (committed to business within 3 weeks)';
    insert e;
    ///do others similarly
  }
}

 

sandeep@Salesforcesandeep@Salesforce

@isTest(SeeAllData = false)

private class Event_TestClass
{
static testMethod void UnitTestForException()
{
test.startTest();
Event e = new Eevent(ActivityDate = system.today(), ActivityDateTime = system.now(), DurationInMinutes = 2);
insert e ;
update e ;
test.stopTest();
}
}

 

I havce provided a structure now you can also include fields to get code coverage more.

anvesh@force.comanvesh@force.com

I  dont  have  class ,  My  requirement is  when Rating__c  changed  according to this  trigger  should fired and  Rating_value__c value is changed accordingly  on  event page(Activity sobject)....because    Rating__c   is a picklist  type.if we select  a value  from picklist ('Excellent Meeting')...then  this trigger ll need to  fire & Rating_value_c  populated as 5...like this.

 

 

so  can i  need to  write a  class?  when  executes ,... the  code execute from  test class or tigger? i  am  confused  i  have trigger  then what  is need  for to write class...sorry  for  silly question i am poor  in sfdc...pls help me

Sri549Sri549

Hi Anvesh

No need of writing any apex classes just write a test class which i have provided that mean that we are inserting dummy values as per the condition and sending to production 

that test class is for trigger.

 

we will write test class for Apex classes and Apex triggers before moving it to production.

 

If you have any doubt regarding solution just ping me.

 

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Srinivas
SFDC Certified Developer

This was selected as the best answer
anvesh@force.comanvesh@force.com

ok  got it...but  small doubt  how  did this  test  class  recognises that  this  class  is for  trigger...i mean  we didnt mentioned trigger  name  any where then hw this test class  recognises its trigger. and  can i  deploy this  test  class along with  trigger?

 

i written your test class  but  trigger code coverage area still showing 0%

Sri549Sri549
Test_UpdateEventRating--> this represents the identication of test class for trigger test indicates test class and Underscore is the sperator and name as trigger name..
anvesh@force.comanvesh@force.com

can i deploy  this class  along with trigger?

Sri549Sri549

yes you can..

John L.ax1429John L.ax1429

Sri549,

 

I have a trigger that I do not know how to develop a class to pass this from sandbox to production can you assist me with this? 

 

 

 

trigger triggerCloneNetValue on OpportunityLineItem (before insert, before Update) {
  for(OpportunityLineItem opportunityLI : Trigger.new){
        opportunityLI.Net_Value_AOP_Installation_OTC_WF__c=opportunityLI.Net_Value_AOP_Installation_OTC__c;
        opportunityLI.Net_Value_AOP_Rental_or_Lease_WF__c=opportunityLI.Net_Value_AOP_Rental_or_Lease__c;
        opportunityLI.Net_Value_AOP_MRC_WF__c=opportunityLI.Net_Value_AOP_MRC__c;
        system.debug(opportunityLI.Net_Value_AOP_Installation_OTC__c);
    }
}

 

Any help will be greatfull

 

Thanks,

 

John