function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dev2IndiaDev2India 

Deployement error

Hi All,

 

Everything is working fine in Sandbox. I am getting erro in Production while validating. Below is the error :

 

================================================================

TestoppTrigger.testpreventoppdelete() Class 35 1 
 Message: "System.AssertException: Assertion Failed: Expected: false, Actual: true",
 Failure Stack Trace: "Class.TestoppTrigger.testpreventoppdelete: line 35, column 1"

================================================================

 

This is the simple trigger and test class:

 

trigger PreventOppDeletion on Opportunity (before delete) {
   
        String UserId = UserInfo.getuserId();
        user userRec =  [Select id from user where id = '005000000079xyz' limit 1];  
       
              for(opportunity  opp : trigger.old)
              {
              if (UserId != userRec.Id && opp.StageName == 'Closed Won' && opp.New_Type__c.startswith('Renewal')) {Opp.addError('You are not authorised to delete this Opportunity.');
              }
             
             }
   
    }

 

================================

@isTest(SeeAllData=true)
   
     private class TestPreventOppTrigger
    {
    static testMethod void testPreventOppDeletion(){
          
           Profile p = [select id from profile where name='Standard User'];
           User u = new User(alias = 'standt', email='standarduser@testorg.com',
                             emailencodingkey='UTF-8', lastname='Testing',
                             languagelocalekey='en_US',localesidkey='en_US',
                             profileid = p.Id,timezonesidkey='America/Los_Angeles',
                             username='teststandarduser@testorg.com');
  
  
           System.runAs(u){
          
                Opportunity o = new Opportunity();
                o.name='Opp1';
                o.stageName='Open';
                o.CloseDate=Date.today();
                o.Type='New Customer';
                o.amount=100.00;
                o.ContractReceived__c=Date.today();
                 insert o;
                 try{
                     delete o;
                 }catch(Exception ex){
                        Boolean Message = false;         
                        if(ex.getMessage().contains('You are not authorised to delete this Opportunity')){
                             Message = true;     
                        }else{
                             Message = false;     
                        }
                        system.debug('########Message : '+Message);
                       
                        System.AssertEquals(Message, true);
                 }
        }
  }


}

================================ 

 

Please suggest. I would appreciate your feedback.

 

Thanks,

 

Best Answer chosen by Admin (Salesforce Developers) 
Dev2IndiaDev2India

I resolved by own. Taking case of assertions in the first trigger

  System.AssertEquals(Message, false);

 

And then

this itself resolved the issue.

 

Thanks all.

Regards

All Answers

levaleva

A couple of suggestions: 

 

1. remove SeeAllData=true

2. Looks like you are entering the exception block, so the delete did fail probably with a different error. What does the debug statement say ('######Messager')

Dev2IndiaDev2India

Thanks for the time. HOwever, I did the same, not worked. The error is referencing by other test class which I used for the other same trigger and class. only conditions are different. Plz see below :

 

=====================================

trigger

 

trigger preventoppdelete on opportunity (before delete){
   
    String UserId = UserInfo.getuserId();
    user userRec = [Select id from user where name = 'XYZ' limit 1];    
      
     for(opportunity o : trigger.old){

       if (UserId != userRec.Id && o.StageName == 'Closed Won'&& o.CFC_Check__c != null && o.Contract_Implemented__c == True){ o.adderror('You are not authorised to delete this Opportunity');
       }
     }         
}

 

test class:

@isTest(SeeAllData = true)
private class TestoppTrigger
{
    static testMethod void testpreventoppdelete(){
          
           Profile p = [select id from profile where name='Standard User'];
           User u = new User(alias = 'standt', email='standarduser@testorg.com',
                             emailencodingkey='UTF-8', lastname='Testing',
                             languagelocalekey='en_US',localesidkey='en_US',
                             profileid = p.Id,timezonesidkey='America/Los_Angeles',
                             username='teststandarduser@testorg.com');
  
  
           System.runAs(u){
          
                Opportunity o = new Opportunity();
                o.name='Opp1';
                o.stageName='Closed Won';
                o.CloseDate=Date.today();
                o.Type='New Customer';
                o.amount=100.00;
                o.ContractReceived__c=Date.today();
                 insert o;
                 try{
                     delete o;
                 }catch(Exception ex){
                        Boolean Message = false;         
                        if(ex.getMessage().contains('You are not authorised to delete this Opportunity')){
                             Message = true;     
                        }else{
                             Message = false;     
                        }
                        system.debug('########Message : '+Message);
                       
                        System.AssertEquals(Message, true);
                 }
        }
  }


}

=====================================

this is working fine in production.

 

====================================

Above referenced deployment error is coming while deploying below same alike

trigger PreventOppDeletion on Opportunity (before delete) {
   
        String UserId = UserInfo.getuserId();
        user userRec =  [Select id from user where id = '005000000079xyz' limit 1];  
       
              for(opportunity  opp : trigger.old)
              {
              if (UserId != userRec.Id && (opp.StageName == 'Closed Won'||opp.StageName == 'Closed Lost') && opp.New_Type__c.startswith('Renewal')) {Opp.addError('You are not authorised to delete this Opportunity.');
              }
             
             }
   
    }

class:

@isTest
   
     private class TestPreventOppTrigger
    {
    static testMethod void testPreventOppDeletion(){
          
           Profile p = [select id from profile where name='Standard User'];
           User u = new User(alias = 'standt', email='standarduser@testorg.com',
                             emailencodingkey='UTF-8', lastname='Testing',
                             languagelocalekey='en_US',localesidkey='en_US',
                             profileid = p.Id,timezonesidkey='America/Los_Angeles',
                             username='teststandarduser@testorg.com');
  
  
           System.runAs(u){
          
                Opportunity o = new Opportunity();
                o.name='Opp1';
                o.stageName='Open';
                o.CloseDate=Date.today();
                o.Type='New Customer';
                o.amount=100.00;
                o.ContractReceived__c=Date.today();
                 insert o;
                try{
                     delete o;
                }catch(Exception ex){
                        Boolean Message = false;         
                        if(ex.getMessage().contains('You are not authorised to delete this Opportunity')){
                             Message = true;     
                        }else{
                             Message = false;     
                        }
                        system.debug('########Message : '+Message);
                       
                        System.AssertEquals(Message, true);
                 }
        }
  }


}

===========================

Please suggest in this condition....

Dev2IndiaDev2India

I resolved by own. Taking case of assertions in the first trigger

  System.AssertEquals(Message, false);

 

And then

this itself resolved the issue.

 

Thanks all.

Regards

This was selected as the best answer