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
Guilherme Brito 8Guilherme Brito 8 

system.dmlexception: insert failed. first exception on row 0; first error: field_integrity_exception

I wrote a controller and a test class that runs just fine in my QA but when I try to deploy it an error occours
"System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, The time trigger value calculated by the Apex class "CalculateSLAMilestone" must be a positive integer .: []
Stack Trace: Class.faleConoscoVFController.duplicateCasoSon: line 47, column 1 Class.faleConoscoVFController.rejectSolution: line 32, column 1 Class.faleConoscoVFControllerTest.test: line 19, column 1"
 
public class faleConoscoVFController {
    public static case css {get; set;}
    public static string motive{get;set;}
    public static boolean disableReopening{get;set;}
    public static boolean caseReopened{get;set;}
    public static string caseID = ApexPages.currentPage().getParameters().get('caseID');
    public static datetime data{get;set;}
    
    public static void onLoad(){
        css = [SELECT id, CaseNumber, Subject, CreatedDate, Description, Status, Data_de_Fechamento__c
               FROM Case 
               WHERE id =:caseID];
        
        faleConoscoVFController.disableReopening();
    }
    
    public static void disableReopening(){
        if(css.Data_de_Fechamento__c < (Date.Today() - 7)){
            disableReopening = true;
        }
    }
    
    public static void rejectSolution(){
        case css2 = [SELECT id, CaseNumber, Subject, CreatedDate, Description, Status, Data_de_Fechamento__c, Data_Prevista_Fechamento__c
                     FROM Case 
                     WHERE id =:caseID];
        
        css2.Status = 'Novo';
        css2.Caso_Reaberto_por_E_mail__c = true;
        update css2;
        caseReopened = true;
        faleConoscoVFController.duplicateCasoFilho();
        data = css2.Data_Prevista_Fechamento__c;
    }
    
    public static void duplicateCasoFilho(){
        case newCase = new case();
        case oldCase = [SELECT ID, Subject, Description, Area_Resolucao__c FROM Case WHERE ParentID =:caseID
                        ORDER BY CreatedDate DESC LIMIT 1];
        
        newCase.Subject = oldCase.Subject;
        newCase.Description = motive;
        newCase.Area_Resolucao__c = oldCase.Area_Resolucao__c;
        newCase.Status = 'Em Tratamento';
        newCase.RecordTypeId = '0121V000001IhK9QAK';
        newCase.ParentId = caseID;
        insert newCase;
    }
}
 
@isTest(SeeAllData=true)
public class faleConoscoVFControllerTest {
    @isTest(SeeAllData=true)
    static void test(){
        Test.startTest();
        case caso = new case();
        insert caso;
        
        case casoFilho = new case();
        casoFilho.ParentId = caso.Id;
        insert casoFilho;
        
        system.debug('Caso: ' + caso.id);
        system.debug('Caso Filho: ' + casoFilho.id);
        
        faleConoscoVFController.data = date.today();
        faleConoscoVFController.caseID = caso.Id;
        faleConoscoVFController.disableReopening = false;
        faleConoscoVFController.onLoad();       
        faleConoscoVFController.rejectSolution();
        Test.stopTest();
    }
}

Could anyone help me with this, please?
Andrew GAndrew G
based on the error message, I would be checking my Case object triggers.  It seems that the output for the class / method CalculateSLAMilestone does not calculate correctly based on the test Case you are inserting at line 47.

From your comment I infer that the Test class above runs correctly in your sandbox but fails on Deploy.  I would check that the Apex class/method CalculateSLAMilestone in your sandbox matches the code in the production environment.  I would then runall all tests in my Sandbox to see if I could replicate the error. 

Regards
Andrew
Guilherme Brito 8Guilherme Brito 8
Hello Andrew, I compared both codes but they're the same save some system.debugs in my QA enviroment :/ 
Andrew GAndrew G
Ok.
I would still investigate the CalculateSLAMilestone class.

I am going to assume that there is a validation on some field that the code writes to that insists on a postive integer value.
I will guess that the CalculateSLAMilestone class (based on the name) will do a calculation based on something like a create date-time and a modified date-time and that the calculation, because it fires on the Case creation so quickly, the value may be created a zero which the validation does not recognise as suitable for that field.

If that is what that class is doing, i would perhaps review the use of the Test.setCreatedDate method in the test classes.

Regards
Andrew