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
Olavo ZapataOlavo Zapata 

Test Class 100% in Sandbox but 0% coverage in Production

Hi guys,

I need help to deploy an apex trigger I saw that is very common to receive an error about coverage in production.
And I read many posts related to that but I couldn't fix it.

I completely new in apex triggers and deployment. I have a trigger that is working in sandbox and we make a test class to test it.

Code:
@isTest(OnInstall=true)
private class UpdateAccountId2 {

    public static List<Account> accountsList;
    public static List<nKPI__c> kpiList;
    public static String rdstation_id_account1;

    static void init(){
    accountsList = new List<Account>();
    kpiList = new List<nKPI__c>();

    rdstation_id_account1 = '100';

    kpiList.add(new nKPI__c(
                    rdstationid__c = rdstation_id_account1
    ));
    }

    /** Test with an existing account **/
    static testMethod void testWithExistingAccount() {
    init();
    Test.startTest();

    accountsList.add(new Account(
        Name = 'Test',
        rdstationid__c = rdstation_id_account1
    ));
    insert accountsList;
    insert kpiList;


    kpiList = [
        SELECT Id, Account__c
        FROM nKPI__c
        WHERE Id = :kpiList[0].Id
    ];

    // Verification
    System.assertEquals(kpiList[0].Account__c, accountsList[0].Id);
    Test.stopTest();
    }
}
It shows me 100% coverage in sandbox when I run.
But in production it is showing me this message:
Your organization's code coverage is 70%. You need at least 75% coverage to complete this deployment. Also, the following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
UpdateAccountId2
Can anyone help me?
Thanks,


 
Best Answer chosen by Olavo Zapata
Olavo ZapataOlavo Zapata
Hi guys I fixed it.

I don't know why was sending the test and the trigger in different deploys. Maybe my ignorance with apex.
But the solution was sending the Trigger and the Test at the same deploy.

Thanks for helping.

All Answers

Sitarama MurthySitarama Murthy
Hi Olavo Zapata,

paste your trigger as well

Thanks
Olavo ZapataOlavo Zapata
My Trigger:
trigger UpdateAccountId2 on nKPI__c (before update, before insert) {

    Map<String, nKPI__c> tintgreMap = new Map<String, nKPI__c>();
    
    for(nKPI__c ti : trigger.new) {
        
        if(trigger.isInsert || (trigger.isUpdate && ti.RDStationID__c != trigger.oldMap.get(ti.Id).rdstationid__c)) {
            tintgreMap.put(ti.rdstationid__c, ti);
        }
    }

    if(tintgreMap == null) return;
    
    Map<String, Account> accountMap = new Map<String, Account>();
    for (Account account : [Select Id, rdstationid__c From Account Where rdstationid__c = :tintgreMap.KeySet()]) {
        accountMap.put(account.rdstationid__c, account);
    }
    
    if(accountMap == null) return;
    
    for(nKPI__c tiRecord : tintgreMap.Values()) {
        
        tiRecord.Account__c = accountMap.get(tiRecord.rdstationid__c).Id;
        
    }

}
Olavo ZapataOlavo Zapata
Hi guys I fixed it.

I don't know why was sending the test and the trigger in different deploys. Maybe my ignorance with apex.
But the solution was sending the Trigger and the Test at the same deploy.

Thanks for helping.
This was selected as the best answer