0

I'm deploying a simple class+test from sandbox to production. The test is successful on test but fails on deploy as stated on the title. I've checked validation rules, process builder flows and workflows or filters. Didn't detected a diff between both environments there. The error it's on this line :

System.AssertException: Assertion Failed: Expected: 1, Actual: 0 Stack Trace: Class.SchedulableCheckContractsTest.itShould: line 32, column 1

On this line :

System.assertEquals(1,[SELECT COUNT() FROM Invoice__c]);

Just to provide a bit of context we have a Process that creates a contract when an opportunity reaches 'Subscription Booked' StageName and this is both activated and identical on sandbox and prod.

Now what i'm deploying is a Schedulable+Batchable class that checks all contracts and creates an invoice if the month corresponds with an invoice month.

Apex Class (deleted debug statements and post insert mailing functionalities to save space)

global class SchedulableCheckContracts implements Database.Batchable<sObject>,Schedulable {

    global void execute(SchedulableContext sc) {
        Database.executeBatch(new SchedulableCheckContracts());
    }

    public Iterable<sObject> start(Database.Batchablecontext BC){
        Map<id,Contract> MapContracts = new Map<id,Contract> ([SELECT id,Amount_invoice__c,Accountid,EndDate,StartDate,Billing_Agreement_Months__c,ContractTerm FROM Contract WHERE EndDate > Today]); 
        for(Contract currentContract: MapContracts.values()) {
            if(!isInvoiceTime(currentContract)) MapContracts.remove(currentContract.id);
        }
        return new List<Contract> (MapContracts.values());
    }

    public void execute(Database.BatchableContext BC, List<sObject> scope){  
        List <Invoice__c> invoiceToInsert = new List <Invoice__c> ();
        for(Contract currentContract: (List <Contract>) scope) {
            invoiceToInsert.add(new Invoice__c(Date_Invoice__c = System.today(),
                                               Contract__c = currentContract.id,
                                               Account__c = currentContract.Accountid,
                                               Type__c = 'Subscription Invoice',
                                               Amount__c = currentContract.Amount_invoice__c)); // TODO declare invoice fields 
        }
            Database.SaveResult[] srList = Database.insert(invoiceToInsert,false);

    }

    public void finish(Database.BatchableContext info){

    }

    // This function receives a contract and based on the current day, determines if we have to generate an invoice for the current month.
    public Boolean isInvoiceTime(Contract C){
        return Math.mod(Math.abs(C.startDate.month()-System.today().month()),(Integer)C.Billing_Agreement_Months__c) == 0;
    }
}

Apex Test Class

@isTest
private class SchedulableCheckContractsTest
{
    static void test1()
    {
        TestDataFactory.insertGeckoboardSettings();
        TestDataFactory.insertOutboundMessageSettings();    
        Account a = TestDataFactory.createAccount('a');
        A.Account_Status__c = 'Approved';
        insert a;
        Opportunity O = new Opportunity(Effective_Date__c = System.today().addDays(-1),CloseDate = System.today(),Accountid = a.id,Amount = 100000, Name='test A',StageName='Stage 3: Business Discovery',Type='Spot');
        insert O;
        O.StageName = 'Subscription Booked';
        O.Fee__c = 0.29;
        O.Contract_term__c = '12';
        O.Billing_Agreement__c = 'Monthly';
        O.Monthly_FX_Service_Package_Fee__c  = 500;
        O.Monthly_Subscription_Revenue__c = 500;
        O.Subscription_Package__c  = 'SW Subscription - Enterprise';
        update O;
        System.assertequals(1,[SELECT COUNT() FROM Contract]);
        Contract C = [SELECT id,endDate FROM Contract LIMIT 1];
        C.EndDate = System.today().addMonths(1);
        update C;
        Test.startTest();
        SchedulableCheckContracts SCC = new SchedulableCheckContracts();
        Database.executeBatch(SCC);
        Test.stopTest();
        System.assertEquals(1,[SELECT COUNT() FROM Invoice__c]);
    }
}

After all the morning trying to find out what the diffs are between both environments which is the only reason I think to explain why this is happening I found no match yet and would like to know what are the most probable things that are causing this Assertion to fail or if there's something i'm missing. Any criticism related to the code is always welcome too.

6
  • put debug here Database.SaveResult[] srList = Database.insert(invoiceToInsert,false); and see if record get inserted because if you get any error then it will not create record. Jul 25 '16 at 10:52
  • The problem is when I try to debug on prod deployment I go to the log and can't find anything relative to this parts of code or debug statements if I do it from Eclipse IDE and Can't see the logs of the test executions of the deployment if I deploy using Salesforce standard UI. I will try this after recheck I have all the debug log settings settled up correctly. And anyway I still don't get why would the record get inserted on sandbox and not on prod if the triggers are the same, process are the same and the class & test are obviously the same :S Jul 25 '16 at 11:00
  • Check for validation, triggers, Process builder they might be the issue. Jul 25 '16 at 11:29
  • Try manually creating an Invoice__c record in Production that meets the same criteria as the way you are creating it in your code. See if you can successfully insert that record with just those values. If you can manually insert the record using those same values/same flow manually, at least you know at that point that its not a VR/WFR/Proc causing a save rollback. Jul 25 '16 at 11:38
  • Verified Process builder, Validation rules and Workflows are the same. Triggers Involving Accounts & Opportunities are also identical on both environments. And Contracts & Invoices don't have triggers. I'm so lost... Jul 25 '16 at 12:21
1

The insert wasn't really performed on prod due to the field Invoice_reference__c being required unlike on Test where it wasn't and I was forgetting to put on the insert inside the batch... And since no Exception was thrown The code didn't stopped worked till the Assertion.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.