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
NongNong 

Got error on validation: Failure Message: "System.AssertException: Assertion Failed: Expected: null,

Dear All,

 

Could you please advise as i created one trigger under event and build testing class in below coding.
I got successful result on Sandbox run testing but i got the error below on production deployment when i clicked

Validate button .

 

Could you please advise what's the problem of my coding.

 

Failure Message: "System.AssertException: Assertion Failed: Expected: null, Actual: 2011-02-15 00:00:00", Failure Stack Trace: "Class.testAccountTravelManagementEvent_Trigger.AccountEventTest: line 31, column 16 External entry point"

 

 

My trigger coding is :

 

trigger AccountTravel_Management_Event on Event(before insert, before update,after insert,after update,after delete) {                
Event evt;  
String evtId='';  
   
    if(Trigger.isInsert || Trigger.isUpdate )  
    {
    evt= Trigger.New[0];
    evtId= evt.whatid;
    }
    else
    evtId=Trigger.old[0].whatid;
      
     
    Integer ivisit=0;
    DateTime dtVisit;
    
    if(evtId != null && evtId.substring(0,3)== '001')
    {       
       //get Account object that link to this Task
       Account acc = [select Rating,Next_Visit_Date__c,Owner_Country__c From Account where id=:evtId]; 
       if(acc.Owner_Country__c<> null)
       {
       if(acc.Owner_Country__c.toLowerCase()=='thailand')
       {
         //==================== set Task Budget ======================  
         String strRating  = acc.Rating.toUpperCase();
            //set Task default Budget via Account Rating
            if (strRating == 'PLATINUM' || strRating == 'GOLD') {
                ivisit=1;
            } else  if (strRating == 'SILVER') {
                ivisit=15;   
            } else  if (strRating == 'BRONZE') {
                ivisit=1;                          
            } else  if (strRating == 'INACTIVE') {
                ivisit=1;
            }
           
//==================== set Next Visti Date ======================
            Event[] evtVisitmax;
            Try{
//===================get latest 'xxxxxxxxx Face to Face' task==========
evtVisitmax= [Select ActivityDateTime  From Event where 
whatid=:evtId and  what.type = 'Account' and ActivityDateTime <> null and subject  like '%Face to Face'  and isDeleted = false  order By ActivityDateTime DESC limit 1];    
                
if(evtVisitmax.size()>0 && ivisit>0)
{
if(ivisit==1)//number of month                
                    dtVisit=evtVisitmax[0].ActivityDateTime.addMonths(ivisit);                
else//number of days           
                    dtVisit=evtVisitmax[0].ActivityDateTime.addDays(ivisit);
                    
System.debug('dtVisit='+dtVisit);
//convert datetime to date 
acc.Next_Visit_Date__c  = date.newinstance(dtVisit.year(), dtVisit.month(), dtVisit.day());                
} 
else{
acc.Next_Visit_Date__c=null ;                
    } 
update acc;                 
            }
            catch(System.QueryException e){
            trigger.new[0].addError(' Can not select latest Start Date of all Tasks'); 
            Return;
            }
        }}//if Thailand                        
    }//if not null and 001   
        
}

 

 

My test class is

 

 

@isTest
private class testAccountTravelManagementEvent_Trigger{
static testMethod void AccountEventTest() 
{     
test.startTest();  
// =========add Account=======================================           
                               
DateTime dtn_before4=system.now().addDays(-4);
DateTime dtn=system.now();
DateTime dtn_after1=system.now().addDays(1);   
DateTime dtn_after2=system.now().addDays(2);
Date dn_after1= date.newinstance(dtn_after1.year(), dtn_after1.month(),dtn_after1.day());
                  
                
Account a1 = new Account(Name = 'Test Account ' + 'Platinum',Rating='Platinum',Owner_Country__c='Thailand');
                 insert a1;
                    
Event e1 = new Event (Subject= 'Account Management Appointment - Face to Face' ,whatid=a1.Id,ActivityDateTime =dtn_after1,EndDateTime=dtn_after2);
                 insert e1; 
                    
Event e2 = new Event (Subject= 'Account Management Appointment - Face to Face' ,whatid=a1.Id,ActivityDateTime =dtn_before4,EndDateTime=dtn);
                 insert e2;
                
               
List<Account> insertedAccounts = [SELECT Id,Rating,Next_Visit_Date__c,Owner_Country__c FROM Account  WHERE Id=:a1.Id ];                       

System.debug('1test added record account='+insertedAccounts.size()); 
               System.debug('date1='+insertedAccounts[0].Next_Visit_Date__c); 
               System.debug('date2='+dn_after1.addMonths(1)); 
               
               System.debug(insertedAccounts[0].Next_Visit_Date__c==dn_after1.addMonths(1));
               System.assertEquals(insertedAccounts[0].Next_Visit_Date__c,dn_after1.addMonths(1));
               
              test.stopTest();
      }
}

 

Could you please advise how can i solve the problem , thank you very much in advance for your help.

 

Best Regards

Anong

 

NongNong

Hello,

 

I just found the way to solve this problem by adding code on RunAs user to that match the criteria.

 

My final coding is

 

@isTest
private class testAccountTravelManagementEvent_Trigger{
     static testMethod void AccountEventTest() 
      {   
            Profile p = [select id from profile where name='Mondial Assistance - Sales (Travel)'];                                       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='standarduser@matestorg.com',            UserRoleId=r.id   );
           
            UserRole r = [select id from UserRole where name='Thailand - Travel'];  


     System.runAs(u)
   
     {  
// =========add Account=======================================
DateTime dtn_before4=system.now().addDays(-4);
DateTime dtn=system.now();
DateTime dtn_after1=system.now().addDays(1);   
DateTime dtn_after2=system.now().addDays(2);
Date dn_after1= date.newinstance(dtn_after1.year(), dtn_after1.month(),dtn_after1.day());
 
test.startTest();                    Account a1 = new Account(Name = 'Test Account ' + 'Platinum',Rating='Platinum',Owner_Country__c='Thailand');
  insert a1;                         
                Event e1 = new Event (Subject= 'Account Management Appointment - Face to Face' ,whatid=a1.Id,ActivityDateTime =dtn_after1,EndDateTime=dtn_after2);
  insert e1;                                    
                Event e2 = new Event (Subject= 'Account Management Appointment - Face to Face' ,whatid=a1.Id,ActivityDateTime =dtn_before4,EndDateTime=dtn);
  insert e2;
List<Account> insertedAccounts = [SELECT Id,Rating,Next_Visit_Date__c,Owner_Country__c FROM Account  WHERE Id=:a1.Id ];                       
  System.assertEquals(insertedAccounts[0].Next_Visit_Date__c,dn_after1.addMonths(1));
  test.stopTest();              
            }//run as      
      }
}