+ Start a Discussion
Olavo ZapataOlavo Zapata 

Error in the Apex Test to insert a register in Contract (Invalid Status)


Hi all,
I'm having a hard time running an Apex Test for a trigger that I'm trying to use.
The trigger basically matches the External ID of a Custom Object with the same External ID from Contract ID, to bring me the Account ID in the Custom Object.

What Test needs to do is test this trigger, so I can put it into production. But is giving me the following error:
System.DmlException: Insert failed. First exception on row 0; first error: FAILED_ACTIVATION, Choose a valid contract status and save your changes. Ask your admin for details.: [Status]
Class.UpdateAccountId2.testWithExistingAccount: line 31, column 1

The trigger and the Test are the following:
/** # New Version of nKPI Trigger **/
trigger UpdateAccountId2 on nKPI__c (before update, before insert) {

    Map<String, nKPI__c> tintgreMap = new Map<String, nKPI__c>();
    
    for(nKPI__c ti : trigger.new) {
        
        if(trigger.isInsert || (trigger.isUpdate && ti.RDStationID__c != trigger.oldMap.get(ti.Id).rdstationid__c)) {
            tintgreMap.put(ti.rdstationid__c, ti);
        }
    }

    if(tintgreMap == null) return;
    
    Map<String, Contract> accountMap = new Map<String, Contract>();
    for (Contract account : [Select AccountId,externalCode__c FROM Contract WHERE Product_Group__c = 'RD Station' AND externalCode__c = :tintgreMap.KeySet()]) {
        accountMap.put(account.externalCode__c, account);
    }
    
    if(accountMap == null) return;
    
    for(nKPI__c tiRecord : tintgreMap.Values()) {
        
        tiRecord.Account__c = accountMap.get(tiRecord.rdstationid__c).AccountId;
        
    }

}
--------------------------------------------------- TEST

@isTest(OnInstall=true)
private class UpdateAccountId2 {

    public static List<Contract> contractsList;
    public static List<nKPI__c> kpiList;
    public static String rdstation_id_account1;

    static void init(){
    contractsList = new List<Contract>();
    kpiList = new List<nKPI__c>();

    rdstation_id_account1 = '100';

    kpiList.add(new nKPI__c(
                    rdstationid__c = rdstation_id_account1
    ));
    }

    /** Test with an existing account **/
    static testMethod void testWithExistingAccount() {
    init();
    Test.startTest();

    contractsList.add(new Contract(
        Name = 'Test',
        internalid__c = '1000001',
        Status = '0 - Trial',
        externalCode__c = rdstation_id_account1
    ));
    insert contractsList;
    insert kpiList;
    
    kpiList = [
        SELECT Id, Account__c
        FROM nKPI__c
        WHERE Id = :kpiList[0].Id
    ];

    // Verification
    System.assertEquals(kpiList[0].Account__c, contractsList[0].Id);
    Test.stopTest();
    }
}

Anyone could identify what the error could be? I'm a beginner on this apex things.
Thanks.
 
Best Answer chosen by Olavo Zapata
Steven NsubugaSteven Nsubuga
The error is in the value you are supplying to the status field of Contract. The status '0 - Trial' is not valid. Try 'Draft'

All Answers

Colin KenworthyColin Kenworthy
When you create the new Contract (line 54 above) shouldn't you be setting the AccountId and Product_Group__c fields too ?
Also that means inserting a new Account in order to get an AccountId.
Olavo ZapataOlavo Zapata

Hi Colin,

You mean like this in line 54:

contractsList.add(new Contract(
        Name = 'Test',
        internalid__c = '1000001',
        Status = '0 - Trial',
        AccountId = '0010U00000PVubPQAT',
        Product_Group__c = 'RD Station',
        externalCode__c = rdstation_id_account1
    ));
I tried in this way as well and get the same error.
 
Steven NsubugaSteven Nsubuga
The error is in the value you are supplying to the status field of Contract. The status '0 - Trial' is not valid. Try 'Draft'
This was selected as the best answer
Olavo ZapataOlavo Zapata
Hi Steven,
Thanks for your help, but I tried:
Look these are my Contract's Status:
Contract Status
Steven NsubugaSteven Nsubuga
Try using 1 of the values in the Draft Status category. Create a new one or test with maybe 1 of the 5 you have.
 
Olavo ZapataOlavo Zapata
Hi Steven,

My friend now works! So, always I need to create a contract through the Apex I need to do with one status of the Draft category?

Thanks for your help!