3

I have a trigger that works in my Sandbox, but when I try to deploy in Production, it keeps failing with the following result: Deploy Error Average test coverage across all Apex Classes and Triggers is 68%, at least 75% test coverage is required.

I have no other triggers/classes besides the defaults. Here is my trigger, let me know if you have any suggestions? (I'm forcing an Opportunity to tie to the Opportunity Owner when manually assigned)

trigger OpportunityAssignment on Opportunity (before update) 
{
    Set<ID> sOwnIDs = new Set<ID>();
    for(Opportunity op : Trigger.new)
    {
            sOwnIDs.add(op.ownerID);
            sOwnIDs.add(Trigger.oldMap.get(op.id).ownerID);
    }

    // System.debug('>>>>>>>>>>>>' + map_ID_User);

    Set<ID> sConID = new Set<ID>();    
    Map<ID,User> map_ID_User = new Map<ID,User>([Select id, name, contactID from User where id in : sOwnIDs]);
    for(User u : map_ID_User.values())
    {
        sConID.add(u.contactID); 
    }
    System.debug('>>>>>>>>>>>>' + map_ID_User);

    Set<ID> sAccountID = new Set<ID>();
    Map<ID,ID> map_ConID_Account = new Map<ID,ID>();

    for(Contact c: [Select id, accountID from Contact where id in:sConID])
    {
        map_ConID_Account.put(c.id,c.accountID);
        sAccountID.add(c.accountID);
    }

    Map<ID,Account> map_ID_Account = new Map<ID,Account>([Select id, name from Account where id in: sAccountID]);

    // Main Logic Starts here    
    List<Account> lAccUpdate = new List<Account>();
    for(Opportunity op : Trigger.new)
    {
        System.debug('Value of owner ID' + op.ownerID);
        if((op.ownerid != Trigger.oldMap.get(op.id).ownerID))
        {
            User usr = map_ID_User.get(op.ownerID);
            System.debug('Value of ContactID' + usr.ContactID);
                if(usr.ContactID != null)
                {
                    ID accID = map_ConID_Account.get(usr.contactID);
                    System.debug('Value of account ID' + accID );

                    Account acc = map_ID_Account.get(accID );    
                    op.accountiD = acc.id;
                }
                else
                {
                    op.accountiD = null;
                }
        }

    }

    if(lAccUpdate.size() > 0)
        update lAccUpdate; 
}

Additionally, here is my test class:

@IsTest(seeAlldata = true)
Class OpportunityAssignment_Test
{
    private static testmethod void OpportunityAssignment_testmethod()
    {

        User usr = [Select id,name from User where Profile.name ='Partner Community User' and IsActive = true limit 1];
        Account acc = new Account();
        acc.name = 'Test Name';
        insert acc;

        Opportunity opp = new Opportunity();
        opp.name = 'Test Opp';
        opp.StageName = 'New';
        opp.closeDate = System.now().date();
        insert opp;

        Test.startTest();
        opp.name = 'Test opp1';
        opp.ownerID = usr.id;
        update opp;        
        Test.stopTest();
    }
}
3
  • What does your test class look like? Jan 29, 2014 at 17:35
  • 1
    Do you have a test class for your trigger ? It appears the test coverage may be failing because you're trying to deploy a trigger which is not covered by test code. Jan 29, 2014 at 17:40
  • The trigger isn't failing, the deployment is failing because there are not enough covered lines of code. Unless you got a specific test failure, you just need to cover more lines in your organization overall. Apr 11, 2015 at 13:16

2 Answers 2

1

Before answering your question, I would like to point out a possible miss in your trigger. Have you forgotten to add the accounts to lAccUpdate to finally update the list?

Coming to your question, Since you use the existing data to test, here are some of the logical points you would want to verify to see the proper coverage in PROD.

  1. This query should return records. Select id,name from User where Profile.name ='Partner Community User' and IsActive = true limit 1
  2. and this query should return non-null contactID Select id, name, contactID from User where id in : sOwnIDs

If you verify the above queries, I think maximum coverage is guaranteed.

Hope this helps.

0

There are two things needs to kept in mind while deploying a Trigger or Apex Class:

1.Whenever you trying to deploy a component to the production, salesforce internally evaluate overall code coverage of the target org including your component. i.e overall coverage of production org + test coverage of your trigger > 75 % then only deployment will be committed.

2.Moreover when overall code coverage evaluated, then the aggregate code %age of all the trigger will be added to individual apex classes coverage and then finally total aggregation calculated.

Its like if you have two trigger with 90%+90% and one apex class(30%) then overall code coverage will be 90+90/2= 90% and then 90+30/2 = 60% < 75% so component would also not deployed to target org.

May be point-1 will be helpful to resolve your issue.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .