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
HasmikHasmik 

test code coverage for apex trigger

Hi, I'm new to Apex and I'm not that good at it yet. I created an Appex Trigger on my sandbox and i need to deploy it but I can't do it without test code coverage. I don't really understand how that works, so can someone help me with the test code for the trigger bellow?

trigger UpdateScheduleOnInsertOrUpdate on pmdm__ServiceSession__c (after insert, after update) {
    set<Id> lstId = new set<Id>();
    string details = '';
    string sessionurl = '';
    for(pmdm__ServiceSession__c Con :trigger.new){
        lstId.add(Con.pmdm__ServiceSchedule__c);
    }
    list<pmdm__ServiceSchedule__c> lstAcc = 
        [select id, Meeting_URL__c, Session_Details__c From pmdm__ServiceSchedule__c Where Id IN :lstId];
    for(pmdm__ServiceSchedule__c Acc :lstAcc){
        sessionurl = Acc.Meeting_URL__c;
    }
    list<pmdm__ServiceSession__c> session = 
        [select Session_Details__c,pmdm__ServiceSchedule__c,pmdm__SessionStart__c From pmdm__ServiceSession__c 
         Where pmdm__ServiceSchedule__c IN :lstId ORDER BY pmdm__SessionStart__c];
    for(pmdm__ServiceSession__c Ses :session){
        details += Ses.Session_Details__c + '\n' + sessionurl + '\n';
    }
    
    for(pmdm__ServiceSchedule__c Acc :lstAcc){
        Acc.Session_Details__c = details;
    }
    update lstAcc;   
}
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hasmik,

You just need to create a class which will insert the record of pmdm__ServiceSession__c object with all the required fields and also you need to add Meeting_URL__c field for sure as trigger is based on this logic.

You also need to create a record for pmdm__ServiceSession__c object and relate with the record created above.

Let me know if you face any issue so I can suggest based on it.

If this solution helps, Please mark it as best answer.

Thanks,
 
HasmikHasmik

Hi Sai Pravin,

Thank you for your reply. I tried to do what you suggested but it still sasys that my code coverage is 0%. Can you please see if i did something wrong?

@istest
public class UpdateScheduleOnDeletedTest {
@istest
    public static void unitTest(){
        pmdm__ServiceSchedule__c Schedule = new pmdm__ServiceSchedule__c();
        Schedule.Name = 'Test Cohort';
        Schedule.Meeting_URL__c = 'test@test.com';
        Schedule.pmdm__Service__c = 'a103H0000015ICnQAM';
        insert Schedule ;
        pmdm__ServiceSession__c Session = new pmdm__ServiceSession__c();
        Session.pmdm__ServiceSchedule__c = Schedule.id;
        insert Session;
    }
}