0

How do I cover the after undelete process in test class for the trigger. The lines which are not covering is below,

public void executeOnUnDeleteAccount(list<Account> accountNewList) {
    set<ID> parentAccountIdSet = new set<ID>();
    for (Account account : accountNewList) {
        if(account.ParentId != null) {
            // set parent Accounts
            parentAccountIdSet.add(account.ParentId);               
        }
    }

    if(!parentAccountIdSet.IsEmpty() && parentAccountIdSet != null){
        // fetch all child accounts
        fetchAllChildAccounts(parentAccountIdSet);
    }
}

  **Test class **


 @isTest
  public class Account_AggregateMRROfChildAccountsTest {
    public static testMethod void insertAccount() {
      Account parentAccount  = new Account();
      parentAccount.Name = 'Test parent Account';
      insert parentAccount;

    Account childAccount  = new Account();
    childAccount.Name = 'Test child Account';
    childAccount.ParentId = parentAccount.Id;
    insert childAccount;

    Opportunity opportunity = new Opportunity(AccountId = childAccount.Id,
                                              Name = 'OpprtuityNAme1', 
                                              StageName = 'Prospecting',
                                              CloseDate = Date.today());
    insert opportunity;

    //Created product line item for MMR__c field
    Id pricebookId = Test.getStandardPricebookId();
    Product2 p2 = new Product2(Name = 'Tes12 Product',
                               ProductCode = 'Pro-X',
                               isActive = true,
                               CanUseRevenueSchedule = true);
    insert p2;

    PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pricebookId,
                                            Product2Id = p2.Id,
                                            UnitPrice = 99,
                                            isActive=true);
    insert pbe;

    OpportunityLineItem oli = new OpportunityLineItem(PriceBookEntryId = pbe.Id,
                                                      OpportunityId = opportunity.Id,
                                                      Quantity = 1,
                                                      UnitPrice = 99);
    insert oli;
}

public static testMethod void updateAccount() {
    Account parentAccount  = new Account();
    parentAccount.Name = 'Test parent Account';
    insert parentAccount;

    Account childAccount  = new Account();
    childAccount.Name = 'Test child Account';
    childAccount.ParentId = parentAccount.Id;
    insert childAccount;

    Opportunity opportunity = new Opportunity(AccountId = childAccount.Id,
                                              Name = 'OpprtuityNAme1', 
                                              StageName = 'Prospecting',
                                              CloseDate = Date.today());
    insert opportunity;

    //Created product line item for MMR__c field
    Id pricebookId = Test.getStandardPricebookId();
    Product2 p2 = new Product2(Name = 'Tes12 Product',
                               ProductCode = 'Pro-X',
                               isActive = true,
                               CanUseRevenueSchedule = true);
    insert p2;

    PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pricebookId,
                                            Product2Id = p2.Id,
                                            UnitPrice = 99,
                                            isActive=true);
    insert pbe;

    OpportunityLineItem oli = new OpportunityLineItem(PriceBookEntryId = pbe.Id,
                                                      OpportunityId = opportunity.Id,
                                                      Quantity = 1,
                                                      UnitPrice = 99);
    insert oli;

    Opportunity opportunity2 = new Opportunity(AccountId = childAccount.Id,
                                              Name = 'OpprtuityNAme1', 
                                              StageName = 'Prospecting',
                                              CloseDate = Date.today());
    insert opportunity2;

    Product2 p = new Product2(Name = 'Tes12 Product',
                               ProductCode = 'Pro-X',
                               isActive = true,
                               CanUseRevenueSchedule = true);
    insert p;

    PricebookEntry pbe1 = new PricebookEntry(Pricebook2Id = pricebookId,
                                            Product2Id = p.Id,
                                            UnitPrice = 99,
                                            isActive=true);
    insert pbe1;

    OpportunityLineItem oli1 = new OpportunityLineItem(PriceBookEntryId = pbe1.Id,
                                                      OpportunityId = opportunity2.Id,
                                                      Quantity = 1,
                                                      UnitPrice = 99);
    insert oli1;
}

public static testMethod void deleteAccount() {
    Account parentAccount  = new Account();
    parentAccount.Name = 'Test parent Account';
    insert parentAccount;

    Account childAccount  = new Account();
    childAccount.Name = 'Test child Account';
    childAccount.ParentId = parentAccount.Id;
    insert childAccount;

    delete childAccount;

    Account[] savedAccts = [SELECT Id, Name FROM Account WHERE Name = 'test child KK 2' ALL ROWS]; 
    undelete savedAccts;
   }
 }
  • You are using the Name='Trump' in the query condition. Please replace it with the correct name. – Devendra Nov 4 '16 at 11:24
  • The account which was deleted and found in recycle bin, its name is 'test child KK 2' and i replaced it with the same. but still not covering, dont know how – Nishant Shah Nov 4 '16 at 11:29
  • Your test data contains child account name as 'Test child Account'. Seems, you need to specify this name at the time of querying account records and then perform undelete operation. – Devendra Nov 4 '16 at 11:33
0

You can go through the Restoring Deleted Records

Deleted records can be restored using the undelete operation.

Account a = new Account(Name='Trump');
insert a;

delete a;

Account[] savedAccts = [SELECT Id, Name FROM Account WHERE Name = 'Trump' ALL ROWS]; 

undelete savedAccts;
  • It is still not able to cover. – Nishant Shah Nov 4 '16 at 11:18
  • Can you please share the test class as well? – Devendra Nov 4 '16 at 11:19
  • Your test code missing the "undelete" statement. – Devendra Nov 4 '16 at 11:21
  • Updated the question with full test class . Thank you and appreciate your help – Nishant Shah Nov 4 '16 at 11:22

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.