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
Monet Zamaripa 3Monet Zamaripa 3 

Hi! I was able to create my first Event trigger to update an opportunity field but am having problems creating a test class. Any assistance is appreciated!

trigger updateOpportunty on Event (after update) {
    
    List<Id> OpportuntyIds=new List<Id>();
    List<Opportunity> OpportunitysToUpdate =new List<Opportunity>();
    for(Event e:trigger.new){
            if(String.valueOf(e.whatId).startsWith('006') == TRUE)
            if((e.subject).contains('First Meeting'))
                        {//check if the task is associated with a Opportunty
                OpportuntyIds.add(e.whatId);
            }
    }
    Map<Id,Opportunity> OpportunityMap = new Map<Id,Opportunity>([SELECT Id,Meeting_Initially_Scheduled_Date__c FROM Opportunity WHERE Id IN :OpportuntyIds]);
    
    For (Event e:trigger.new){
        Opportunity opp = OpportunityMap.get(e.whatId);
        opp.Meeting_Initially_Scheduled_Date__c = e.Initially_Scheduled__c;
        OpportunitysToUpdate.add(opp);
    }
    
    try{
        update OpportunitysToUpdate;
    }catch(DMLException e){
        system.debug('Opportunitys were not all properly updated.  Error: '+e);
    }
}
Best Answer chosen by Monet Zamaripa 3
DeveloperSudDeveloperSud
Hi Monet,

Actually in my code I creted those filed as Text. Changed now.Try the below code .Please mark this as solved if this solves your problem.Thanks.
@isTest
public class updateOpportuntyTest {
    
    static testmethod void myUnitTest1(){
        // Create Opportunity Test Data
        Opportunity ops=new Opportunity();
        ops.Name='Test Opportunity';
        ops.StageName='Test Stage';
        ops.CloseDate=system.today();
        insert ops;
        // Create a test event 
        event ev =new event();
        ev.Subject='Meeting';
        ev.StartDateTime=system.now()-1;
        ev.EndDateTime=system.now();
        ev.OwnerId=userinfo.getUserId();
        ev.WhatId=ops.id;
        insert ev;
        
        //Update a that event with 
        Test.startTest();
       
        ev.Subject='First Meeting';
        ev.Initially_Scheduled__c=system.today();
        try{
        update ev;
        }
        catch(Exception e){
        }
        Test.stopTest();
        
        Opportunity op=[select id,Meeting_Initially_Scheduled_Date__c from Opportunity where id=:ops.id];
        system.assertEquals(system.today(), op.Meeting_Initially_Scheduled_Date__c);
        
    }
}

 

All Answers

DeveloperSudDeveloperSud
Hi Monet,

Try something like this .
@isTest
public class updateOpportuntyTest {
    
    static testmethod void myUnitTest1(){
        // Create Opportunity Test Data
        Opportunity ops=new Opportunity();
        ops.Name='Test Opportunity';
        ops.StageName='Test Stage';
        ops.CloseDate=system.today();
        insert ops;
        // Create a test event 
        event ev =new event();
        ev.Subject='Meeting';
        ev.StartDateTime=system.now()-1;
        ev.EndDateTime=system.now();
        ev.OwnerId=userinfo.getUserId();
        ev.WhatId=ops.id;
        insert ev;
        
        //Update a that event with 
        Test.startTest();
       
        ev.Subject='First Meeting';
        ev.Initially_Scheduled__c='Yes';
        try{
        update ev;
        }
        catch(Exception e){
        }
        Test.stopTest();
        
        Opportunity op=[select id,Meeting_Initially_Scheduled_Date__c from Opportunity where id=:ops.id];
        system.assertEquals('Yes', op.Meeting_Initially_Scheduled_Date__c);
        
    }
}

 
Monet Zamaripa 3Monet Zamaripa 3
THANK YOU SO MUCH!! Thanks you to you, I am very close!  I'm only getting an error on line 24 because the Initially Scheduled field is a DATE field.  I want the Initially scheduled field to populate the Meeting Initially scheduled field on the opportunity.  Any ideas what to change that to?
DeveloperSudDeveloperSud
Hi Monet,

Actually in my code I creted those filed as Text. Changed now.Try the below code .Please mark this as solved if this solves your problem.Thanks.
@isTest
public class updateOpportuntyTest {
    
    static testmethod void myUnitTest1(){
        // Create Opportunity Test Data
        Opportunity ops=new Opportunity();
        ops.Name='Test Opportunity';
        ops.StageName='Test Stage';
        ops.CloseDate=system.today();
        insert ops;
        // Create a test event 
        event ev =new event();
        ev.Subject='Meeting';
        ev.StartDateTime=system.now()-1;
        ev.EndDateTime=system.now();
        ev.OwnerId=userinfo.getUserId();
        ev.WhatId=ops.id;
        insert ev;
        
        //Update a that event with 
        Test.startTest();
       
        ev.Subject='First Meeting';
        ev.Initially_Scheduled__c=system.today();
        try{
        update ev;
        }
        catch(Exception e){
        }
        Test.stopTest();
        
        Opportunity op=[select id,Meeting_Initially_Scheduled_Date__c from Opportunity where id=:ops.id];
        system.assertEquals(system.today(), op.Meeting_Initially_Scheduled_Date__c);
        
    }
}

 
This was selected as the best answer