1

I am new to apex and SF and need help in writing a test class for my trigger.

This trigger runs every time a Calender event is created and updates the scheduled date and status on Opportunity. The trigger is working fine in Sandbox.

I created the given test class and it runs successfully but in dev-console, the test coverage displayed is 0%. This is my first time writing a test class and I have tried to research as much as I can before asking this question. Please be considerate and help, I am in urgent need. Thanks a lot in advance.

trigger UpdOpportunityStagefromEvent on Event (after insert , after update){

    Id optyId;

    for (Event evntObj: Trigger.new){
        optyId = evntObj.WhatId;
    }

    Event evntObj = [select Id, StartDateTime, LastModifiedDate from Event where WhatId =:optyId order by LastModifiedDate DESC LIMIT 1];

    date d = Date.newInstance(evntObj.StartDateTime.year(),evntObj.StartDateTime.month(),evntObj.StartDateTime.day());
    opportunity optyObj = [select Id from opportunity where Id = :optyId LIMIT 1];
    optyObj.StageName='Work Order Scheduled';
    optyObj.Scheduled_Work_Date__c = d;
    update optyObj;
}

Test Class:


@isTest private class TestUpdOppfromEventTrigger {

static testMethod void myUnitTest() {
Event e = new Event();
e.IsAllDayEvent = true;
e.ActivityDate = Date.today();

Opportunity optyObj = new Opportunity();
optyObj.StageName='Work Order Scheduled';
optyObj.Scheduled_Work_Date__c = Date.today();
optyObj.id = '006L0000005P1YW';
insert e;
update optyObj;

}

}

I made the changes given above and I am getting 66% coverage now. The last 3 lines in the trigger are not getting covered. Any ideas will be highly appreciated. Thanks.

  • 2
    SFNoob, welcome to SFSE. I think you'l find that you get pretty rapid response on SFSE without having to use words like urgent which have a way of irritating the community. Just take a look at other posts where the response time to a well-formulated question can typically be in less than 1 hour. – cropredy Sep 29 '14 at 23:30
  • 1
    Yes, it indeed looks like that. Will keep in mind in future. Thanks. – SFNoob Sep 29 '14 at 23:38
  • I would also comment that if you are just going to update the Opportunity, you don't need to query for it - you can just set the Id in the constructor, and update it directly – BritishBoyinDC Sep 30 '14 at 2:26
1

Your trigger is on Event, but your test inserts an Opportunity. You need to insert an Event and not an Opportunity.

| improve this answer | |
1

Also - you have an Opportunity Id hard-coded in the test - but for the most part, when you run a test, it is isolated from the live data, so you need to create a new Opportunity in the test, insert that record, and then link the Event record to it.

The test still won't work as written - it should look more like this I think

@isTest private class TestUpdOppfromEventTrigger {

static testMethod void myUnitTest() {

    Opportunity optyObj = new Opportunity();
    optyObj.Name = 'Test Record';
    optyObj.StageName='Work Order Scheduled';
    optyObj.Scheduled_Work_Date__c = Date.today();
    optyObj.CloseDate = system.today();

    insert optyObj;


  Event e = new Event();
  e.IsAllDayEvent = true;
  e.ActivityDate = Date.today();
  e.WhatId = optyObj.Id

  insert e;
//query for opportunity and check stage updated
}
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.