Hello I am trying to deploy a Trigger and a Class from my Sandbox to production environment. I am running into Code coverage error.
- The code coverage when i look in the developer console says is 75% but when i try to deploy its says its only 60%
- 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();
}
}