-2

This is driving me nuts...

My trigger has 42% coverage in my sandbox but it won't deploy saying that the coverage is 0% (code below)

The test method both inserts and updates an opportunity. I'm lost as to why the trigger never fires.

Things I've tried so far...

  • I tried a creating new trigger and deactivating the old one.
  • The trigger's system version was 18 - I updated it to 40.
  • I tried implementing the changes in a brand new sandbox.

All to no avail

Please help save my sanity...

Thanks,

Here's my test code....

@isTest
private class paymentManager_test {

    @isTest static void test_addingPayments() {
        id acctRT = lpTools.getRecordTypeID('Account', 'Business');
        id oppRT = lpTools.getRecordTypeID('Opportunity', 'Donation');
        account a = new account(name = 'test', recordtypeid=acctRT);
        insert a;
        opportunity o = new opportunity(name='blah', accountid=a.id, closedate=date.today(), stagename='Pledged', amount=47, recordtypeid = oppRT);
        insert o;
        o.stagename = 'Posted';
        update o;
    }
} 

And my trigger....

trigger opportunityTrigger on Opportunity (before insert, before update, before delete, after insert, after update, after delete, after undelete) {

    set<ID>         cIds    = new set<ID>();
    List<String>    apIds   = new List<String>();
    List<ActionPlan__c> deletePermantently_apIds= new List<ActionPlan__c>();        

    // Add payments to newly closed opps
    if (trigger.isAfter){
        list<Opportunity> inNeed = new list<Opportunity>();
        if (trigger.isInsert) {
            for (Opportunity is : trigger.new) { 
                if (is.stagename == 'Posted' && is.amount > 0) inNeed.add(is);
            }
        }
        else if (trigger.isUpdate){
            for (Opportunity is : trigger.new) {
                Opportunity was = trigger.oldmap.get(is.id);
                if (is.stagename == 'Posted' && is.amount > 0 && is.Num_Payments__c == 0 && (was.stagename != 'Posted' || was.amount == null || was.amount == 0)) inNeed.add(is);
            }
        }
        paymentManager.addPayments(inNeed);
    }


    // Delete related action plans
    if ( trigger.isBefore && trigger.isdelete ){
        for( Opportunity o : trigger.old ){
            cIds.add( o.Id );
        } 

        /* GET Action Plans to delete from recycle bin */
        deletePermantently_apIds = [ select Id, Name , LastModifiedDate from ActionPlan__c where Opportunity__c in : cIds and isDeleted = true ALL ROWS ];

        if ( deletePermantently_apIds.size() >0 ){          
            Database.emptyRecycleBin(deletePermantently_apIds);
        }

        List<ActionPlan__c> apList =  [ select Id from ActionPlan__c where Opportunity__c in : cIds ];
        for( ActionPlan__c ap : [ select Id from ActionPlan__c where Opportunity__c in : cIds ] ){
            apIds.add( ap.Id );
        }
        if ( apIds.size() >0 ){         
            ActionPlansBatchDelete aPBatch = new ActionPlansBatchDelete(apIds, Userinfo.getUserId());
            Database.ExecuteBatch( aPBatch );   
        }       
    }

    // Undelete related action plans
    if ( trigger.isAfter && trigger.isUnDelete ){
        for( Opportunity o : trigger.new ){
            cIds.add( o.Id );
        }
        list <ActionPlan__c> aPs = [ select Id from ActionPlan__c where Opportunity__c in : cIds ALL ROWS ];

        try{
            if(ActionPlanObjectTriggerTest.isTest){
                //throw dmlException
                insert new Contact();   
            }
            //undelete aPs;
            Database.undelete( aPs,false);
        } catch ( Dmlexception e ){
            for (Opportunity o: trigger.new){
                o.addError('You can not undelete an action plan whose related object is deleted.');
            }
        }
    }
}
  • A couple of things you have to take care: (1) Are you deploying the trigger and its associated test code in one go(in changeset) (2) Is your test class throwing an exception?.If its so then you have to Identify that error and fix it and deploy it again. – SFDC FAN Nov 30 '17 at 8:32
  • is the trigger inactive? Does your "test" class fail? – Novarg Nov 30 '17 at 10:06
  • Try deploy only the trigger/class to make sure whether you package isn't being corrupted - Honestly doesn't make sense, but you should give a try. Also I'd advise you add at least an AssertEquals method. – Carlos Nov 30 '17 at 14:22
  • 1
    You need to have System.asserts in your unit tests to confirm that you code does what you expect. Also, 42% is pretty low, you really should test more use cases. Deploying with low coverage will guarantee headaches down the road. – David Cheng Nov 30 '17 at 20:29
0
////////if (trigger.isAfter){////////
        list<Opportunity> inNeed = new list<Opportunity>();
        if (trigger.isInsert) {
            for (Opportunity is : trigger.new) { 
                if (is.stagename == 'Posted' && is.amount > 0) inNeed.add(is);
            }
        }
        else if (trigger.isUpdate){
            for (Opportunity is : trigger.new) {
                Opportunity was = trigger.oldmap.get(is.id);
                if (is.stagename == 'Posted' && is.amount > 0 && is.Num_Payments__c == 0 && (was.stagename != 'Posted' || was.amount == null || was.amount == 0)) inNeed.add(is);
            }
        }
        paymentManager.addPayments(inNeed);
    }

You are updating the records in the after trigger, but not using any DMLs.So the operation doesn't happen. I don't know how the code coverage is happening in the sandbox.

Change the event from 'After' to 'Before' and try again. I think this will solve the problem.

  • Thanks for assistance - the trigger isolates records needing to be worked on, puts them in the list inNeed then calls paymentManager.addPayments which processes them -- > including a save. I didn't include that in the code to keep the posting shorter. Sorry. Excellent catch though. But still - my issue is not that the datas not saving - it's that I'm getting 0% coverage in the trigger - for the problem you mention to even be an issue I'd have some coverage of the trigger – user330428 Nov 30 '17 at 17:20
  • In the sample record in the test class, specify all the fields values which you are using in the trigger and make sure the record satisfies the criteria. Also you can add delete and undelete DMLs to increase coverage. I think your main focus should be to make sure your test data satisfies the conditions. – Sagnik Chattopadhyay Dec 1 '17 at 12:02

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.