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
Kent ManningKent Manning 

How do I activate a Contract with Apex code?

I have a test class for a trigger that essentally creates a 'test' contract.  Once the contract is activated my trigger copies the pertainate infromation to a custom object.   My test code method is this:

@isTest(SeeAllData=true)
public class CopyContractInfo_Test{
    public static testMethod void testCaseInstrument_A(){
    	List<Case_instrument__c> ciResult;
        Case_Instrument__c ci1 = new Case_instrument__c (case__c = myObjectFactory.insertCase().id,Instrument__c = 'xxxx', Name = '.', Asset__c=myAssetFactory.insertAssetB().id  );
        
        myObjectFactory.insertContract2y(); /* calls my helper class called myObjectFactory */
        
        test.startTest();
        	insert ci1;
        test.stopTest();
     
/* system assert tests */ }

 

This is the myObjectFactory helper class. 

public static Contract insertContract2y(){ Contract testContract = new Contract(); testContract.AccountId = myAssetFactory.insertAccount().id; testContract.recordTypeId = '0126000000018Z2AAI'; testContract.Name = 'Case Instrument Contract Test'; testContract.Location__c = 'the factory'; testContract.Product__c = insertProduct().id; testContract.Contract_Purchase_Price__c = 100.00; testContract.Bio_Inst_Serial_Number__c = myAssetFactory.insertAsset().id; testContract.StartDate = System.today()+10; testContract.ContractTerm = 24; testContract.Status = 'Activated'; <---------------------------- error occurs here. testContract.Contract_type__c = '2-year Service Contract'; testContract.Free_ChamberMaintenance_Allowed__c = 0; testContract.Free_Calibration_s_Allowed__c = 1; testContract.Customer_Visit_Allowed__c = 0; insert testContract; return testContract; }

  I tried to change the status to activated on the contract object, but here is what comes back:  System.DmlException: Insert failed. First exception on row 0; first error: INVALID_STATUS, invalid status: []  If I set the status line to "draft"  everything is happy.  

 

So how do I "activate a contract" so that this test code will work properly?

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

Well you need to create a Contract in In "Draft" status and then update it to "Activated".

 

 

As per SF Docs "Client applications must initially create a Contract in a non-Activated state. Client applications can subsequently activate a Contract by updating it and setting the value in its Status field to Activated; however, the Status field is the only field you can update when activating the Contract."

 

So try this

 

public static Contract insertContract2y(){
        Contract testContract = new Contract();
        testContract.AccountId = LicorAssetFactory.insertAccount().id;
        testContract.recordTypeId = '0126000000018Z2AAI';
        testContract.Name = 'Case Instrument Contract Test';
        testContract.Location__c = 'the factory';
        testContract.Product__c = insertProduct7000().id;
        testContract.Contract_Purchase_Price__c = 100.00;
        testContract.Bio_Instrument_Serial_Number__c = LicorAssetFactory.insertAsset7000().id;
        testContract.StartDate = System.today()+10;
        testContract.ContractTerm = 24;
        testContract.Status = 'Draft'; 
        testContract.Contract_type__c = '2-year Service Contract'; 
        testContract.Free_8100ChamberMaintenance_Allowed__c = 0;
        testContract.Free_Calibration_s_Allowed__c = 1;
        testContract.Customer_Visit_Allowed__c = 0;
        insert testContract;
testContract.Status = 'Activated';
update testContract; return testContract; }