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
Larry LeonidasLarry Leonidas 

Test Coverage 89% Not Working

I'm trying to Upload the trigger below into our production environment and receiving the errors below. Test Coverage for the trigger is 85%. Not sure why I would get the errors below:

 

 

 

trigger ExpenseReportUpdate on Expense__c (before insert) {

Expense__c exp = Trigger.new[0];

date trx_d = exp.date__c;
Integer trx_m = trx_d.month();
Integer trx_y = trx_d.year();
String exname = exp.Employee_First_Name__c + ' ' + exp.Employee_Last_Name__c;
String exname_id;
String expR_id;
date trx_st = trx_d.toStartOfMonth();
date stdate = trx_st.addmonths(1);

    try {
        exname_id = [SELECT id
                    FROM User
                    WHERE name = :exname].id;
        }
        
    catch (QueryException e) {
        exname_id = [SELECT id
                    FROM User
                    WHERE name = 'Force Admin'].id;
        }
          
    try { 
    
        expR_id = [SELECT id
            FROM Expense_Report__c
            WHERE CALENDAR_MONTH(Statement_Date__c) = :trx_m
            AND CALENDAR_YEAR(Statement_Date__c)= :trx_y
            AND Employee__c = :exname_id].id; 
            
        exp.Expense_Report__c = expR_id;
       
        }
                    
    catch (QueryException e) {
            Expense_Report__c newER = new Expense_Report__c(Employee__c = exname_id, Statement_Date__c = stdate);
            insert newER;
            
            exp.Expense_Report__c = newER.id;       
    }
    
}

 

 

ERRORS I GET:

MyProfilePageController.testSave()            Failure Message: "System.QueryException: List has no rows for assignment to SObject", Failure Stack Trace: "Class.MyProfilePageController.testSave: line 78, column 35 External entry point"

"not even referencing this?"

ExpenseReportUpdateTest  Coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

"coverage is 89%?"

Deploy Error Average test coverage across all Apex Classes and Triggers is 66%, at least 75% test coverage is required

"not sure why this is happening"

Best Answer chosen by Admin (Salesforce Developers) 
bryan.gilbertbryan.gilbert

Hi

 

The problem is likely due to the save error on MyProfilePageController.  I posted a solution here:

Hi

 

I had the same problem and posted the solution in this topic:

http://boards.developerforce.com/t5/Apex-Code-Development/Error-while-deploying-to-production/m-p/24...

All Answers

Lucy YuanLucy Yuan

hi

please see my  comment .

 

trigger ExpenseReportUpdate on Expense__c (before ​insert) {

Expense__c exp = Trigger.new[0];

date trx_d = exp.date__c;
Integer trx_m = trx_d.month();
Integer trx_y = trx_d.year();
String exname = exp.Employee_First_Name__c + ' ' +​ exp.Employee_Last_Name__c;
String exname_id;
String expR_id;
date trx_st = trx_d.toStartOfMonth();
date stdate = trx_st.addmonths(1);

    try {
        exname_id = [SELECT id
                    FROM User
                    WHERE name = :exname].id;   //  This query  return a list<User> not a string.! 
        }
        
    catch (QueryException e) {
        exname_id = [SELECT id
                    FROM User
                    WHERE name = 'Force Admin'].id​;  // i guess you don't need to query in here . just use constom label
        }
          
    try { 
    

         // problem same with first query ! if you just  want to query  one record, you can use "LIMIT" statement! 
        expR_id = [SELECT id
            FROM Expense_Report__c
            WHERE CALENDAR_MONTH(Statement_Date__c​) = :trx_m
            AND CALENDAR_YEAR(Statement_Date__c)= ​:trx_y
            AND Employee__c = :exname_id].id;                         
            
        exp.Expense_Report__c = expR_id;
       
        }
                    
    catch (QueryException e) {
            Expense_Report__c newER = new Expense_​Report__c(Employee__c = exname_id, Statement_Date_​_c = stdate);

 

 

            // here may throw DMLException !


            insert newER;
            
            exp.Expense_Report__c = newER.id;     ​  
    }
    
}

bob_buzzardbob_buzzard

How are you trying to deploy the trigger into production - change set, from eclipse etc?  Are you just deploying the trigger and its test class or any additional code?

 

The error that you are seeing looks like a sample controller that I've seen in the past for for partner portals.  It smacks that when you are deploying, all unit tests in the org are running and one of them is failing.  Have you tried running the unit tests in your live org from the UI to see if you get any errors?

Larry LeonidasLarry Leonidas

Lucy,

 

Yes missing the DML exception. Will definitely address that.

 

I thought dereferencing (.id) would limit results to 1? Is that not best practices. If not, please let me know how the LIMIT statement would fit into this code.

Larry LeonidasLarry Leonidas

Bob_Buzzard,

 

ISSUE 1:

 

The MyProfilePageController error is a mystery.

 

I think this MyProfilePageController somehow was created/appended when Salesforce enabled a trial of PartnerPortal for us. We since don't use partner portal, at least for now, and therefore don't have any Partner Portal users. I believe the issue is that upon Upload of the myChangeSet into Production an ALL TESTS is run, and this controller is FAILING.

 

To fix, I've included an appended version of MyProfilePageController in my ChangeSet and commented out the failling code, after careful review of its necessity to us.

 

ISSUE 2:

 

The reason for no TestCoverage was an amateur mistake on my part. I didn't include my TestController for this Trigger as part of the ChangeSet. I did this and it resolved that error.

 

ISSUE 3:

 

The overall coverage was below 75%. After revewiing an ALL TESTS Run in the Sandbox it seems many of the Packages from the APPExchange have poor code coverage, and is affecting our overall CodeCoverage. I am currently targetting removing the MyFAX package and hoping this will improve the overall coverage to be over 75%.

 

My question here is, don't APPEXCHANGE partners have to meet codecoverage requirements before publishing? Am I missing something here?

bob_buzzardbob_buzzard

Certainly my experience with managed packages is that I can't upload a version unless I have satisfied the standard unit tests requirements of all classes > 75% and triggers at least some coverage.  That said, it is possible to install a package with failing unit tests etc (for example, due to data that already exists in the org) via a checkbox at installation time.  

 

I'm not sure about managed packages or if this has always been the case though.

 

 

bryan.gilbertbryan.gilbert

Hi

 

The problem is likely due to the save error on MyProfilePageController.  I posted a solution here:

Hi

 

I had the same problem and posted the solution in this topic:

http://boards.developerforce.com/t5/Apex-Code-Development/Error-while-deploying-to-production/m-p/24...

This was selected as the best answer
srvassrvas

please send coverage for this code

(srvas137@gmail.com)

 

 

public with sharing class accdata {
public accdata(ApexPages.StandardController controller) {

}

 


public List<Account> acc { get; set; }
public list<student__c> stu { get; set; }
public string recordid{ get; set; }

public accdata()
{
acc=[select id ,name,fax from account limit 100];
stu=[select id,name,phone__c from student__c limit 100];
}

public PageReference recEdit()
{
pageReference ref=new pageReference('/'+recordid+'/e');
return ref;
}


public PageReference recDel()
{

account acc=[select id from account where id=:recordid];
delete acc;

pageReference ref =new pageReference('/apex/displaydata');
ref.setRedirect(true);
return ref;

}


public PageReference actStu()
{
PageReference ref=new PageReference('/'+recordid+'/e');

return ref;
}

public PageReference studel() {

student__c stu=[select id,batch__c from student__c where id=:recordid];




delete stu;



pageReference ref=new pageReference('/apex/displaydata');
ref.setRedirect(true);
return ref;
}

}