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
Tommy GeorgiouTommy Georgiou 

test class for trigger on custom object

Hi all,


Due to my poor knowledge on apex I am seeking for help in this forum. I am really new to this so please spare me.
With help from another Dev a trigger was created for a custom object where using the Count_Dinstinct it was reading the unique ReservationCode__c and rolling up the custom field on Contacts if the trigger was firing due to its rules. 
Trigger is : 
 
trigger ReservationTrigger on Reservations__c (after insert,after update) {
    // Get the IDs of related contacts
    Set<Id> contactIds = new Set<Id>();
    for(Reservations__c oneReservation:trigger.new){
        if(oneReservation.ReservationStatus__c == 'Confirmed'){
            contactIds.add(oneReservation.Email__c);
        }
    }
    // Count the distinct Reservation_Number__c from the Reservation objects of all related contacts
    Map<Id, Integer> countDistinctReservationMap = new Map<Id, Integer>();
    for (AggregateResult aggRes : [SELECT COUNT_DISTINCT(ReservationCode__c) resNum, Email__c conId FROM Reservations__c WHERE Email__c IN: contactIds GROUP BY Email__c ]) {
         Id conId = (Id) aggRes.get('conId');
         Integer resNum  = (Integer) aggRes.get('resNum');
         countDistinctReservationMap.put(conId, resNum);
    }
    // Now fetch the Contacts in a list
    List<Contact> listContacts = [Select Id, customRollupField__c from Contact Where Id IN:contactIds];
    if(listContacts.size()>0) {
         for(Contact con : listContacts) {
              // fetch or get the distinct count or rollup from the map and copy it to the contact's field
              con.customRollupField__c = countDistinctReservationMap.get(con.Id);
         }
    }
    // Update the contacts
    update listContacts;
}

My problem is how to get the custom object onto the test class. 

Due to my poor knowledge I think that a test class should include  

Account a = new Account(Name = 'Test');
        insert a;     

     Contact c = new Contact(AccountId=a.Id, lastname='testing', firstname='apex', Email= 'xxxxx');
        insert c;

but here is where I stuck. How to get the custom object onto the test class with the count dinstinct??

Thank you in advance
 
Best Answer chosen by Tommy Georgiou
sandeep sankhlasandeep sankhla
Hi Tommy,

Replace your code for reservation object with below code


    Reservations__c  objR = new Reservations__c (Name = 'testR', ReservationStatus__c = 'Confirmed' , Email__c = objContact.Id , ReservationCode__c = '21');
        Insert objR;

Now check and let me know..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 

All Answers

sandeep sankhlasandeep sankhla
Hi Tommy,

You need to first insert a contact and then Reservations__c  record in your test class like below

      Contact objContact = new Contact(AccountId=a.Id, lastname='testing', firstname='apex', Email= 'xxxxx');
        insert objContact;
    
    Reservations__c  objR = new Reservations__c (Name = 'testR', ReservationStatus__c = 'Confirmed' , Email__c = objContact.Id , ReservationCode__c = 21);
        Insert objR;
        

Also , please provide all mandatory field so it will insert succesfully..

Please use above code to insert these things and then check and let me know if you are able to cover the trigger code ..ReservationStatus__c  should be confirmed and then Email will be conatctId..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
        
Tommy GeorgiouTommy Georgiou
Hi Sandeep,

Thank you for your time to address to my problem.

I have inserted the the above code but I get an error like Error: Compile Error: Invalid initial expression type for field Reservations__c.ReservationCode__c, expecting: String at line 11 column 151


Another thing that was in question. How do I close this test class? with a systemassert?
sandeep sankhlasandeep sankhla
Hi Tommy,

Replace your code for reservation object with below code


    Reservations__c  objR = new Reservations__c (Name = 'testR', ReservationStatus__c = 'Confirmed' , Email__c = objContact.Id , ReservationCode__c = '21');
        Insert objR;

Now check and let me know..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
This was selected as the best answer
Tommy GeorgiouTommy Georgiou
Hi Sandeep,

Thank you very much. It covered 100%. One more question is how do I close this test class. Like in other test classes I have system.assertequals. 
In this case what is more appropriate to use?
sandeep sankhlasandeep sankhla
Hi Tommy ,

It is not mandatory to use system.assert ..but as a best practise and checking if our functionality is cporrect we can use assert statments in our test class..

In above scenario you can use assert to check the list size..if you need..but without assert also it will work..assert is just to confirm the functionality from test class itself..

Thanks,
Sandeep
Tommy GeorgiouTommy Georgiou
Ahh ok Sandeep thank you very much