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
Nitzan MarinovNitzan Marinov 

Deployment Validation failed but no explanation

Hi all,

I made some changes to Apex class code and all tests come back without a failure. However, when I try to deploy and run the validation, I still get below 75% coverege but there is no explanation. Does anyone know how to resolve this?

Deployment Validation Error

Thanks
Nitzan
Best Answer chosen by Nitzan Marinov
Himanshu ParasharHimanshu Parashar

Hi Nitzan,

Salesforce require mininum 75% code coverage of Apex code prior to PROD deployment. As you have added some new Apex code in your Apex Class you need to add test cases for that code.

You can go to Developer console -> Test Cases (bottom tab) and check for uncovered code.

User-added image 


Thanks,

Himanshu

All Answers

Himanshu ParasharHimanshu Parashar

Hi Nitzan,

Salesforce require mininum 75% code coverage of Apex code prior to PROD deployment. As you have added some new Apex code in your Apex Class you need to add test cases for that code.

You can go to Developer console -> Test Cases (bottom tab) and check for uncovered code.

User-added image 


Thanks,

Himanshu

This was selected as the best answer
Ashish Dev 3Ashish Dev 3
"Run all test" and observe coverage of individual classes. Try to increate the coverage where classes where ever you can.
Nitzan MarinovNitzan Marinov
Thanks guys. This is useful as it takes me a bit closer. However, I am still not sure what's going on.

I "Run all tests" and got quite a few error messages:
Errors
All the ones I opened up are 'hidden' so I'm not sure how to proceed.

Thanks for your help.
Nitzan
Himanshu ParasharHimanshu Parashar
If your last deployement was successful try to run test class for the class only for which you have made changes.
Nitzan MarinovNitzan Marinov
I made changes to 3 classes. I am trying to deploy 2 of them here. They all pass the tests. That's whay I'm not sure why they fail deployment validation.
Himanshu ParasharHimanshu Parashar
what is the code coverage of that class? is it above 75% ?
Nitzan MarinovNitzan Marinov

Sorry, I'm still finding my way around this so will ask some basic questions...:-)

I think the screenshot I shared shows 71%. Am I looking in the right place?

Himanshu ParasharHimanshu Parashar
yes you are looking at right place and yes you have 71% overall coverage, scroll down in that right area and check what is the code coverage you have for that class. are you covering all code which you are deploying. 

your code should not be in red mark area once you check that class.
Nitzan MarinovNitzan Marinov
OK. So I'm trying to work out what code coverage means and read up about it.
I ran the test for one of my classess and saw this list:
Coverage

Does this mean that I need to focus on the classes with the lowest coverage percentage?
Thanks
Himanshu ParasharHimanshu Parashar
yes that is correct. Are you deploying ProjectTrigger ?
Nitzan MarinovNitzan Marinov
No. This is one (of many...) of the things that confses me....

