1

Hello I am trying to deploy a Trigger and a Class from my Sandbox to production environment. I am running into Code coverage error.

  1. The code coverage when i look in the developer console says is 75% but when i try to deploy its says its only 60%
  2. The deployment also complains that there is 0% coverage for a Trigger which i am not even trying to deploy and already exists on production. Also developer console says this Trigger Code coverage is 100%

Any ideas why there is discrepancy ?

Also on a different the code coverage functionality in the developer console doesn't work in Sandbox and works on production. Any ideas ?

Following is the code and Test i am trying to deploy

trigger OpportunityTrigger on Opportunity (before insert, before update) {

    Boolean isError = false;

    for (Opportunity op: Trigger.new) {

       List<Opportunity > opps = [SELECT Id, Name, RecordTypeId FROM Opportunity WHERE AccountId =: op.AccountId]; 

        // Loop through the list and update the Name field
        for(Opportunity o : opps){
            if(op.Id != null && op.Id != o.Id){
                if(o.RecordTypeId == op.RecordTypeId)
                {
                    isError  = true;
                }
            }
        }

        if(isError){
            op.addError('Opportunity Record Type needs to be Unique for each Account'); 
        }


        // PERMISSION VALIDATION

        List<PermissionSetAssignment> permissionSets = [SELECT Id FROM PermissionSetAssignment WHERE AssigneeId = :Userinfo.getUserId() AND PermissionSet.Name = 'SkyBlue_Admin' ];

        if(permissionSets == null || permissionSets.Size()== 0)
        {
            Opportunity oldOpp = Trigger.oldMap.get(op.Id);

            Boolean oldOppIsWon = oldOpp.StageName.equals('Closed Won');
            Boolean newOppIsWon = op.StageName.equals('Closed Won');

            if(oldOppIsWon){
              if(!newOppIsWon)
              {
                op.addError('You donot have permission to change the Stage from Closed Won'); 
              }

            }else{
                if(newOppIsWon)
                  {
                    op.addError('You donot have permission to change the Stage to Closed Won'); 
                  }
            }
        }

    }
    }

Test Class

@isTest 
private class OpportunityTriggerTestClass{

    static testMethod void ValidateOpportunityTriggger() {

       Test.startTest();

       account acct = new account(Name = 'Testing Account Opportunity');
       Opportunity o = new Opportunity(Name='Test Opportunity',StageName = 'Interest', Description='Tesing Opportunity Trigger',CloseDate=Date.today());
       System.debug('Opportunity Name: ' + o.Name);



       insert acct;
       o.AccountId = acct.Id;
       insert o;
            // Retrieve the new book
       o = [SELECT Name FROM Opportunity WHERE Id =:o.Id];
       System.debug('Opportunity Name: ' + o.Name);

       // Test that the trigger correctly updated the price
       System.assertEquals('Interest', o.StageName);



      Test.stopTest();
    }
}
  • The code coverage that is reported in your production org is for ALL classes and triggers in that org. This includes the code you are pushing in as well as all other code that exists in that org. I can't see how you could have a trigger in production that has 0% test coverage, SF should of blocked this trigger from being saved to the org. – BarCotter Sep 18 '14 at 11:28
  • Are the objects identical between the sandbox and production? Are there any required fields or workflows present only in production that might be involved? – Jagular Sep 18 '14 at 14:26
0

1.The code coverage when i look in the developer console says is 75% but when i try to deploy its says its only 60%

  • You talk about total Code Coverage or this class? if you mean only class I assume there could be issue with permission set assignment a.k.a configuration.

2. The deployment also complains that there is 0% coverage for a Trigger which i am not even trying to deploy and already exists on production. Also developer console says this Trigger Code coverage is 100%

  • When you deploy ALL test run and all requirements for deploy must be fulfilled so you must either delete this trigger(if it is not used) or create test for it. About second part - are you sure all test classes(for this trigger) compile?

For Your Information: It is best practice to have logic less triggers.

0

75% code coverage is for the org or for the specific class?

For the second part of the question, I assume the issue is because the test class associated to the trigger is failing due to some reason and hence the trigger code coverage is dipped to 0%

0

thank you very much for your responses.

Turns out that the two triggers i have are a Lead Conversion and Opportunity Trigger. Lead conversion trigger was also creating a opportunity and so there was a collision in the Lead Test Class and the Opportunity Test Class which was causing a DeadLock.

I was able to fix this issue by changing the Lead Test Class not to create an opportunity while the conversion process.

Thanks a lot for your responses.

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.