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
kumarcrm bingikumarcrm bingi 

i am try to writing test class but not code coverage

This is my Class
-------------------
trigger trgStudentSchoolFields on Student_School_Fields__c (after insert, after update){
    String IntegrationUser = System.Label.IntegrationUser;
    map<string,string> mapContacts = new map<string,string>();
    list<Contact> listOfContact = new list<Contact>();
    list<Inquiry__c> inquiryList = new list<Inquiry__c>();
    list<Opportunity> oppList = new list<Opportunity>();
    String uid = userinfo.getUserId();
    String IntgUser;
    system.debug('USERID>>>' + uid);
    system.debug('IntegrationUser>>>' + IntegrationUser);
    list<Contact_Method__c> cmlist=new list<Contact_Method__c>();
    Set<string> stdscmid = New Set<string>();
    List<Error__c> errconlst = New List<Error__c>();
    List<Error__c> errlst = New List<Error__c>();  
   // if (!uid.contains(IntegrationUser))
   // {
    if(userinfo.getUserId() != '00555000003neB6'){
        list<Student_School_Fields__c> lstSchStud=new list<Student_School_Fields__c>();
        for(Student_School_Fields__c schstudobj:trigger.new)
        {
            stdscmid.add(schstudobj.id);
            lstSchStud.add(schstudobj);
            mapContacts.put(schstudobj.Contact__c, schstudobj.Student_Enrollment_Campus__c);
        }
        errconlst = [select ErrorCode__c, Error_Message__c, Error_Type__c, ObjectType__c, Status__c,
                     ContactRelated__c, OpportunityRelated__c, APIOperation__c
                     From Error__c 
                     Where Error_Type__c = 'Data' 
                     AND Status__c = 'Failed' 
                     AND ObjectType__c = 'StudentSchoolFields'
                     AND StudentSchoolFieldRelated__c IN :stdscmid];
        for (Error__c errobj:errconlst)
        {
            errobj.Status__c = 'Processed';
            errlst.add(errobj);
        }
        if (errlst.size() > 0)
            update errlst;
        
        if(mapContacts.size()>0 && !mapContacts.isEmpty()){
            for(Contact con : [SELECT Id, Student_Enrollment_Campus__c FROM Contact
                               where  id IN:mapContacts.keySet()]){
                                   string enrollment = mapContacts.get(con.id);
                                   System.debug('con'+con);
                                   System.debug('enrollment'+enrollment);
                                   if(enrollment != null){
                                       con.Student_Enrollment_Campus__c = enrollment; 
                                       listOfContact.add(con);
                                   }
                               }
            
            for(Inquiry__c inquiry : [SELECT Id, Student_Student_Enrollment_Campus__c, Contact__c FROM Inquiry__c
                                      where  Contact__c IN:mapContacts.keySet()]){
                                          System.debug('inquiry'+inquiry);
                                          string enrollment = mapContacts.get(inquiry.Contact__c);
                                          if(enrollment != null){
                                              inquiry.Student_Student_Enrollment_Campus__c = enrollment; 
                                              inquiryList.add(inquiry);
                                          }
                                      }
            for(Opportunity opp : [SELECT Id, ContactId , Student_Enrollment_Campus__c FROM Opportunity where
                                   ContactId  IN:mapContacts.keySet()]){
                                       System.debug('opp'+opp);
                                       string enrollment = mapContacts.get(opp.ContactId);
                                       if(enrollment != null){
                                           opp.Student_Enrollment_Campus__c = enrollment; 
                                           oppList.add(opp);
                                       }
                                   }
            if(listOfContact.size()>0 && !listOfContact.isEmpty()){
                TrgInquiryHandler.isStudentSchoolStopTriggerContact=true;
                update listOfContact;
            }
            if(inquiryList.size()>0 && !inquiryList.isEmpty()){
                update inquiryList;
            }
            if(oppList.size()>0 && !oppList.isEmpty()){
                TrgInquiryHandler.isStudentSchoolStopTriggerOpp=true;
                update oppList;
            }
        }
        string jsonstr=JSON.serialize(lstSchStud);
        
        if(Trigger.isAfter && Trigger.isInsert || Trigger.isAfter && Trigger.isUpdate )
        {   if(!System.isFuture() && !System.isBatch())
            studentSchoolFields.updateSchfldStudentsCVUe(jsonstr);
        }
   // }
        }
}


