+ Start a Discussion
Arpit Jain92Arpit Jain92 

Trigger test

Hi,

I am writing a test class for a trigger, in that test class when I am getting this error when I write my login in system.runas method but when I write same login in another function it will work. "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, The formula in the "Room Certification Expiry Date" rule or process is invalid due to the following:<br/>Error evaluating formula field for time trigger date:  Room Certification: Expiry Date: " Please tell what is the issue.

Here is the code of Trigeer and test class-

Trigger:-
trigger RG_RoomCertificationExpiryDateUpdate on Room_Certification__c  (After insert,before update)
{    
List<Room_Certification__c > roomCertificationList=[select id,name,Certification_Date__c,Visit__r.Study__r.Study_Certificate_Duration__c from Room_Certification__c where id In:trigger.newMap.keySet()];
 
   if(Trigger.IsUpdate)
   {
    for(Room_Certification__c roomCert:roomCertificationList)
    {
        if(roomCert.Visit__r.Study__r.Study_Certificate_Duration__c=='Duration of Study')
        {
                trigger.newMap.get(roomCert.id).CertificationExpiryDate_String__c='Duration of Study';
        }
        
        if(roomCert.Visit__r.Study__r.Study_Certificate_Duration__c!='Duration of Study' && roomCert.Visit__r.Study__r.Study_Certificate_Duration__c!=null )
        {
                string monthh=roomCert.Visit__r.Study__r.Study_Certificate_Duration__c.removeEnd('m');
                system.debug('@@month'+monthh);
                Date dt=(trigger.newMap.get(roomCert.id).Certification_Date__c.AddMonths(Integer.ValueOf(monthh)))-1;
                system.debug('@@CalculatedDate'+dt);
                trigger.newMap.get(roomCert.id).CertificationExpiryDate_String__c=dt.format();
        }
    }
    }
    //After insert
    if(Trigger.IsInsert)
   {
    List<Room_Certification__c> exCertiChildUpdate=new List<Room_Certification__c>();
    for(Room_Certification__c roomCert:roomCertificationList)
    {
        if(roomCert.Visit__r.Study__r.Study_Certificate_Duration__c=='Duration of Study')
        {
                roomCert.CertificationExpiryDate_String__c='Duration of Study';
                exCertiChildUpdate.add(roomCert);
        }
        
        if(roomCert.Visit__r.Study__r.Study_Certificate_Duration__c!='Duration of Study' && roomCert.Visit__r.Study__r.Study_Certificate_Duration__c!=null )
        {
                string monthh=roomCert.Visit__r.Study__r.Study_Certificate_Duration__c.removeEnd('m');
                system.debug('@@month'+monthh);
                Date dt=(trigger.newMap.get(roomCert.id).Certification_Date__c.AddMonths(Integer.ValueOf(monthh)))-1;
                system.debug('@@CalculatedDate'+dt);
                roomCert.CertificationExpiryDate_String__c=dt.format();
                exCertiChildUpdate.add(roomCert);
        }
    }
    if (exCertiChildUpdate.isEmpty() == false)
    {
        Database.update(exCertiChildUpdate);
    }
    }
}
Test Class:-
@isTest
public class RG_RoomCertificationExpiryDate_Test
{
    public static testmethod void main()
    {
        //Client Create
        Account acc= new Account(Name='Test');
        insert acc;
        //Study Create
        Study__c study= new Study__c(Client__c=acc.id,Name='Testing',NumberofExaminerCertificationRounds__c=2,PONumber__c='123456',Study_Certificate_Duration__c='Duration of Study');
        insert study;
        //Site Create
        Site__c site= new Site__c(Name='Testing Site',Zip_Postal_Code__c='301001',City__c='Delhi');
        insert site;
        //Visit Create
        Visit__c Visit= new Visit__c(Study__c=study.id,Round__c=2,VisitType__c='Visit',VisitStatus__c='Contacted',SiteC__c=site.id,Study_Specific_Site_Number__c='123456');
        insert Visit;
        //Room Create
        Room__c Room= new Room__c(Room_Name__c='Test',SiteC__c=site.Id);
        insert Room;
        //Room Certification Create
        Room_Certification__c Rc= new Room_Certification__c(Room__c=Room.Id,Status__c='certified',Room_Certification_Round__c=2,Certification_Date__c=Date.Today(),VisitSite__c=site.id,Visit__c=Visit.Id);
        Insert Rc;
        study.Study_Certificate_Duration__c='6m';
        update study;
    }
    public static testmethod void main1()
    {
        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='Testingapexclass@gmail.com');
        system.RunAs(u)
        {
            //Client Create
            Account acc= new Account(Name='Test');
            insert acc;
            //Study Create
            Study__c study= new Study__c(Client__c=acc.id,Name='Testing',NumberofExaminerCertificationRounds__c=2,PONumber__c='123456',Study_Certificate_Duration__c='Duration of Study');
            insert study;
            //Site Create
            Site__c site= new Site__c(Name='Testing Site',Zip_Postal_Code__c='301001',City__c='Delhi');
            insert site;
            //Visit Create
            Visit__c Visit= new Visit__c(Study__c=study.id,Round__c=2,VisitType__c='Visit',VisitStatus__c='Contacted',SiteC__c=site.id,Study_Specific_Site_Number__c='123456');
            insert Visit;
            //Room Create
            Room__c Room= new Room__c(Room_Name__c='Test',SiteC__c=site.Id);
            insert Room;
            //Room Certification Create
            Room_Certification__c Rc= new Room_Certification__c(Room__c=Room.Id,Status__c='certified',Room_Certification_Round__c=2,Certification_Date__c=Date.Today(),VisitSite__c=site.id,Visit__c=Visit.Id);
            Insert Rc;
        }
    }
}
Best Answer chosen by Arpit Jain92
RaidanRaidan
Hi Arpit,

It seems like the trigger is causing the formula to break. Perhaps you want to check the formula field and see if the trigger and its test data create an invalid date within the formula.

All Answers

RaidanRaidan
Hi Arpit,

It seems like the trigger is causing the formula to break. Perhaps you want to check the formula field and see if the trigger and its test data create an invalid date within the formula.
This was selected as the best answer
Arpit Jain92Arpit Jain92
@Raidan

Thank You.