I have this with 100% code coverage in sandbox and am trying to deploy to production via change set. When validating this method with test class in production it is showing only 70% code coverage ( when in sandbox it is 100% ) and also it is saying that my return is empty.

    public static String getPreviousContractStatus(Id parentId) {

        // create new list of all previous Contract amounts
        List<String> oldValueList = new List<String>();

        //query for OldValue of Contract Status
        List<Contract__History> oldValuesContractStatus = [
                SELECT OldValue
                FROM Contract__History
                WHERE Field = 'Contract_Status__c'
                AND ParentId =: parentId
                ORDER BY CreatedDate DESC

        // Remove Approval Requested value from list as it always will be OldValue
        // when using Close Contract button
        if(oldValuesCommitStatus.size() > 0){
            for(Contract__History cH : oldValuesContractStatus){
                if(cH.OldValue != 'Approval Requested') {

        return oldValueList[0];


In test class I am basically just calling this method with contract__c parentId and it is giving 100% coverage. BUT when trying to validate this into production it shows error on the

        return oldValueList[0];

that this is empty. What can I do to deploy this via change set and get it to return an element? My guess is that while it is a change set it cannot access the values of field history until it actually is in the destination org.

  • What does the test class look like? It did some digging and it looks like unit test for field history are tricky. Maybe this thread will be useful for you: salesforce.stackexchange.com/questions/4007/… – adriancg May 19 at 14:10
  • test class just called the method with a contract record.. . and no I read that thread about 10x times and it helped but does not saying anything about Deploying to a new org. My guess is in destination org's validation it does not allow the method to see field history of the fields in Prod. how can I get around this deploying via change set? – Tom Jacobson May 19 at 14:14
  • Are you enabling field history tracking in prod in this same deployment? Maybe there's no field history? If not maybe Id add something to have it return a string (for now): if(Test.isRunningTest() && oldValueList.isEmpty()) oldValueList.add('Workaround to return something') I'm not sure if you could manually instantiate a History record and add it to the list of records to have the for loop run and get coverage there. – adriancg May 19 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.