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
LaaralLaaral 

Hard code values

Hi, I have a problem with my test class which I'm moving from another sandbox to a different one with different data. Because I've picked up ID:s from cases, setups, contracts from this old environment the new environment doesn't have those. So I was thinking should I hard code these values to the code? How is it done? ( Id marked red on the code)

 

/**
 *
 */
@isTest
private class ServiceAvailablityForCaseATriggerTest {

    static testMethod void addCaseAndSAObjectTest() {
        Account ma = new account();
        ma.Name = 'test account2';
        RecordType recordTypeMa = [ select Id, Name from RecordType where SObjectType = 'Account' and Name = 'Main Account' LIMIT 1];
        ma.RecordTypeId = recordTypeMa.Id;
        insert ma;
        
        Account ac = new account();
        ac.ParentId = ma.Id;
        ac.Name = 'test account2';
        ac.ShippingCity = 'nevada';
        ac.ShippingCountry = 'russia';
        ac.ShippingPostalCode = '70211';
        ac.ShippingState = 'na';
        ac.ShippingStreet = 'testikatu';
        RecordType recordType = [ select Id, Name from RecordType where SObjectType = 'Account' and Name = 'Cost Center' LIMIT 1];
        ac.recordTypeId = recordType.Id;        
        ac.BillingStreet = 'testroad 4';
        ac.BillingPostalCode = '00070';
        ac.BillingCity = 'testcityi';
        ac.BillingCountry = 'testcountryi';
        ac.Address_for_Email_Invoices__c = 'test@videra.com';
        insert ac;
        
        Contract__c cc = new Contract__c();
        cc.Status__c = 'Active';
        cc.Contract_Agreement__c ='800L00000001R8K';
        cc.Main_Account__c = '001L000000HN8Pb';
        
        insert cc;
        
        Setup__c setup = new Setup__c();         
        setup.Name = 'LauranSetup3';
        setup.contract__c = 'a0oL0000000pBof;
        
        insert setup;
 

        Contract contract = new Contract();
        contract.Name = 'test contract';
        contract.Account = ma;
        contract.Business_Hours__c = '01m20000000Gvg7h';
        contract.CurrencyIsoCode = 'EUR';
        contract.Status = 'Negotiation/review';
        contract.StartDate = Date.newInstance(2013, 5, 23);
        contract.ContractTerm = 36;
        contract.OwnerExpirationNotice = '90';
        contract.AccountId = ma.Id;
        
        insert contract;
        
        Case ca = new Case();        
        RecordType recordType2 = [ select Id, Name from RecordType where SObjectType = 'Case' and Name = 'Support' LIMIT 1];
        ca.RecordTypeId = recordType2.Id;
        ca.Related_Setup__c = setup.Id;
        ca.Status = 'New';
        ca.priority = 'Low';
        ca.subject = 'subject1';
        ca.description = 'desc2';
        ca.Satisfaction_Survey_Language__c = 'English';
        
        ca.Fault_Classification__c =  'Major';
        //c.Last_Status_Change__c = System.Now();
        
        
        
       Test.StartTest();
          insert ca;
       Test.StopTest();
        
        List<Service_Availability__c> caseSAs = [select Name, Status__c, Status_explanation__c, Duration__c, Start_DateTime__c from Service_Availability__c
                                                //where Setup__c =: updatedCase.Related_Setup__c
                                                //and Case__c =: updatedCase.Id
                                                where Case__c =: ca.Id
                                                order by Name desc];  
        System.debug('ADD CASE: CASE SA count: ' + caseSAs.Size());        
        
        System.assert(caseSAs.size() == 1);
        System.assert(caseSAs[0].Duration__c == 0);
        
        List<Service_Availability__c> setupSAs = [select Name, Status__c, Status_explanation__c, Duration__c, Start_DateTime__c from Service_Availability__c
                                                where Setup__c =: ca.Related_Setup__c
                                                //and Case__c =: updatedCase.Id
                                                //where Case__c =: updatedCase.Id
                                                order by Name desc];  
        System.debug('ADD CASE: SETUP SA count: ' + setupSAs.Size());        
        
        System.assert(setupSAs.size() == 1);
        System.assert(setupSAs[0].Duration__c == 0);
        
        
        /*
        Contract contract = new Contract();
        contract.Name = 'test contract';
        contract.Account = ma;
        contract.CurrencyIsoCode = 'EUR';
        contract.Status = 'Negotiation/review';
        contract.StartDate = Date.newInstance(2013, 5, 23);
        contract.ContractTerm = 36;
        contract.OwnerExpirationNotice = '90';
        contract.AccountId = ma.Id;
        
        insert contract;
        */       
    }
}

Satish_SFDCSatish_SFDC
Hard Coding is generally not a best practice.
Instead you can issue soql queries to dynamically get the data you need and use this data in your code.

More Information:
http://wiki.developerforce.com/page/Apex_Code_Best_Practices#Best_Practices_.2310:_Avoid_Hardcoding_IDs

Regards,
Satish Kumar
Satish_SFDCSatish_SFDC
In your case, for eg, you have hardcoded certain fields of the Contract object.
Instead of hardcoding you can issue an SOQK query to get the same ID's dynamically.
So irrespective of the instance, your code will always work.

Regards,
Satish Kumar