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
Krishna Sahu 1Krishna Sahu 1 

Please help me to solve the code to cover all the code

 Set<Id> setInvalidWhatIdRecordTypes = new Set<Id>();
        Set<Id> setWhatIds = new Set<Id>();
        for(Event objEvent : listEvents)
        { 
            if(objEvent.WhatId != null){
                setWhatIds.add(objEvent.WhatId);
            }
        }
        for(Account acc : [SELECT Id
                           FROM Account
                           WHERE Id IN : setWhatIds
                           AND RecordType.DeveloperName != 'IndustriesBusiness']){
                               setInvalidWhatIdRecordTypes.add(acc.Id);
                           }
        if(!setInvalidWhatIdRecordTypes.isEmpty())
        {
            for(Event objEvent : listEvents)
            {
                if(objEvent.whatId != null && setInvalidWhatIdRecordTypes.contains(objEvent.whatId))
                {
                    objEvent.WhatId.addError('Account record type can only be business.');
                }
            }
        }
    }
Krishna Sahu 1Krishna Sahu 1
I need to write test class
CharuDuttCharuDutt
Hii Kridhna
Can You Please Give Full Code
Krishna Sahu 1Krishna Sahu 1
before insert trigger
 
public static void acceptOnlybussinessAccount(List<Event> listEvents){
        Set<Id> setInvalidWhatIdRecordTypes = new Set<Id>();
        Set<Id> setWhatIds = new Set<Id>();
        for(Event objEvent : listEvents)
        { 
            if(objEvent.WhatId != null){
                setWhatIds.add(objEvent.WhatId);
            }
        }
        for(Account acc : [SELECT Id
                           FROM Account
                           WHERE Id IN : setWhatIds
                           AND RecordType.DeveloperName != 'IndustriesBusiness']){
                               setInvalidWhatIdRecordTypes.add(acc.Id);
                           }
        if(!setInvalidWhatIdRecordTypes.isEmpty())
        {
            for(Event objEvent : listEvents)
            {
                if(objEvent.whatId != null && setInvalidWhatIdRecordTypes.contains(objEvent.whatId))
                {
                    objEvent.WhatId.addError('Account record type can only be business.');
                }
            }
        }
    }


===============================================================
Test Class
@IsTest
public class EventTriggerHandlerTest {
    
    @testSetup
    public Static void CreateTestData(){
        RecordType recordType = [SELECT Id, Name, DeveloperName FROM RecordType WHERE DeveloperName='IndustriesBusiness' LIMIT 1];
        Account accountRecord = new Account();
        accountRecord.Name = 'TestData';
        accountRecord.Region__c='Midwest';
        accountRecord.RecordTypeId = recordType.Id;
        insert accountRecord;
        List<Event> listEvent = new List<Event>();
        for(Integer count=0; count<4; count++){
            Event eventRecord = new Event();
            eventRecord.procedure_type__c = 'Trial';
            eventRecord.implant_status__c = 'New';
            eventRecord.ActivityDate = System.today();
            eventRecord.DurationInMinutes = 0;
            eventRecord.WhatId = accountRecord.Id;
            eventRecord.EndDateTime = System.today() + 1;
            eventRecord.StartDateTime = System.today() + 1;
            eventRecord.ActivityDateTime = System.today() + 1;
            eventRecord.patient__c = accountRecord.id;
            eventRecord.rep_assigned__c = UserInfo.getUserId();
            eventRecord.Subject = 'Call';
            listEvent.add(eventRecord);
        }
        insert listEvent;
    }
    
    
    @isTest
    public static void checkCreateTaskAfterTrialImplant(){
        List<Event> eventLists = [SELECT Id, patient__c, OwnerId  FROM Event];
        Set<Id> patientIds = new Set<Id>();
        for(Event eventRecord : eventLists){
            patientIds.add(eventRecord.patient__c);
        }
        Test.startTest();
        List<Task> taskRecords=[SELECT Id, OwnerId, WhoId, WhatId FROM Task WHERE patient__c IN: patientIds];
        Test.stopTest();
        System.assertequals(4 , taskRecords.size());    
    }

    @isTest
    public static void verifyUpdatePatientProcedureStatus(){
        List<Event> eventLists = [SELECT Id, patient__c, OwnerId  FROM Event];
        Set<Id> patientIds = new Set<Id>();
        for(Event eventRecord :eventLists){
            eventRecord.implant_status__c = 'Completed';
           }
        Test.startTest();
        update eventLists;
        Test.stopTest();
        List<Account> accList=[SELECT Id , trial_procedure_status__c FROM Account WHERE Id=:eventLists[0].patient__c];
        System.assertEquals('Follow-Up Started', accList[0].trial_procedure_status__c);
    } 
    @isTest
    public static void verifyacceptOnlybussinessAccount(){
        Account listOfAccount=[SELECT Id, Name , RecordType.Name FROM Account WHERE Name ='TestData'];
        List<Event>  listOfEvent = [SELECT Id, patient__c FROM Event WHERE WhatId =: listOfAccount.Id];
        //Code method//
    }
}
CharuDuttCharuDutt
Hii Krishna
Try Below Code Please Replace The ClassName With the Highlight  Change Record Type Other Than 'IndustriesBusiness'
Code Coverage 100!
@IsTest
public class EventTriggerHandlerTest {
    
    @istest
    public Static void CreateTestData(){
        RecordType recordType = [SELECT Id, Name, DeveloperName FROM RecordType WHERE DeveloperName='Recordtype1' LIMIT 1];
        Account accountRecord = new Account();
        accountRecord.Name = 'TestData';
        accountRecord.RecordTypeId = recordType.Id;
        insert accountRecord;
        List<Event> listEvent = new List<Event>();
        for(Integer count=0; count<4; count++){
            Event eventRecord = new Event();
         
            eventRecord.ActivityDate = System.today();
            eventRecord.DurationInMinutes = 0;
            eventRecord.WhatId = accountRecord.Id;
            eventRecord.EndDateTime = System.today() + 1;
            eventRecord.StartDateTime = System.today() + 1;
            eventRecord.ActivityDateTime = System.today() + 1;
          
            eventRecord.Subject = 'Call';
            listEvent.add(eventRecord);
        }
        insert listEvent;
    dxcxvxcvxc.acceptOnlybussinessAccount(listEvent);
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
ravi soniravi soni
Hi Krishna,
Here is test class with 100% Coverage and With Best Practice.
don't forget to mark it as best answer.
Thank you