Test Class 
=============
@isTest
Public  class trgStudentSchoolFieldsTestClass {  
    public static testmethod void contactmethodtest(){
    
       set<string> conid=new set<string>();
        
        Contact con =new Contact();
        con.lastname = 'testmore';
        con.Subscribe_To_SMS_Service__c=true;
        con.Student_Enrollment_Campus__c = 'AUR';
        insert con;
        
        Inquiry__c  inq = new Inquiry__c();
        inq.Name = 'inasfewna===100years====createNewInq';
        inq.Student_Student_Enrollment_Campus__c = 'ONL';
        inq.Contact__c = con.id;
       // insert inq;

        
        Contact_Method__c cm=new Contact_Method__c();
        cm.Name='test';
        cm.Contact__c=con.Id;
        cm.Subscribe_To_SMS_Service__c=true;
        insert cm;
        conid.add(cm.id);
    }
    }
This test class is not Code Coverage in my Class were i am missing i am not understand can you please any one help me
AnudeepAnudeep (Salesforce Developers) 
Hi kumarcrm, 

It appears that this test class is not firing the trgStudentSchoolFields trigger. Please ensure that you insert/update Student_School_Fields__c record to fire the trigger 

Please see the example listed in the following trailhead module. Thank you

https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_triggers

Anudeep
 
Ashima nidhiAshima nidhi
@kumarcrm, Looks like you are new to salesforce. However for your question . TO run a trigger we require DML operation on the same object . In you case you are not doing so inside a test class mentioned.

There should be a DML on object Student_School_Fields__c  (insert and update) in the test class trgStudentSchoolFieldsTestClass to cover the trigger.
the way you are doing for contact do the same for above object.

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.
SFDC Apex DevSFDC Apex Dev
Hi Kumar,

As your trigger is on Student_School_Fields__c, you must insert this object in your test class in order to cover your trigger.
 
@isTest
Public  class trgStudentSchoolFieldsTestClass {  
    public static testmethod void contactmethodtest(){
                   
        test.startTest();
        
        Contact con = new Contact();
        con.lastname = 'testmore';
        con.Subscribe_To_SMS_Service__c=true;
        con.Student_Enrollment_Campus__c = 'AUR';
        insert con;
        
        Opportunity opp = new Opportunity();
        opp.ContactId = con.Id;
        opp.Name = 'Test Opportunity';
        opp.StageName = 'Won';
        opp.CloseDate = System.today();
        opp.Student_Enrollment_Campus__c = 'Selected';
        insert opp;
        
        Inquiry__c  inq = new Inquiry__c();
        inq.Name = 'inasfewna===100years====createNewInq';
        inq.Student_Student_Enrollment_Campus__c = 'ONL';
        inq.Contact__c = con.id;
        insert inq;        
        
        Contact_Method__c cm=new Contact_Method__c();
        cm.Name='test';
        cm.Contact__c=con.Id;
        cm.Subscribe_To_SMS_Service__c=true;
        insert cm;
        
        Student_School_Fields__c students = new Student_School_Fields__c();
        students.Name = 'Test';
        students.Contact__c = con.Id;
        students.Student_Enrollment_Campus__c = 'Selected';
        insert students;
        
        Error__c error = new Error__c();
        error.Name = 'Test Error';
        error.Error_Type__c = 'Data';
        error.Error_Message__c = 'failure';
        error.ErrorCode__c = '404';
        error.ObjectType__c = 'contact';
        error.Status__c = 'Failed';
        error.ContactRelated__c = con.Id;
        error.OpportunityRelated__c = opp.Id;
        error.StudentSchoolFieldRelated__c = students.Id;
        insert error;
                
        test.stopTest();
    }
}

Above test class will give you 92% code coverage.
Please mark this as the best answer if it resolves your issue. 

Thanks!
Chirag