-2

I have encountered this error

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY it points the error in Class.CaseExtension.updateCase: line 25, column 1 Class.CaseExtensionTest.CaseExt: line 64, column 1

Is there anything that i missed on my code?

Class

 @isTest

public class CaseExtensionTest {
Static testMethod void CaseExt(){

    Profile prof = [SELECT Id
            FROM  Profile
            WHERE Name ='System Administrator'];

    //Create a user
    List<User> user1         = new List<User>();
    for(Integer i=0;i<10;i++) {
      User u1                 =new User();
      u1.FirstName            = 'test'+i;
      u1.LastName             = 'tes';
      u1.Email                = '[email protected]';
      u1.ProfileId            =  prof.id;
      u1.username             = 'Testing'+i+'@samplename.com';
      u1.Alias                = 'Test'+i;
      u1.IsActive             =  True;
      u1.EmailEncodingKey     = 'UTF-8';
      u1.LanguageLocaleKey    = 'en_US';
      u1.LocaleSidKey         = 'en_US';
      u1.TimeZoneSidKey       = 'America/Los_Angeles';
      user1.add(u1);
    }

    insert user1; 

        User u = [SELECT Id,
                        Username 
                      FROM  User 
                    WHERE Email =: '[email protected]' LIMIT 1];
        System.runAs(u) {
              // The following code runs as user 'u'
              System.debug('Current User: ' + UserInfo.getUserName());
              System.debug('Current Profile: ' + UserInfo.getProfileId());
          }


    /*================CREATE ACCOUNT ====================*/
    Map <String,Schema.RecordTypeInfo> recordTypeMap = Account.sObjectType.getDescribe().getRecordTypeInfosByName();

    list<Account> Account_test = new List<Account>();

    Account TestAccount = new Account();
    TestAccount.Name = 'TestClass-TestAccount';
    TestAccount.Phone = '00000000000';
    TestAccount.Landline_Number__c = '00000000000';
    TestAccount.Standard_Address_Street__c = 'Test Address St';
    TestAccount.Type = 'Client';
    TestAccount.Customer_Code__c = '0000000';
    TestAccount.RecordTypeId = recordTypeMap.get('Business Account').getRecordTypeId();        
    Account_test.add(TestAccount);
    insert Account_test;     

    /* ===============CREATE CASE =========================*/
    List<Case> Case_test1 = new List<Case>();
    List<Case> Case_test2 = new List<Case>();

    Case CaseTest = new Case();
    CaseTest.Accountid = TestAccount.id;
    CaseTest.Origin= 'Phone';
    CaseTest.Transaction_Group__c ='FOR PROBING';
    CaseTest.Transaction_Item__c ='FOR PROBING';
    CaseTest.Type = 'Inquiry';
    CaseTest.Status = 'In Progress';
    CaseTest.Subject = 'Test Subject ';
    CaseTest.Description = 'Test Description ';
    CaseTest.SuppliedEmail = '[email protected]';
    CaseTest.Case_Brand_Handler__c = 'Amicassa';
    CaseTest.Priority = 'Medium';
    CaseTest.OwnerID = UserInfo.getUserId();
    Case_test1.add(CaseTest);

    insert Case_test1;

    for(Case case1 : Case_test1 ){
        Case_test2.add(case1);

    }
     test.startTest();
        ApexPages.StandardController sc = new ApexPages.StandardController(Case_test2[0]);
        CaseExtension CaseExt = new CaseExtension(sc);
        CaseExt.updateCase();
    test.stopTest();

}
}

Test Class

@isTest

public class CaseExtensionTest {

    Static testMethod void CaseExt(){

         String uniqueUserName = 'unique' + DateTime.now().getTime() + '@testorg.com';
        // This code runs as the system user
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        User u = new User(Alias = 'standt', Email='[email protected]',
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles',
         UserName=uniqueUserName);

        System.runAs(u) {
              // The following code runs as user 'u'
              System.debug('Current User: ' + UserInfo.getUserName());
              System.debug('Current Profile: ' + UserInfo.getProfileId());
          }

        /*================CREATE ACCOUNT ====================*/
        Map <String,Schema.RecordTypeInfo> recordTypeMap = Account.sObjectType.getDescribe().getRecordTypeInfosByName();

        list<Account> Account_test = new List<Account>();

        Account TestAccount = new Account();
        TestAccount.Name = 'TestClass-TestAccount';
        TestAccount.Phone = '00000000000';
        TestAccount.Landline_Number__c = '00000000000';
        TestAccount.Standard_Address_Street__c = 'Test Address St';
        TestAccount.Type = 'Client';
        TestAccount.Customer_Code__c = '0000000';
        TestAccount.RecordTypeId = recordTypeMap.get('Business Account').getRecordTypeId();        
        Account_test.add(TestAccount);
        insert Account_test;     

        /* ===============CREATE CASE =========================*/
        List<Case> Case_test1 = new List<Case>();

        Case CaseTest = new Case();
        CaseTest.Accountid = TestAccount.id;
        CaseTest.Origin= 'Phone';
        CaseTest.Transaction_Group__c ='FOR PROBING';
        CaseTest.Transaction_Item__c ='FOR PROBING';
        CaseTest.Type = 'Inquiry';
        CaseTest.Status = 'In Progress';
        CaseTest.Subject = 'Test Subject ';
        CaseTest.Description = 'Test Description ';
        CaseTest.SuppliedEmail = '[email protected]';
        CaseTest.Priority = 'Medium';
        CaseTest.OwnerID = UserInfo.getUserId();
        Case_test1.add(CaseTest);


        insert Case_test1;

         test.startTest();
            ApexPages.StandardController sc = new ApexPages.StandardController(CaseTest1[0]);

            CaseExtension CaseExt = new CaseExtension(sc);
            CaseExt.updateCase();
        test.stopTest();

    }
}
1

