+ Start a Discussion

Apex class/trigger 100% in sandbox, but 72% in production


Task - update Opportunity__c lookup field on case - from a text field on case with an opportunity id. This is my first coding task - but unsure how to troubleshoot the following issue.


Test code covers 100% in sandbox and manual testing of trigger works as required.  But ony 72% covered in production and it fails to delploy.


Here is class:


public class ImpCaseOppUpdate {
public static void ImpOppUpdate(Case[] oids) {
for (Case c :oids){
c.Opportunity__c = c.Opportunity_id_w__c;




Here is trigger:


trigger ImpCaseOppTrigger on Case (before insert,before update) 
Case[] oids = Trigger.new;



Here is test class:


private class ImpOppCaseTestClass{
 static testMethod void validateImpOppCase(){
Case c = new Case(Opportunity__c=Null,Opportunity_Id_w__c='006R0000007Jt1j');
System.debug('Opportunity before inserting new: ' +c.Opportunity__c);

insert c;

c=[SELECT Opportunity__c FROM Case WHERE Id=:c.id];
System.debug('Opportunity after trigger fired:' +c.Opportunity__c);



Here are the errors after attempting to deploy to production.




Failure Message: "System.NullPointerException: Attempt to de-reference a null object", Failure Stack Trace: "Class.MassUpdateSimpleController.__sfdc_fieldName: line 111, column 1 Class.MassUpdateSimpleControllerTest.singleUpdateTest: line 33, column 1"


Deploy Error


Average test coverage across all Apex Classes and Triggers is 72%, at least 75% test coverage is required.


Your problem isn't in your code, at least not directly. What's happening here is that some other code elsewhere in the system is causing MassUpdateSimpleController to have a null value when one is not expected. This bug may be unresolveable, and you may need to contact the developer of the MassUpdateSimpleController class for a fix to this problem.


On a related note, your class can not succeed in production and sandbox simultaneously, because you're using a hardcoded ID value, which I presume came from your sandbox (the ID starts with 006R, a Sandbox ID prefix). This means that the system won't find the opportunity you're hardcoding, and it will throw an "insufficient rights on cross-reference ID" error. Instead, your code should create a new Opportunity, then assign the value from that newly created record directly into the test Case record.


Please let us know if you need any further assistance in this matter.

Vinod TomarVinod Tomar

This error is not in your code. Error is in MassUpdateSimpleController and MassUpdateSimpleControllerTest. Can you please these classes and see what wrong at given line nubers. Its not finding the values.


Those two were a part of a mass update free application I was testing. I removed this package - rather than troubleshoot code I'm sure I will not understand at this time.


I also updated the test flow to use a created opportunity rather than a hard code value.


These resolved my issues and I was able to deploy!


Thanks for you help.


We've also had the same problem, however we fixed it in a different way.

It appears that the test tries to copy the field Amount of Opportunity, however the nature of DescribeSObjectResult that MassUpdateSimpleController uses doesn't recognize fields that you don't have permission to view, and it appears that by default Amount cannot be viewed.

So in the end this depends all on who is the one performing the deployment.


In order to fix this, you need to enable your profile to read the Opportunity.Amount field.

Dave DaulDave Daul
Nice fix @alongb-plimus