I haven't touched ProjectTrigger so I don't understand why I need to make any changes to it. And I have no clue what I need to change :-(

ProjectTrigger

Thanks for helping me with this and sticking with me, I appreciate it.
Himanshu ParasharHimanshu Parashar
Sorry for confusion. I said ProjectTrigger just because it was selected in your last screenshot. lol.

I still don't know which one is your deploy class. so here what I am proposing to resolve your issue.

Your main task is now to increase the code coverage and make it 75%,

 If you don't have any idea about this trigger you can leave that but in that case you need to pick any other class or trigger where code coverage is very low. so for example if you have a class with 10% code coverage and you write test class for that and make it 70% your overall code coverage will go up, it can cross the 75% what we want to achieve so simply give it a try and let me know your progress ;)

If you face any issue while writing the class you can take help from here.

Thanks,
Himanshu
Nitzan MarinovNitzan Marinov
I'll write in bold to differentiate my words from the code.
Thank you so much! With every answer I understand a bit more (I think....).

So, let me be more clear (and sorry I wasn't before. I wasn't sure what information you'd need).


The only (non test) class I changed is this:

public with sharing class TestUtilities {
    
    public Contact      aContac         {get; set;}
    public Attachment   aAttachment     {get; set;}
    
    static User testUser;
    
    public void generateContact(){

        this.aContac                = new Contact();
        this.aContac.firstName      = 'Test';
        this.aContac.LastName       = 'Test';
        
        insert this.aContac;

        this.aAttachment = new Attachment();
        this.aAttachment.Body = Blob.valueOf('String');
    }
    
    public static TestUtilities generateTest(){
        TestUtilities e = new TestUtilities();
        e.generateContact();
        return e;
    }
    
        public static User createUser(Id profileId)
        {   
            //fetch existing users with provided profile and role
            List<User> userLst = [Select Id From User where ProfileId =: profileId and IsActive = :true Limit 1];
            
            User user;
            
            if(userLst.size()>0)
            {
                //at least one user exists; return user record
                user =  userLst[0];
            }
            else
            {
                //user does not exist; create new user
                user = new User(profileid = profileId,
                                alias = 'standt', email = 'standarduser@testorg.com',
                                emailencodingkey = 'UTF-8', lastname = 'Tsting', languagelocalekey = 'en_US',
                                localesidkey = 'en_US', timezonesidkey = 'America/Los_Angeles',
                                username = profileId + '@testorg.com');
                insert user;
            }
           
            return user;
        }
        
    public static Clients__c insertClient()
    {
        Clients__c c = new Clients__c(Name ='TestClient', Active__c = true,
                                      OC_Programme__c = 'OC Central', Current_Address__c = 'N/A',
                                      Primary_Phone_Number__c = 'N/A');
        return c;
    }
    
    public static Contact insertContact()
    {
        Contact c = new Contact(FirstName ='Test',LastName ='Test', Training_Contact__c = true);
        return c;
    }
    
    public static Project__c insertProject()
    {
        Profile p = [select id from profile where name='OC Impact'];
        UserRole r = [select id from UserRole where name='OC Impact Director'];
        User userSRO  = TestUtilities.createUser(p.Id);
        RecordType rt = [select id from RecordType where SobjectType = 'Project__c' and DeveloperName = 'Project' limit 1];
        string rId = rt.Id;
        Project__c proj = new Project__c(Name = 'testProject',
                                         OC_field__c = 'OC Impact',
                                         Project_summary__c = 'Test Summary',
                                         Project_start_date__c = date.newInstance(2013, 1, 1),
                                         Project_end_date__c = date.newInstance(2013, 12, 25),
                                         RecordtypeId = rt.id,
                                         Submit_for_Approval_by__c = date.newInstance(2018, 12, 25),
                                         Senior_Responsible_Officer_SRO__c = userSRO.Id);
        return proj;                                 
    }
    
    public static Project_Budget__c createProjectBudget(Id projId)
    {
        Project_Budget__c pb = new Project_Budget__c(Name = 'Session',
                                                     Cost_Centre__c = 'Session',
                                                     Income__c = 0,
                                                     Expense__c = 0,
                                                     Project__c = projId);
        return pb;
    }
    
    public static Project_Budget_Detail__c createProjectBudgetDetail(Id projBudgetId)
    {
        Project_Budget_Detail__c pb = new Project_Budget_Detail__c(
                                                     Paid_As__c = 'Cash',
                                                     Income__c = 0,
                                                     Expense__c = 0,
                                                     Project_Budget__c = projBudgetId);
        return pb;
    }
}

I also changed 2 test clases:

1.

/**

 */
@isTest
private class AttendeeControllerTEST {

    static testMethod void testAddClientToSession() {
        
        Clients__c c = TestUtilities.insertClient();
        insert c;
        Project__c p = TestUtilities.insertProject();
        insert p;
        Event e = new Event(Subject='Test',ActivityDateTime=datetime.now(),WhatId=p.Id,DurationInMinutes=10);
        insert e;
        
        Test.setCurrentPageReference(Page.EventAttendees);
        
        AttendeeController con = new AttendeeController();
        apexPages.currentPage().getParameters().put('EventID', e.Id);

        system.assert(con.ClientList.size()==1);
        con.clientList[0].isSelected = true;
        con.SaveAttendees();

    }
    
    static testMethod void testAddClientToAllSessions() {
        
        Clients__c c = TestUtilities.insertClient();
        insert c;
        Project__c p = TestUtilities.insertProject();
        insert p;
        Event e = new Event(Subject='Test',ActivityDateTime=datetime.now(),WhatId=p.Id,DurationInMinutes=10);
        insert e;
        
        Test.setCurrentPageReference(Page.EventAttendees);
        
        AttendeeController con = new AttendeeController();
        apexPages.currentPage().getParameters().put('ProjectId', p.Id);

        system.assert(con.ClientList.size()==1);
        con.clientList[0].isSelected = true;
        con.SaveAttendees();

    }
}

2.
@isTest
private class TESTCLASS_EventTrigger {
static testMethod void validateEventTrigger() {

    string mySubject = 'Test Class Call';
    datetime myDateTime = datetime.newInstance(2012, 1, 1);
    integer myDuration = 30;
   
Clients__c c = TestUtilities.insertClient();
    insert c;
Event e = new Event(Subject=mySubject, WhatID = c.id,
                    DurationInMinutes=myDuration, StartDateTime=myDateTime);
       insert e;

// Retrieve the new Event
e = [SELECT ID, Calendar_Title__c FROM Event WHERE Subject = :mySubject];
System.debug('Calendar_Title__c: ' + e.Calendar_Title__c);
    

// Test that the trigger correctly updated the price
System.assertEquals(c.Name + ': ' + mySubject, e.Calendar_Title__c);
}
}


I think the problem is here: TestUtilities as it only has 8% coverage:
1
2

3
4
5

But I wouldn't have a clue on where to start to increase coverage. It may be something simple that you can help me change. I hope so, anyway :-)

Again, thank you very much.
Himanshu ParasharHimanshu Parashar
If you feel comfortable. I am asking because it is bit difficult to understand the exact issue from these images.
Nitzan MarinovNitzan Marinov
What are you asking for?
Nitzan MarinovNitzan Marinov
Thank you so much, Himanshu. I really appreciate your help today.

Take care
Himanshu ParasharHimanshu Parashar
It's all my pleasure, take care bye.