2 Answers 2

1

You cannot do Mixed DML operation .go through the link.And I would suggest something like this.

 @testSetup
static void dataSetup() {

Profile prof = [SELECT Id
                FROM  Profile
                WHERE Name ='System Administrator'];

//Create a user
List<User> user1         = new List<User>();
for(Integer i=0;i<10;i++) {
  User u1                 =new User();
  u1.FirstName            = 'test'+i;
  u1.LastName             = 'tes';
  u1.Email                = '[email protected]';
  u1.ProfileId            =  prof.id;
  u1.username             = 'sh'+i+'@l.com';
  u1.Alias                = 'sh'+i;
  u1.IsActive             =  True;
  u1.EmailEncodingKey     = 'UTF-8';
  u1.LanguageLocaleKey    = 'en_US';
  u1.LocaleSidKey         = 'en_US';
  u1.TimeZoneSidKey       = 'America/Los_Angeles';
  user1.add(u1);
}

insert user1; }



Static testMethod void CaseExt(){

      User u = [SELECT Id,
                    Username 
                  FROM  User 
                WHERE Username = '[email protected]'];
    System.runAs(u) {
          // The following code runs as user 'u'
          System.debug('Current User: ' + UserInfo.getUserName());
          System.debug('Current Profile: ' + UserInfo.getProfileId());
      }

    /*================CREATE ACCOUNT ====================*/
    Map <String,Schema.RecordTypeInfo> recordTypeMap = Account.sObjectType.getDescribe().getRecordTypeInfosByName();

    list<Account> Account_test = new List<Account>();

    Account TestAccount = new Account();
    TestAccount.Name = 'TestClass-TestAccount';
    TestAccount.Phone = '00000000000';
    TestAccount.Landline_Number__c = '00000000000';
    TestAccount.Standard_Address_Street__c = 'Test Address St';
    TestAccount.Type = 'Client';
    TestAccount.Customer_Code__c = '0000000';
    TestAccount.RecordTypeId = recordTypeMap.get('Business Account').getRecordTypeId();        
    Account_test.add(TestAccount);
    insert Account_test;     

    /* ===============CREATE CASE =========================*/
    List<Case> Case_test1 = new List<Case>();

    Case CaseTest = new Case();
    CaseTest.Accountid = TestAccount.id;
    CaseTest.Origin= 'Phone';
    CaseTest.Transaction_Group__c ='FOR PROBING';
    CaseTest.Transaction_Item__c ='FOR PROBING';
    CaseTest.Type = 'Inquiry';
    CaseTest.Status = 'In Progress';
    CaseTest.Subject = 'Test Subject ';
    CaseTest.Description = 'Test Description ';
    CaseTest.SuppliedEmail = '[email protected]';
    CaseTest.Priority = 'Medium';
    CaseTest.OwnerID = UserInfo.getUserId();
    Case_test1.add(CaseTest);


    insert Case_test1;

     test.startTest();
        ApexPages.StandardController sc = new ApexPages.StandardController(CaseTest1[0]);

        CaseExtension CaseExt = new CaseExtension(sc);
        CaseExt.updateCase();
    test.stopTest();

}
1
  • I tried it but the error persists. It seems that the case list I passed to updatecase is null
    – begginers
    Jan 12, 2018 at 7:50
0

This is my updated class it is now working properly. The list that I was passing was null.

@isTest

public class CaseExtensionTest {

       Static testMethod void caseExt() {

        List<BusinessHours> bHours = CaseTat_DataFactory_Test.bHours();
        list<User> lUser = CaseTat_DataFactory_Test.lUser();
        list<Account> lAccounts = CaseTat_DataFactory_Test.createAccounts(1);
        list<Contract__c> lContracts = CaseTat_DataFactory_Test.createContracts(1);
        map<String, muteCS__c> muteCS = CaseTat_DataFactory_Test.muteCS();

        String uniqueUserName = 'unique' + DateTime.now().getTime() + '@testorg.com';
        // This code runs as the system user
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        User u = new User(Alias = 'standt', Email='[email protected]',
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles',
         UserName=uniqueUserName);

        System.runAs(u) {
              // The following code runs as user 'u'
              System.debug('Current User: ' + UserInfo.getUserName());
              System.debug('Current Profile: ' + UserInfo.getProfileId());
          }

            /* ===================== Create a Case ==========================*/

            Case c = new Case();
            c.Subject = 'Subject'; 
            c.Description = 'Description';
            c.Viewed_by_Handler__c = false;
            c.Case_Brand_Handler__c = 'AMICASSA';
            c.Transaction_Group__c = 'FOR PROBING';
            c.Transaction_Item__c= 'FOR PROBING';
            c.Type = 'Inquiry';
            c.OwnerId = UserInfo.getUserId();
            c.Escalated_From_Handler__c = UserInfo.getUserId();
            c.AccountId =  lAccounts[0].Id;         
            c.Related_Contract__c = lContracts[0].Id;
            c.X24Hr_TAT_End_TimeStamp__c = DateTime.Now.addDays(1);
            c.X24Hr_TAT_Start_TimeStamp__c = DateTime.Now();
            c.TAT_in_Progress__c = 123;
            c.
            insert c;

            test.startTest();
            ApexPages.StandardController sc = new ApexPages.StandardController(c);
            CaseExtension CaseExt = new CaseExtension(sc);
            CaseExt.updateCase();

            System.assertEquals(true, c.Viewed_by_Handler__c = TRUE);
            test.stopTest();

    }   

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .