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
bouscalbouscal 

Increase code coverage from 70% - Asset Trigger - Create Event

I've got 70% coverage and don't understand how to write a test that will check the Event created when the Asset is updated.
Lines that are NOT covered by my current test will be appended with /* not covered */
 
/*  
/* 
/* Create an Event when Asset is Migrated
/* 
/* */

public class CreateEvent{
    public static void newCompanyEvent(Asset[] asid){
        String strAccountSite=null;  // Account.Site field value
        Id conId=null; 
        Id assId=null; 
        Id accId=null; 
        Id recId='01270000000DtPb'; 
        Id ownId='00570000001fIep'; 
        
        Datetime dtTargetDateTime=datetime.now();  // Target date/time for Event
       // dtTargetDateTime=dtTargetDateTime.addDays(365); 
        Date dtTargetDate=dtTargetDateTime.date();  // Target date for Event
		Map<Id, String> assSiteMap = new Map<Id, String>();  
        for (Asset a:asid){
            assSiteMap.put(a.AccountId, null); /* not covered */
        }
        
        List<Account> accList = new List<Account>([SELECT Id, Site FROM Account 
                                                  WHERE Id IN: assSiteMap.keyset()]); 
        for(Account ac:accList){
            assSiteMap.put(ac.id, ac.Site);  /* not covered */
        }
        
        List<Event> ev = new List<Event>();
        
        for (Asset a:asid){
            conId=a.ContactId;  /* not covered */
            accId=a.AccountId;  /* not covered */
            strAccountSite=assSiteMap.get(accId);  /* not covered */
            
            Event newEvent= new Event(  /* not covered */
                ownerid=ownId,
                Subject='Company "Go Live"', 
                ActivityDate=dtTargetDate,
                StartDateTime=dtTargetDateTime, 
                EndDateTime=dtTargetDateTime,
                Location=strAccountSite, 
                ShowAs='Free', 
                IsAllDayEvent=TRUE, 
                Event_Status__c='Scheduled',
                Type='Non-Visit',
                Purpose__c='Sales',
                Reason__c='Conversion',
                Whoid=conId, 
                Whatid=assId, 
                RecordTypeId=recId);       
            ev.add(newEvent);            /* not covered */
        }
        insert ev; 
    }
}
and here is my current test class
@isTest(SeeAllData=true)
public class CreateEvent_test {
    
    static testMethod void testAll(){
      
        id idRecType=null; 
        TestObject to = new TestObject(); 
        Account acc = to.getAccount(true); 
        Contact con = to.getContact(acc.id, true); 
        Opportunity opp = to.getOpportunity(acc.id, con.Id, true); 
        opp.Product_Group__c='Company Enterprise'; 
        update opp; 
        

        Asset ass = to.getAsset(acc.id, con.id, opp.id, true); 
        ass.status='Migrated'; 
        ass.Product_Group__c='Product X'; 

        update ass; // this should fire the trigger to create the Event
        List<Event> ev = new List<Event>([SELECT id, subject FROM event WHERE whatid=:ass.Id]);
        test.startTest();           
        for(Event e:ev){    

            // validate success
            System.assertEquals(e.subject, 'Company "Go Live"'); 
            System.assertEquals(e.AccountId, acc.id); 
            System.assertEquals(e.WhoId, con.id); 
            // System.assertEquals(e.Location)
        }
        test.stopTest(); 
    }
}



 
ra1ra1
Can you please post your Asset's trigger code too.
ra1ra1
One assumption is that the newCompanyEvent method is getting empty "asid" List and that's why Test code is not convering any line which is in loop of "asid".

One alternative of this issue can be, instead of updating asset, you can directly call newCompanyEvent from your test class and pass List of assets to it. 
Sample code can be:
static testMethod void testAll2(){

        id idRecType=null;

        TestObject to = new TestObject();
        Account acc = to.getAccount(true);
        Contact con = to.getContact(acc.id, true);
        Opportunity opp = to.getOpportunity(acc.id, con.Id, true);
        opp.Product_Group__c='Company Enterprise';
        update opp;

        Asset ass = to.getAsset(acc.id, con.id, opp.id, true);
        ass.status='Migrated';
        ass.Product_Group__c='Product X';
 
        List<Asset> assetList = new List<Asset>();
        assetList.add(ass);

        Test.startTest();
        CreateEvent.newCompanyEvent( assetList);
        Test.stopTest();
        
        List<Event> ev = new List<Event>([SELECT id, subject FROM event WHERE whatid=:ass.Id]);

        for(Event e:ev){   
                // validate your event
        }
}

I will prefer to have both methods availabe in your test class. This will help you identify the issue within newCOmpanyEvent (if testAll2 get failed) or the end-to-end process is having issue (where testAll get failed).