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
SFDCDevQASFDCDevQA 

Test class failures when deploying to production but not in sandbox

I am running into a deployment error with a test class which runs fine in the sandbox.  The error I am receiving is Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, QuoteAdoptionAgreementSent: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 0Q0600000005hI8CAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY


The line and column from the test class that it is hanging on is the insertion of a task.  My test class is as follows and the line getting the error is Line 23.  I've checked my system admin profile and it has access to everything.  I can't figure this out.  Please help.

 

Thank you,

Amanda

 

@IsTest
private class TestQuoteAASentUpdateOppCreateCourses
{
    private static TestMethod void testTrigger(){
        
        //Step 1 : Data Insertion
        Account a=new Account(Name='Test Account');
           insert a;
           Contact c = new Contact(FirstName='John',LastName='Doe');
        insert c;
        Opportunity o = new Opportunity(Name='Test Opportunity',closedate=system.today(), stagename='Confirmed Teaching/Class Schedule',Probability=0.95);
        insert o;      
        OpportunityContactRole ocr = new OpportunityContactRole (OpportunityID = o.id, ContactID=c.id, role='Decision Maker') ;     
        insert ocr;  
        Quote q= new Quote (Name='Test Quote', ContactID=c.id, OpportunityID=o.id);
        insert q;
        
        
        test.startTest();
        
        //Perform the dml action on which trigger gets fired , like insert, update ,delete , undelete, in your case you have to update account record that you created in above  
       Task t= new Task (Subject='Email: Sapling Learning Adoption Agreement', WhoID=c.id, WhatID=q.id);
        insert t;
       
       
        // Switch back to runtime context
        Test.stopTest();
        
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
dphilldphill

Try doing this and looking at the debug log.  When looking at your debug log, you can just use Ctrl+F to find the text 'My error = ':

 

try
{
  //Perform the dml action on which trigger gets fired , like insert, update ,delete , undelete, in your case you have to update account record that you created in above  
  Task t= new Task (Subject='Email: Sapling Learning Adoption Agreement', WhoID=c.id, WhatID=q.id);
  insert t;
}catch (Exception e)
{
  System.debug('My error = ' + e);
}

 

All Answers

dphilldphill
These sort of things happen a lot when there are differences between Production and Sandbox. There could be differences in Triggers or Validation rules or any other number of things. This happens a lot when people decide to make changes directly in production and not a Sandbox and then deploy to production. It is hard to give you a better answer without having knowledge of your system. Sorry I couldn't give a better response.
SFDCDevQASFDCDevQA
Is there a way to get it to tell me exactly why it can't do the insert? It used to tell me exact validation rules if they were affecting it and I've edited all the validation rules to ignore the System Admin Profile so I'm at a loss as to figure out what it is that is preventing it. I have another test class with an insert Task that was deployed last week and it works fine. Thanks, Amanda -- Amanda Howell Certified Salesforce.com Consultant 919-807-1116 amanda@sfdcsolutions.com
dphilldphill

Try doing this and looking at the debug log.  When looking at your debug log, you can just use Ctrl+F to find the text 'My error = ':

 

try
{
  //Perform the dml action on which trigger gets fired , like insert, update ,delete , undelete, in your case you have to update account record that you created in above  
  Task t= new Task (Subject='Email: Sapling Learning Adoption Agreement', WhoID=c.id, WhatID=q.id);
  insert t;
}catch (Exception e)
{
  System.debug('My error = ' + e);
}

 

This was selected as the best answer
SFDCDevQASFDCDevQA

I have no idea why but changing the test start and stop test to the try method fixed the problem.

 

Thanks!

Amanda

dphilldphill

Well, what the try/catch does is catches the error.  So you will no longer see the error happen when you have it with the try/catch, but it should still be happening.  That's why I wanted you to take a look at the debug log.  It should hopefully give you a better response.