0

I'm new to Apex coding and have created a trigger that closes an Opportunity (StageName='Closed') whenever a checkbox in the related contract is checked (ApStatus=true). After searching around I came up with the following code and associated test that passed 100% coverage on the Sandbox. However, when I try to deploy to production, it fails with the following error code:

System.QueryException: List has no rows for assignment to SObject Stack Trace: Class.testContract2Opp.testUpdate: line 27, column 1

How can the test runs work on Sandbox and then generate errors on deployment? Any help is much appreciated!

The trigger:

     trigger Contract2Opp_Close on Contract (after update) {
  Set<Id> oppIds = new Set<Id>();
  for(Contract c:Trigger.new) {
    if(c.ApStatus__c==true && Trigger.oldMap.get(c.id).ApStatus__c!=true) {
      oppIds.add(c.Opportunity_related_to__c);
    }
  }
  for(Opportunity[] oppList:[select id from opportunity where id in     :oppIds]) {
    for(Opportunity opp:oppList) {
      opp.StageName = 'Closed';
      opp.Closed_Approved__c = true;
    }
    update oppList;
  }
}

And it's test class:

 @isTest

public class testContract2Opp {
static testMethod void testUpdate(){
            Account acct = new Account (Name = 'Acme, Inc.');
            insert acct;   
            Opportunity testOpp = new Opportunity( 
                Name = 'Test Opportunity',
                StageName='Qualified', 
                CloseDate=System.today(), 
                AccountId = acct.Id,
                Input_device__c='ebook',
                Biz_Type__c='NB');
            insert testOpp;
            Contract testcontract = new Contract( 
                Name='Test Contract',
                Opportunity_related_to__c = testOpp.Id,
                StartDate=System.today(),
                ContractTerm=4,
                Status = 'Draft',
                ApStatus__c=false,
                Short_Company_Name__c = 'TOTO', 
                AccountId = acct.Id);
            insert testcontract;
    testcontract.ApStatus__c=true;
        update testcontract;
    testOpp = [SELECT Name, StageName
        FROM Opportunity
        WHERE StageName = :'Closed'];
    System.assertEquals('Closed', testOpp.StageName);
    }
}
1
  • Many thanks for all the fast answers! This is really stange now because the error I'm getting is from another test for another trigger that is not part of the my change set! I got rid of the code on the Sandbox but some way it keeps trying to deploy it :( And I'm pretty sure my Change Set only has 2 components: Trigger + Test Class but somehow SF is trying to deploy 3 components....
    – Frynne
    Mar 6, 2015 at 17:32

2 Answers 2

0

Looks like your SOQL is now incorrect.

testOpp = [SELECT Name, StageName
        FROM Opportunity
        WHERE StageName = :'Closed'];

Might be better to query for:

testOpp = [SELECT Name, StageName
        FROM Opportunity
        WHERE id=:testOpp.id];

There are a number of reasons why code works in the sandbox and not in production. I would hazard a guess that a trigger is missing or a workflow.There is something that isn't closing the opportunity when the contract is updated.

You will need to compare the stacktrace on sandbox and production and see if you can 'spot the difference'; alternatively look at the workflows and triggers in both systems and look for any differences

1
  • The error is the deployment. Therefore I can't even get the code from the sandbox to my production environment. See my added comment above, nothing to do with the code after all but due to some old code that's got hooked up to this one and is causing errors...
    – Frynne
    Mar 6, 2015 at 17:41
0

You don't need the bind variable ":" in your select statement for the testOpp if you are passing a string (you do if you are passing a variable. Actually, I would create a different Opp to test with, instead of repurpose the original opp.

@isTest

public class testContract2Opp {
static testMethod void testUpdate(){
            Account acct = new Account (Name = 'Acme, Inc.');
            insert acct;   
            Opportunity testOpp = new Opportunity( 
                Name = 'Test Opportunity',
                StageName='Qualified', 
                CloseDate=System.today(), 
                AccountId = acct.Id,
                Input_device__c='ebook',
                Biz_Type__c='NB');
            insert testOpp;
            Contract testcontract = new Contract( 
                Name='Test Contract',
                Opportunity_related_to__c = testOpp.Id,
                StartDate=System.today(),
                ContractTerm=4,
                Status = 'Draft',
                ApStatus__c=false,
                Short_Company_Name__c = 'TOTO', 
                AccountId = acct.Id);
            insert testcontract;
    testcontract.ApStatus__c=true;
        update testcontract;
    Opportunity checkOpp = [SELECT Name, StageName
        FROM Opportunity
        WHERE ID = :testOpp.id];
    System.assertEquals('Closed', checkOpp.StageName);
    }
}

My assumption would be that one of the records you are inserting (Account, Opportunity or Contract) are not getting inserted due to a validation rule or missing required field.

Check the debug log to see if one of the other inserts failed.

2
  • The assert should be on the newly loaded checkOpp and not testOpp. Just a minor thing... Mar 6, 2015 at 16:50
  • RIght Richard. Updated.
    – JimRae
    Mar 6, 2015 at 17:13

You must log in to answer this question.

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