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
Mahmoud Coudsi 1Mahmoud Coudsi 1 

How to delete this Apex class from production? URGENT

Hi,

I have deployed this Apex Testing class in production that messed up my whole production org; it got to a point where I'm not able to deploy anything on production nor able to delete it (the Apex class that's causing the issue). 

Here what I did to delete test class:

1. I download Eclipse and insalled the Foce.com IDE.
2. Then I created Foce.com two projects; one includes all my master production org components and one includes all of my sandbox components.
3. After that I went to sandbox project and altered my class file.xml to "Deleted" and changed four of my triggers (tied to the class, not needed anymore) to "Inactive"
4. I attempted to deploy the five components (referenced above) to production in order to delete trigger and deactive class but I failed. Got this error message and coverage results:
 
*** Deployment Log ***
Result: FAILED
Date: October 17, 2017 5:35:32 PM PDT

# Deployed From:
   Project name: Inbody_replacing
   Username: mahmoud@enarahealth.com.sandbox
   Endpoint: test.salesforce.com

# Deployed To:
   Username: mahmoud@enarahealth.com
   Endpoint: login.salesforce.com

# Deploy Results:
   File Name:    classes/InsertInbodyData_TestClass.cls
   Full Name:  InsertInbodyData_TestClass
   Action:  UPDATED
   Result:  SUCCESS
   Problem: n/a

   File Name:    package.xml
   Full Name:  package.xml
   Action:  UPDATED
   Result:  SUCCESS
   Problem: n/a

   File Name:    triggers/GetInitialPBFValue.trigger
   Full Name:  GetInitialPBFValue
   Action:  NO ACTION
   Result:  SUCCESS
   Problem: n/a

   File Name:    triggers/GetInitialSMMValue.trigger
   Full Name:  GetInitialSMMValue
   Action:  NO ACTION
   Result:  SUCCESS
   Problem: n/a

   File Name:    triggers/GetLatestPBFValue.trigger
   Full Name:  GetLatestPBFValue
   Action:  NO ACTION
   Result:  SUCCESS
   Problem: n/a

   File Name:    triggers/GetLatestSMMValue.trigger
   Full Name:  GetLatestSMMValue
   Action:  NO ACTION
   Result:  SUCCESS
   Problem: n/a

# Test Results:

Run Failures:
  InsertInbodyData_TestClass.UpdateBIA System.DmlException: Update failed. First exception on row 0 with id a076100000HLRd3AAH; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
User-added image

Here is the code for Apex class (InsertInbodyData_TestClass) and one of the triggers (GetInitialPBFValue) (The triggers logic is identical across all four triggers).
 
Trigger GetInitialPBFValue on Inbody__c(after insert, after update) {
    Set<Id> accountIds = new Set<Id>();

    for (Inbody__c inbdy : trigger.new) {
        accountIds.add(inbdy.Patient__c);
    }

    //Elimnate the the accounts that don't have IDs for
    accountIds.remove(null);

    //SOQL query that returns that latest weight value 
    if (!accountIds.isEmpty()) {
        List<Account> accountsToUpdate = new List<Account>();
        for (Account account : [
            Select Id,
                (
                    SELECT Test_Date_Time__c, Percent_Body_Fat__c
                    FROM Inbody__r
                    WHERE Percent_Body_Fat__c != NULL
                    ORDER by Test_Date_Time__c asc
                    Limit 1
                )
            From Account
            Where Id in :accountIds
        ]) {
           
            //Declare a decimal variable to store latest weight value 
            Decimal IPBF = NULL;
            
            // Get(0) to return the first element in the list value
            if (!account.Inbody__r.isEmpty()) {
                IPBF = account.Inbody__r.get(0).Percent_Body_Fat__c;
            }

            accountsToUpdate.add(new Account(
                Id = account.Id,
                initial_PBF_Value__c = IPBF
               
            ));
                         
        }   
        
        Update accountsToUpdate;
        
        }
      }
GetInitialPBFValue Trigger:
@isTest(SeeAllData=true)
Private class InsertInbodyData_TestClass {

    @isTest static void InsertInbody() {
        Account accnts = new Account();
        Inbody__c BIA = new Inbody__c();
        
        RecordType rt = [SELECT ID,Name FROM RecordType WHERE SobjectType='Account' and Name='Patients' Limit 1];
        
        accnts.name = 'afagas';
        accnts.RecordTypeID = rt.id;
        
        insert accnts;
        
        BIA.Skeletal_Muscle_Mass__c = 200;
        BIA.Percent_Body_Fat__c = 160;
        BIA.Test_Date_Time__c = Date.today();
        BIA.patient__c = accnts.id;
        
        insert BIA;
               
        }
        
    @isTest static void UpdateBIA() {
        Inbody__c BIA = new Inbody__c();
        BIA.id = 'a076100000HLRd3';
        BIA.weight__c = 100;
        BIA.Test_Date_Time__c = date.today();
        BIA.patient__c = '0016100000V5qTw';
        
        update BIA;   
        
        }
     
}
This issue had became a HUGE bottleneck in my development process, any input would be greatly appreciated!





 
Tad Aalgaard 1Tad Aalgaard 1
Those hard coded IDs probably only exist in the dev sandbox you worked in and do not exist in the prod sandbox.  If you paste those ID's on your prod URL on the browser do they exist on that system?

Hard coding ID's in your code is a very bad practice.  You should query for records instead and then get the ID from the results and use them in your code instead.

Remove or comment out all the code inside the class structure and redeploy to prod.
Mahmoud Coudsi 1Mahmoud Coudsi 1
@Tad Aalgaard 3

Yes, I learned it the hard way that hardcoding is bad practice. After several trial I was able to finally delete the class. That's good for now

Now, I'm trying fix the code in the test class and redeploy it again. How can I code it in a way that satisfy the coverage requierment at the same time doesn't be hardcoded? 

Thanks,
Mahmoud.
Tad Aalgaard 3Tad Aalgaard 3
Reuse the code in InsertInbody in UpdateBIA to create your records then change something in BIA and update BIA.

Also, while this may give you test coverage, it is not really doing any testing.  You need to add some SOQL queries and asserts after the insert and updates to make sure the GetInitialPBFValue trigger is doing what you want it to do.  Code coverage is just half of a test classes responsibility.