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 How to delete 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: []
Deployement coverage results:

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!
 
Arp_SFDCArp_SFDC
Hi Mahmoud,

In your code you have written hardcode id of patient(BIA.patient__c = '0016100000V5qTw';), Please check that record is exist or not OR try to create test data in apex class by setting seeAllData to false.

Avoid to use hardcode id.

Thanks
Arpit 

 
Abdul KhatriAbdul Khatri
I hope following thread help you what you are looking for. If does please mark it a best answer

https://developer.salesforce.com/forums/?id=906F00000008yIyIAI
Mahmoud Coudsi 1Mahmoud Coudsi 1
@arp_SFDC 

Thanks for the quick turnaround. I just verified that 0016100000V5qTw account does exist in my production org AND sandbox.

Not sure what you mean by create a test data in apex class by setting seeAllData to false. Can you give me guided instructions?
 
Mahmoud Coudsi 1Mahmoud Coudsi 1
@arp_SFDC 

I just tried again to delete the class and it's finally worked!! You mentioned above that I should avoid to use hardcode id. How would you rewrite this Apex class without using hardcoded id? 

Your help would be greatly appericated!

Thanks,
Mahmoud.
 
Arp_SFDCArp_SFDC
Hi Mahmoud,

As You have used hardcoded id,Sometime record is deleted by mistake which break our functionaliy Which is not a good practice.

SeeAllData = true means, when our test class run then data is reterieve from org.
Always try to use SeeAllData = false means tried to create test data in test class itself .untill unless not required.

Please see link for seeAllData Understanding.
https://developer.salesforce.com/forums/?id=906F0000000D9guIAC

How create test data in test class without using hardcoded id, Please see this link 
https://trailhead.salesforce.com/en/modules/apex_testing/units/apex_testing_data

If you need more help let me know.

Kindly mark this as best answer if it's help.

Thanks
Arpit
Abdul KhatriAbdul Khatri
I am not sure where you at for this if you still having issues but done. Still try to help you out with the following code

Please never use SeeAllData = true, so removing that and updating by removing hard coded values

Please follow the same process in all your classes and you should be good.
 
@isTest
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() {
        
        RecordType rt = [SELECT ID,Name FROM RecordType WHERE SobjectType='Account' and Name='Patients' Limit 1];
        Account accnts = new Account();
        accnts.name = 'afagas';
        accnts.RecordTypeID = rt.id;
        insert accnts;        
        
        Inbody__c BIAInsert = new Inbody__c();
        BIAInsert.Skeletal_Muscle_Mass__c = 200;
        BIAInsert.Percent_Body_Fat__c = 160;
        BIAInsert.Test_Date_Time__c = Date.today();
        BIAInsert.patient__c = accnts.id;
        insert BIAInsert;
               
        Inbody__c BIA = new Inbody__c();
        BIA.id = BIAInsert.Id;
        BIA.weight__c = 100;
        BIA.Test_Date_Time__c = date.today();
        BIA.patient__c = accnts.Id;
        
        update BIA;   
        
     }
     
}