1

I am trying to deploy some custom fields and custom settings to production, however they keep failing on the same testclass. The class passes fine when run in both prod and sandbox but fails when validation deployment everytime.

I do not know why (this is code I have taken over) and am finding it hard to understand how to fix it when reading documentation

Below is the code in production the error is attributed to line 161

    @isTest(SeeAllData=true)
private class Account_To_Be_Deleted_TestClass {

    static testMethod void myUnitTest() {
/*
        List<Account> newAccs = new List<Account>();
        for (Integer i = 0; i < 102; i++) {
            Account a = new Account();
            a.Name = 'Delete Test: ' + i;
            newAccs.add(a);
        }
        insert(newAccs);

        List<Contact> newCnts = new List<Contact>();
        for (Integer i = 0; i < 52; i++) {
            Contact c = new Contact();
            c.LastName = 'Delete Contact Test ' + i;
            c.AccountId = newAccs[i].Id;
            newCnts.add(c);
        }
        insert(newCnts);

        // Insert Task to multiple Contacts
        List<Task> newTasks = new List<Task>();
        for (Integer i = 0; i < 26; i+=2) {
            Task t = new Task();
            t.WhoId = newCnts[i].Id;
            t.Status = 'Not Started';
            if (i == 4 || i == 10) {
                t.Status = 'Completed';
            } 
            newTasks.add(t);
        }

        // Insert multiple Tasks to the one Contact
        for (Integer i = 0; i < 10; i++) {
            Task t = new Task();
            t.WhoId = newCnts[27].Id;
            t.Status = 'Not Started';
            if (i == 4 || i == 10) {
                t.Status = 'Completed';
            } 
            newTasks.add(t);
        }        

        // Insert Tasks to multiple Accounts
        for (Integer i = 1; i < 26; i+=2) {
            Task t = new Task();
            t.WhatId = newAccs[i].Id;
            t.Status = 'Not Started';
            if (i == 4 || i == 10) {
                t.Status = 'Completed';
            }
            newTasks.add(t);
        }        

        // Insert multiple Tasks to the one Account
        for (Integer i = 1; i < 26; i+=2) {
            Task t = new Task();
            t.WhatId = newAccs[27].Id;
            t.Status = 'Not Started';
            if (i == 4 || i == 10) {
                t.Status = 'Completed';
            }
            newTasks.add(t);
        }        

        // Insert all the Tasks created
        insert(newTasks);

        // Insert Events to multiple Contacts
        List<Event> newEvents = new List<Event>();
        for (Integer i = 27; i < 52; i+=2) {
            Event e = new Event();
            e.WhoId = newCnts[i].Id;
            e.DurationInMinutes = 60;
            e.ActivityDateTime = DateTime.now();
            newEvents.add(e);
        }

        // Insert multiple Events to one Contact
        for (Integer i = 0; i < 10; i++) {
            Event e = new Event();
            e.WhoId = newCnts[51].Id;
            e.DurationInMinutes = 60;
            e.ActivityDateTime = DateTime.now();
            newEvents.add(e);
        }

        // Insert Events to multiple Accounts        
        for (Integer i = 28; i < 52; i+=2) {
            Event e = new Event();
            e.WhatId = newAccs[i].Id;
            e.DurationInMinutes = 60;
            e.ActivityDateTime = DateTime.now();
            newEvents.add(e);
        }        

        // Insert a group of Events all to the one Account
        for (Integer i = 0; i < 10; i++) {
            Event e = new Event();
            e.WhatId = newAccs[53].Id;
            e.DurationInMinutes = 60;
            e.ActivityDateTime = DateTime.now();
            newEvents.add(e);
        }        

        // Isert all the Events created
        insert(newEvents);         

        List<Opportunity> newOpps = new List<Opportunity>();
        for (Integer i = 52; i < 102; i++) {
            Opportunity o = new Opportunity();
            o.Name = newAccs[i].Name +  ' - Opportunity ' + i;
            o.StageName = 'F - First Phase Open';
            o.CloseDate = Date.Today();
            o.AccountId = newAccs[i].Id;
            if (i == 60 || i == 75) {
                o.AccountId = newAccs[i-1].Id;
            }
            newOpps.add(o);
        }
        insert(newOpps);         

        for (Account a : newAccs) {
            a.To_Be_Deleted__c = true;
        }           

        try {
            update(newAccs);
        } catch (DmlException e) {
            System.assert(e.getDmlMessage(0).indexOf('Contacts tied to this Account contain uncompleted Tasks. The Account cannot be deleted') > -1); 
        }
*/

    RecordType accRec = [SELECT Id FROM RecordType WHERE Name = 'UK Customer Account Record Type' LIMIT 1];

    // TESTING ACCOUNT WITH NOTHING. RESULT. ACCOUNT DELETED
    Account acc1 = new Account(Name='Delete Account 1');
    acc1.Exact_Debtor_Number__c = 'eteffs';
    acc1.Exact_Company_Number__c = 507.0;
    acc1.RecordTypeId = accRec.Id;
    acc1.BillingStreet = 'Street1';
    acc1.BillingCity = 'City1';
    acc1.BillingState = 'State1';
    acc1.BillingCountry = 'Country1';
    acc1.Billing_County__c = 'County1';
    acc1.ShippingStreet = 'Street1';
    acc1.ShippingCity = 'City1';
    acc1.ShippingState = 'State1';
    acc1.ShippingCountry = 'Country1';
    acc1.Shipping_County__c = 'County1';
    insert(acc1);
    PageReference pageRef1 = Page.Validate_Deletion;
    ApexPages.currentPage().getParameters().put('id', acc1.Id);
    Account_To_Be_Deleted_Methods atbd1 = new Account_To_Be_Deleted_Methods(new ApexPages.StandardController(acc1));
    System.assertEquals(atbd1.getName(), acc1.Name);
    String nextPage = atbd1.deleteAccount().getUrl();

    // TESTING ACCOUNT WITH A CLOSED OPPORTUNITY. RESULT. HIDDEN ACCOUNT OWNER
    Opportunity opp2 = [SELECT Id, AccountId FROM Opportunity WHERE IsClosed = true LIMIT 1];
    Account acc2 = new Account (Id=opp2.AccountId);
    acc2.RecordTypeId = accRec.Id;
    acc2.BillingStreet = 'Street2';
    acc2.BillingCity = 'City2';
    acc2.BillingState = 'State2';
    acc2.BillingCountry = 'Country2';
    acc2.Billing_County__c = 'County2';
    acc2.ShippingStreet = 'Street2';
    acc2.ShippingCity = 'City2';
    acc2.ShippingState = 'State2';
    acc2.ShippingCountry = 'Country2';
    acc2.Shipping_County__c = 'County2';    
    PageReference pageRef2 = Page.Validate_Deletion;
    ApexPages.currentPage().getParameters().put('id', acc2.Id);
    Account_To_Be_Deleted_Methods atbd2 = new Account_To_Be_Deleted_Methods(new ApexPages.StandardController(acc2));
    //nextPage = atbd2.deleteAccount().getUrl();
    //System.assertEquals(atbd2.deleteAccount().getUrl(), null);      

    // TESTING ACCOUNT WITH AN OPEN OPPORTUNITY. RESULT. ERROR MESSAGE
    Account acc3 = new Account (Name='Delete Account 3');
    acc3.RecordTypeId = accRec.Id;
    insert (acc3);
    Opportunity opp3 = new Opportunity(AccountId=acc3.Id);
    opp3.Name = 'Delete Opp 3';
    opp3.CloseDate = Date.Today() + 365;
    opp3.StageName = 'F- First Phase Open';
    insert(opp3);    
    PageReference pageRef3 = Page.Validate_Deletion;
    ApexPages.currentPage().getParameters().put('id', acc3.Id);
    Account_To_Be_Deleted_Methods atbd3 = new Account_To_Be_Deleted_Methods(new ApexPages.StandardController(acc3));
    System.assertEquals(atbd3.deleteAccount(), null);
    System.assertEquals(atbd3.getHasOpenOpps(), true);
    System.assertEquals(atbd3.getOpenOpps().size(), 1);
    // nextPage = atbd3.deleteAccount().getUrl();  

    // TESTING ACCOUNT WITH A CLOSED EVENT. RESULT. HIDDEN ACCOUNT OWNER
    Account acc4 = new Account ();
    acc4.Name = 'Delete Account 4';
    acc4.BillingStreet = 'Street4';
    acc4.BillingCity = 'City4';
    acc4.BillingState = 'State4';
    acc4.BillingCountry = 'Country4';
    acc4.Billing_County__c = 'County4';
    acc4.ShippingStreet = 'Street4';
    acc4.ShippingCity = 'City4';
    acc4.ShippingState = 'State4';
    acc4.ShippingCountry = 'Country4';
    acc4.Shipping_County__c = 'County4';    
    acc4.RecordTypeId = accRec.Id;
    insert(acc4);
    Event evt4 = new Event();
    evt4.StartDateTime = DateTime.newInstance(Date.Today() - 5, Time.newInstance(11, 11, 11, 11));
    evt4.EndDateTime = DateTime.newInstance(Date.Today() - 3, Time.newInstance(11, 11, 11, 11));
    evt4.WhatId = acc4.Id;
    insert(evt4);
    PageReference pageRef4 = Page.Validate_Deletion;
    ApexPages.currentPage().getParameters().put('id', acc4.Id);
    Account_To_Be_Deleted_Methods atbd4 = new Account_To_Be_Deleted_Methods(new ApexPages.StandardController(acc4));
    System.assertEquals(atbd4.deleteAccount(), null);
    // nextPage = atbd4.deleteAccount().getUrl();  

    // TESTING ACCOUNT WITH A OPEN EVENT. RESULT. ERROR MESSAGE
    Account acc5 = new Account ();
    acc5.Name = 'Delete Account 5';
    acc5.BillingStreet = 'Street5';
    acc5.BillingCity = 'City5';
    acc5.BillingState = 'State5';
    acc5.BillingCountry = 'Country5';
    acc5.Billing_County__c = 'County5';
    acc5.ShippingStreet = 'Street5';
    acc5.ShippingCity = 'City5';
    acc5.ShippingState = 'State5';
    acc5.ShippingCountry = 'Country5';
    acc5.Shipping_County__c = 'County5';    
    acc5.Exact_Company_Number__c = 510.0;
    acc5.RecordTypeId = accRec.Id;
    insert(acc5);
    Event evt5 = new Event();
    evt5.StartDateTime = DateTime.newInstance(Date.Today() + 3, Time.newInstance(11, 11, 11, 11));
    evt5.EndDateTime = DateTime.newInstance(Date.Today() + 5, Time.newInstance(11, 11, 11, 11));
    evt5.WhatId = acc5.Id;
    insert(evt5);
    PageReference pageRef5 = Page.Validate_Deletion;
    ApexPages.currentPage().getParameters().put('id', acc5.Id);
    Account_To_Be_Deleted_Methods atbd5 = new Account_To_Be_Deleted_Methods(new ApexPages.StandardController(acc5));
    System.assertEquals(atbd5.deleteAccount(), null);
    System.assertEquals(atbd5.getHasOpenEvents(), true);
    System.assertEquals(atbd5.getOpenEvts().size(), 1);    
    // nextPage = atbd4.deleteAccount().getUrl();  

    // TESTING ACCOUNT WITH A CLOSED TASK. RESULT. HIDDEN ACCOUNT OWNER
    Account acc6 = new Account ();
    acc6.Name = 'Delete Account 6';
    acc6.BillingStreet = 'Street6';
    acc6.BillingCity = 'City6';
    acc6.BillingState = 'State6';
    acc6.BillingCountry = 'Country6';
    acc6.Billing_County__c = 'County6';
    acc6.ShippingStreet = 'Street6';
    acc6.ShippingCity = 'City6';
    acc6.ShippingState = 'State6';
    acc6.ShippingCountry = 'Country6';
    acc6.Shipping_County__c = 'County6';    
    acc6.RecordTypeId = accRec.Id;
    insert(acc6);
    Task tsk6 = new Task();
    tsk6.Status = 'Completed';
    tsk6.WhatId = acc6.Id;
    insert(tsk6);
    PageReference pageRef6 = Page.Validate_Deletion;
    ApexPages.currentPage().getParameters().put('id', acc6.Id);
    Account_To_Be_Deleted_Methods atbd6 = new Account_To_Be_Deleted_Methods(new ApexPages.StandardController(acc6));
    System.assertEquals(atbd6.deleteAccount(), null);
    // nextPage = atbd4.deleteAccount().getUrl();  

    // TESTING ACCOUNT WITH A OPEN TASK. RESULT. ERROR MESSAGE
    Account acc7 = new Account ();
    acc7.Name = 'Delete Account 7';
    acc7.BillingStreet = 'Street1';
    acc7.BillingCity = 'City1';
    acc7.BillingState = 'State1';
    acc7.BillingCountry = 'Country1';
    acc7.Billing_County__c = 'County1';
    acc7.ShippingStreet = 'Street1';
    acc7.ShippingCity = 'City1';
    acc7.ShippingState = 'State1';
    acc7.ShippingCountry = 'Country1';
    acc7.Shipping_County__c = 'County1';    
    acc7.Exact_Company_Number__c = 510.0;
    acc7.RecordTypeId = accRec.Id;
    insert(acc7);
    Task tsk7 = new Task();
    tsk7.Status = 'Not Started';
    tsk7.WhatId = acc7.Id;
    insert(tsk7);
    PageReference pageRef7 = Page.Validate_Deletion;
    ApexPages.currentPage().getParameters().put('id', acc7.Id);
    Account_To_Be_Deleted_Methods atbd7 = new Account_To_Be_Deleted_Methods(new ApexPages.StandardController(acc7));
    System.assertEquals(atbd7.deleteAccount(), null);
    System.assertEquals(atbd7.getHasOpenTasks(), true);
    System.assertEquals(atbd7.getOpenTsks().size(), 1);     

    // TESTING ERROR FOR ACCOUNT WITH DUPLICATE EXACT DEBTOR NUMBER
      //Map<String, DAX_Record_Types_To_Process__c> emeaDAXRecordTypes = DAX_Record_Types_To_Process__c.getAll(); 
      //Map<Id, RecordType> recordTypesToNotProcess = new Map<Id, RecordType>([SELECT Id FROM RecordType WHERE Name LIKE 'NA%' OR Name IN :emeaDAXRecordTypes.keySet()]);    
    //Account acc = [SELECT Id, RecordTypeId, Exact_Debtor_Number__c, Exact_Company_Number__c FROM Account WHERE Exact_Debtor_Number__c != NULL AND RecordTypeId NOT IN :recordTypesToNotProcess.keySet() LIMIT 1];
    Account acc = [SELECT Id, RecordTypeId, Exact_Debtor_Number__c, Exact_Company_Number__c FROM Account WHERE Exact_Debtor_Number__c != NULL LIMIT 1];
    Account newAcc = new Account();
    newAcc.BillingStreet = 'Street1';
    newAcc.BillingCity = 'City1';
    newAcc.BillingState = 'State1';
    newAcc.BillingCountry = 'Country1';
    newAcc.Billing_County__c = 'County1';
    newAcc.ShippingStreet = 'Street1';
    newAcc.ShippingCity = 'City1';
    newAcc.ShippingState = 'State1';
    newAcc.ShippingCountry = 'Country1';
    newAcc.Shipping_County__c = 'County1';    
    newAcc.Exact_Debtor_Number__c = acc.Exact_Debtor_Number__c;
    newAcc.Exact_Company_Number__c = acc.Exact_Company_Number__c;
    newAcc.RecordTypeId = acc.RecordTypeId;
    try {
      insert(newAcc);
      System.assert(false);
    } catch (DmlException e) {
            System.assert(e.getDmlMessage(0).indexOf('An Account already exists with the same Exact Debtor Number. You cannot have another Account with the same Exact Debtor Number') > -1);
        }
    }
}

thank you for your help

1

This unit test relies on pre-existing data (SeeAllData=true), which means that the query (WHERE IsClosed = TRUE LIMIT 1) may be considered non-selective, because the system may need to do a full table scan covering more than 100k rows. The most appropriate fix, in this case, would be to create the data you need. Starting at line 165, you can change it to:

Account acc2 = new Account (RecordTypeId = accRec.Id, BillingStreet = 'Street2',
    BillingCity = 'City2', BillingState = 'State2', BillingCountry = 'Country2', 
    Billing_County__c = 'County2', ShippingStreet = 'Street2', ShippingCity = 'City2', ShippingState = 'State2', 
    ShippingCountry = 'Country2', Shipping_County__c = 'County2');
insert acc2;
Opportunity opp2 = new Opportunity(AccountId=acc2.Id, Name='Test', CloseDate=Date.Today(),
    StageName='Closed/Won'); // Replace Closed/Won with your own Closed value
insert opp2;

This produces the data your unit tests need without relying on real data. You should also consider changing SeeAllData=true to SeeAllData=false and making sure the test still passes. Your unit tests shouldn't depend on production data.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.