0

I am sorry if this has been answered here before, I was not able to find an answer.

I made a small modification[changed an if condition, no new lines added or removed] to one of the apex classes in the sandbox. I created a changeset to deploy that to production, for some reason I am getting the code coverage failure error message. I tried deploying it with run specified test as well, still didn't help. Below is the snip of the error message.

enter image description here

I need this to be pushed to production as soon as possible. Any idea how to make this work?

Edited - Here is the if condition I modified and I am including the pre-written test class here as well, please advise!

**APEX CONTROLLER CLASS**
    @AuraEnabled
  public static List<SimpleAccountBudget> getAccountBudgets(Id accountId) {
    List<SimpleAccountBudget> accountBudgets = new List<SimpleAccountBudget>();
    Date today = Date.today();
    List<String> years = new List<String>();
     //if (today.month() == 1 || today.month() == 2) { backup for budget change
    if (today.month() == 1) {//changed to this IF from the above line
      years.add(String.valueof(today.year() - 1));
    }else{
      years.add(String.valueof(today.year()));
    }
  
    AggregateResult[] budgetTotals = [SELECT Account__c, Year__c, SUM(Amount__c) Amount FROM Coop_Budget__c WHERE Account__c = :accountId AND Year__c IN :years GROUP BY Account__c, Year__c];
    AggregateResult[] paymentTotals = [SELECT Account__c, Coop_Budget_Year__c, SUM(Amount_Payable__c) Amount FROM Payment_Request__c WHERE Account__c = :accountId AND Status__c = 'Approved' AND Coop_Budget_Year__c IN :years GROUP BY Account__c, Coop_Budget_Year__c];
    Map<String, SimpleAccountBudget> calculatedAccountBudgets = calculateAccountBudgets(budgetTotals, paymentTotals);
    for (String year : years) {
      String budgetKey = (String)accountId + '-' + year;
      if (calculatedAccountBudgets.containsKey(budgetKey)) {
        accountBudgets.add(calculatedAccountBudgets.get(budgetKey));
      } else {
        SimpleAccountBudget accountBudget = new SimpleAccountBudget();
        accountBudget.AccountId = accountId;
        accountBudget.Year = year;
        accountBudgets.add(accountBudget);
      }
    }
    return accountBudgets;
  }

TEST CLASS
@isTest
private class PaymentRequestControllerTests {
  @isTest static void testGetPaymentRequests() {
    User u = createUsersAndReturnOne();
    System.runAs(u) {
      Coop_Campaign__c coopCampaign = createCoopCampaign();
      CoopCampaignController.submitForPaymentReview(coopCampaign.Id);
      CoopCampaignController.approvePaymentRequests(coopCampaign.Id);

      PaymentRequestController.SimplePaymentRequests simplePaymentRequests = PaymentRequestController.getPaymentRequests();
      System.assert(simplePaymentRequests.PaymentRequests.size() > 0);
    }
  }

  @isTest static void testGetPaymentRequest() {
    User u = createUsersAndReturnOne();
    System.runAs(u) {
      Coop_Campaign__c coopCampaign = createCoopCampaign();
      Payment_Request__c paymentRequest = [SELECT Id FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
      PaymentRequestController.SimplePaymentRequest simplePaymentRequest = PaymentRequestController.getPaymentRequest(paymentRequest.Id);
      System.assert(simplePaymentRequest.Id == paymentRequest.Id);
    }
  }

  @isTest static void testGetPartialInvoices() {
    User u = createUsersAndReturnOne();
    System.runAs(u) {
      Coop_Campaign__c coopCampaign = createCoopCampaign();
      List<PaymentRequestController.SimpleInvoice> invoices = PaymentRequestController.getPartialInvoices(coopCampaign.Account__c);
      System.assert(invoices.size() == 1);
    }
  }

  @isTest static void testApprovePaymentRequest() {
    User u = createUsersAndReturnOne();
    System.runAs(u) {
      Coop_Campaign__c coopCampaign = createCoopCampaign();
      CoopCampaignController.submitForPaymentReview(coopCampaign.Id);
      CoopCampaignController.approvePaymentRequests(coopCampaign.Id);

      Payment_Request__c paymentRequest = [SELECT Id, Status__c FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
      PaymentRequestController.approvePaymentRequest(paymentRequest.Id);
      paymentRequest = [SELECT Id, Status__c, Amount_Payable__c FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
      System.assert(paymentRequest.Status__c == 'Approved');

      List<PaymentRequestController.SimpleAccountBudget> accountBudgets = PaymentRequestController.getAccountBudgets(coopCampaign.Account__c);
      PaymentRequestController.SimpleAccountBudget accountBudget = accountBudgets[0];
      //System.assert(accountBudget.Year == String.valueOf(Date.today().year()));
    }
  }

  @isTest static void testCancelPaymentRequest() {
    User u = createUsersAndReturnOne();
    System.runAs(u) {
      Coop_Campaign__c coopCampaign = createCoopCampaign();
      Payment_Request__c paymentRequest = [SELECT Id, Status__c FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
      PaymentRequestController.cancelPaymentRequest(paymentRequest.Id);
      paymentRequest = [SELECT Id, Status__c FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
      Coop_Campaign__c updatedCoopCampaign = [SELECT Id, Payment_Status__c FROM Coop_Campaign__c WHERE Id = :coopCampaign.Id];
      System.assert(paymentRequest.Status__c == 'Cancelled');
      System.assert(updatedCoopCampaign.Payment_Status__c == 'Not Paid');
    }
  }

  @isTest static void testSetPaymentCodes() {
    User u = createUsersAndReturnOne();
    System.runAs(u) {
      Coop_Campaign__c coopCampaign = createCoopCampaign();
      Payment_Request__c paymentRequest = [SELECT Id, Status__c FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
      PaymentRequestController.setPaymentCodes(paymentRequest.Id, '12', '2018');
      paymentRequest = [SELECT Id, Payment_Code__c FROM Payment_Request__c WHERE Id = :paymentRequest.Id];
      System.assert(paymentRequest.Payment_Code__c == 'COOP2018-123412');
    }
  }

  private static Coop_Campaign__c createCoopCampaign() {
    Account account = new Account(Dealer_Code__c = '1234', Name = 'Test Account');
    insert account;

    Coop_Budget__c coopBudget = new Coop_Budget__c();
    coopBudget.Account__c = account.Id;
    coopBudget.Amount__c = 1000;
    coopBudget.Month__c = 1;
    coopBudget.Year__c = '2018';
    insert coopBudget;

    Coop_Campaign__c coopCampaign = new Coop_Campaign__c();
    coopCampaign.Account__c = account.Id;
    coopCampaign.Media_Type__c = 'Radio';
    coopCampaign.Campaign_Status__c = 'Draft';
    coopCampaign.Campaign_Start_Date__c = Date.newInstance(2018, 1, 1);
    coopCampaign.Campaign_End_Date__c = Date.newInstance(2018, 1, 1);
    coopCampaign.Payment_Status__c = 'Pending DBM Approval';
    insert coopCampaign;

    Creative__c creative = new Creative__c();
    creative.Name = 'Test Creative';
    creative.Media_Type__c = 'Radio';
    creative.Expiry_Date__c = Date.today().addDays(1);
    insert creative;

    ContentVersion newContentVersion = new ContentVersion();
    newContentVersion.Title = 'Test';
    newContentVersion.PathOnClient = 'Test.jpg';
    newContentVersion.VersionData = Blob.valueOf('Test');
    insert newContentVersion;

    ContentVersion contentVersion = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = :newContentVersion.Id];

    ContentDocumentLink newContentDocumentLink = new ContentDocumentLink();
    newContentDocumentLink.ContentDocumentId = contentVersion.ContentDocumentId;
    newContentDocumentLink.LinkedEntityId = creative.Id;
    newContentDocumentLink.ShareType = 'V';
    insert newContentDocumentLink;

    Creative_Request__c creativeRequest = new Creative_Request__c();
    creativeRequest.Account__c = account.Id;
    creativeRequest.Coop_Campaign__c = coopCampaign.Id;
    creativeRequest.Creative__c = creative.Id;
    creativeRequest.Customization_Required__c = true;
    insert creativeRequest;

    Invoice__c invoice = new Invoice__c();
    invoice.Account__c = account.Id;
    invoice.Name = 'Test Invoice';
    invoice.Subtotal_Amount__c = 100;
    invoice.Tax_Rate__c = 0.13;
    invoice.Total_Amount__c = 113;
    insert invoice;

    Payment_Request__c paymentRequest = new Payment_Request__c();
    paymentRequest.Account__c = account.Id;
    paymentRequest.Amount_Claimed__c = 50;
    paymentRequest.Coop_Campaign__c = coopCampaign.Id;
    paymentRequest.Coop_Budget_Year__c = String.valueOf(Date.today().year());
    paymentRequest.Claim_Start_Date__c = Date.newInstance(2018, 1, 1);
    paymentRequest.Claim_End_Date__c = Date.newInstance(2018, 1, 1);
    paymentRequest.Invoice__c = invoice.Id;
    insert paymentRequest;

    return coopCampaign;
  }

  private static User createUsersAndReturnOne() {
    createUser('Brand User');
    createUser('Finance User');
    createUser('Dealer Business Manager');
    return createUser('Acquisition User');
  }

  private static User createUser(String profileName) {
    String uniqueUserName = 'standarduser' + DateTime.now().getTime() + '@testorg.com';
    Profile profile = [SELECT Id FROM Profile WHERE Name=:profileName];
    UserRole userRole = [SELECT Id FROM UserRole WHERE Name=:profileName];
    User user = new User(
      Alias = 'standt',
      Email = '[email protected]',
      EmailEncodingKey = 'UTF-8',
      LastName = 'Testing',
      LanguageLocaleKey = 'en_US',
      LocaleSidKey = 'en_US',
      ProfileId = profile.Id,
      TimeZoneSidKey = 'America/Los_Angeles',
       UserName = uniqueUserName,
      UserRoleId = userRole.Id
    );
    insert user;
    return user;
  }
}

Edited after test class modification

After modification, ran the test multiple times, below is the snip of the message I saw on the logs inside developer console - enter image description here

Edited after the finding the correct picklist values

These are the values I see for the Media_Type__c picklist for both the objects - enter image description here

I went ahead and modified that part of the test code to this way -

Coop_Campaign__c coopCampaign = new Coop_Campaign__c();
coopCampaign.Account__c = account.Id;
//coopCampaign.Media_Type__c = 'Radio';
coopCampaign.Media_Type__c = 'Print';
coopCampaign.Campaign_Status__c = 'Draft';
coopCampaign.Campaign_Start_Date__c = Date.newInstance(2019, 1, 1);
coopCampaign.Campaign_End_Date__c = Date.newInstance(2020, 1, 1);
coopCampaign.Payment_Status__c = 'Pending DBM Approval';
insert coopCampaign;

Coop_Campaign__c coopCampaign1 = new Coop_Campaign__c();
coopCampaign1.Account__c = account.Id;
//coopCampaign1.Media_Type__c = 'Radio';
coopCampaign.Media_Type__c = 'Print';
coopCampaign1.Campaign_Status__c = 'Draft';
coopCampaign1.Campaign_Start_Date__c = Date.newInstance(2018, 2, 2);
coopCampaign1.Campaign_End_Date__c = Date.newInstance(2018, 2, 2);
coopCampaign1.Payment_Status__c = 'Pending DBM Approval';
insert coopCampaign1;

Creative__c creative = new Creative__c();
creative.Name = 'Test Creative';
//creative.Media_Type__c = 'Radio';
creative.Media_Type__c = 'Print';
creative.Expiry_Date__c = Date.today().addDays(1);
insert creative;

Now the error I get in the logs is enter image description here

6
  • Have you tried running all tests during the deployment? Commented Feb 1, 2021 at 15:54
  • Make sure all your test classes have 75% coverage. I guess it's having 22%. As you have update your code added IF condition. Check whether your test class code is covering this IF condition.
    – Prathyusha
    Commented Feb 1, 2021 at 15:56
  • @Prathyusha new to all this, how do I make sure if the IF condition is covered in the test class. I haven't written any test classes, I am just running the one that was already there. I have added the method that contains the if condition and the whole test class.
    – sumchans
    Commented Feb 1, 2021 at 16:06
  • @user1067017 yes i did, still ended up in error
    – sumchans
    Commented Feb 1, 2021 at 16:07
  • @sumchans, Open Developer console -> Open your test class -> Click on Run Test button , then open your class in developer console on top left corner there would be a drag down named Code Coverage. Click on 22% one you would know what all is covered in class.
    – Prathyusha
    Commented Feb 1, 2021 at 16:11

1 Answer 1

2

Try to update your test class as below and then run test class again and check, let me know any error:

@isTest
private class PaymentRequestControllerTests {
  @isTest static void testGetPaymentRequests() {
    User u = createUsersAndReturnOne();
    System.runAs(u) {
      Coop_Campaign__c coopCampaign = createCoopCampaign();
      CoopCampaignController.submitForPaymentReview(coopCampaign.Id);
      CoopCampaignController.approvePaymentRequests(coopCampaign.Id);

      PaymentRequestController.SimplePaymentRequests simplePaymentRequests = PaymentRequestController.getPaymentRequests();
      System.assert(simplePaymentRequests.PaymentRequests.size() > 0);
    }
  }

  @isTest static void testGetPaymentRequest() {
    User u = createUsersAndReturnOne();
    System.runAs(u) {
      Coop_Campaign__c coopCampaign = createCoopCampaign();
      Payment_Request__c paymentRequest = [SELECT Id FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
      PaymentRequestController.SimplePaymentRequest simplePaymentRequest = PaymentRequestController.getPaymentRequest(paymentRequest.Id);
      System.assert(simplePaymentRequest.Id == paymentRequest.Id);
    }
  }

  @isTest static void testGetPartialInvoices() {
    User u = createUsersAndReturnOne();
    System.runAs(u) {
      Coop_Campaign__c coopCampaign = createCoopCampaign();
      List<PaymentRequestController.SimpleInvoice> invoices = PaymentRequestController.getPartialInvoices(coopCampaign.Account__c);
      System.assert(invoices.size() == 1);
    }
  }

  @isTest static void testApprovePaymentRequest() {
    User u = createUsersAndReturnOne();
    System.runAs(u) {
      Coop_Campaign__c coopCampaign = createCoopCampaign();
      CoopCampaignController.submitForPaymentReview(coopCampaign.Id);
      CoopCampaignController.approvePaymentRequests(coopCampaign.Id);

      Payment_Request__c paymentRequest = [SELECT Id, Status__c FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
      PaymentRequestController.approvePaymentRequest(paymentRequest.Id);
      paymentRequest = [SELECT Id, Status__c, Amount_Payable__c FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
      System.assert(paymentRequest.Status__c == 'Approved');

      List<PaymentRequestController.SimpleAccountBudget> accountBudgets = PaymentRequestController.getAccountBudgets(coopCampaign.Account__c);
      PaymentRequestController.SimpleAccountBudget accountBudget = accountBudgets[0];
      //System.assert(accountBudget.Year == String.valueOf(Date.today().year()));
    }
  }

  @isTest static void testCancelPaymentRequest() {
    User u = createUsersAndReturnOne();
    System.runAs(u) {
      Coop_Campaign__c coopCampaign = createCoopCampaign();
      Payment_Request__c paymentRequest = [SELECT Id, Status__c FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
      PaymentRequestController.cancelPaymentRequest(paymentRequest.Id);
      paymentRequest = [SELECT Id, Status__c FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
      Coop_Campaign__c updatedCoopCampaign = [SELECT Id, Payment_Status__c FROM Coop_Campaign__c WHERE Id = :coopCampaign.Id];
      System.assert(paymentRequest.Status__c == 'Cancelled');
      System.assert(updatedCoopCampaign.Payment_Status__c == 'Not Paid');
    }
  }

  @isTest static void testSetPaymentCodes() {
    User u = createUsersAndReturnOne();
    System.runAs(u) {
      Coop_Campaign__c coopCampaign = createCoopCampaign();
      Payment_Request__c paymentRequest = [SELECT Id, Status__c FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
      PaymentRequestController.setPaymentCodes(paymentRequest.Id, '12', '2018');
      paymentRequest = [SELECT Id, Payment_Code__c FROM Payment_Request__c WHERE Id = :paymentRequest.Id];
      System.assert(paymentRequest.Payment_Code__c == 'COOP2018-123412');
    }
  }

  private static Coop_Campaign__c createCoopCampaign() {
    Account account = new Account(Dealer_Code__c = '1234', Name = 'Test Account');
    insert account;

    Coop_Budget__c coopBudget = new Coop_Budget__c();
    coopBudget.Account__c = account.Id;
    coopBudget.Amount__c = 1000;
    coopBudget.Month__c = 1;
    coopBudget.Year__c = '2019';
    insert coopBudget;
    
    Coop_Budget__c coopBudget1 = new Coop_Budget__c();
    coopBudget1.Account__c = account.Id;
    coopBudget1.Amount__c = 1000;
    coopBudget1.Month__c = 1;
    coopBudget1.Year__c = '2020';
    insert coopBudget1;

    Coop_Campaign__c coopCampaign = new Coop_Campaign__c();
    coopCampaign.Account__c = account.Id;
    coopCampaign.Media_Type__c = 'Radio';
    coopCampaign.Campaign_Status__c = 'Draft';
    coopCampaign.Campaign_Start_Date__c = Date.newInstance(2019, 1, 1);
    coopCampaign.Campaign_End_Date__c = Date.newInstance(2020, 1, 1);
    coopCampaign.Payment_Status__c = 'Pending DBM Approval';
    insert coopCampaign;
    
    Coop_Campaign__c coopCampaign1 = new Coop_Campaign__c();
    coopCampaign1.Account__c = account.Id;
    coopCampaign1.Media_Type__c = 'Radio';
    coopCampaign1.Campaign_Status__c = 'Draft';
    coopCampaign1.Campaign_Start_Date__c = Date.newInstance(2018, 2, 2);
    coopCampaign1.Campaign_End_Date__c = Date.newInstance(2018, 2, 2);
    coopCampaign1.Payment_Status__c = 'Pending DBM Approval';
    insert coopCampaign1;

    Creative__c creative = new Creative__c();
    creative.Name = 'Test Creative';
    creative.Media_Type__c = 'Radio';
    creative.Expiry_Date__c = Date.today().addDays(1);
    insert creative;

    ContentVersion newContentVersion = new ContentVersion();
    newContentVersion.Title = 'Test';
    newContentVersion.PathOnClient = 'Test.jpg';
    newContentVersion.VersionData = Blob.valueOf('Test');
    insert newContentVersion;

    ContentVersion contentVersion = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = :newContentVersion.Id];

    ContentDocumentLink newContentDocumentLink = new ContentDocumentLink();
    newContentDocumentLink.ContentDocumentId = contentVersion.ContentDocumentId;
    newContentDocumentLink.LinkedEntityId = creative.Id;
    newContentDocumentLink.ShareType = 'V';
    insert newContentDocumentLink;

    Creative_Request__c creativeRequest = new Creative_Request__c();
    creativeRequest.Account__c = account.Id;
    creativeRequest.Coop_Campaign__c = coopCampaign.Id;
    creativeRequest.Creative__c = creative.Id;
    creativeRequest.Customization_Required__c = true;
    insert creativeRequest;

    Invoice__c invoice = new Invoice__c();
    invoice.Account__c = account.Id;
    invoice.Name = 'Test Invoice';
    invoice.Subtotal_Amount__c = 100;
    invoice.Tax_Rate__c = 0.13;
    invoice.Total_Amount__c = 113;
    insert invoice;

    Payment_Request__c paymentRequest = new Payment_Request__c();
    paymentRequest.Account__c = account.Id;
    paymentRequest.Amount_Claimed__c = 50;
    paymentRequest.Coop_Campaign__c = coopCampaign.Id;
    paymentRequest.Coop_Budget_Year__c = String.valueOf(Date.today().year());
    paymentRequest.Claim_Start_Date__c = Date.newInstance(2018, 1, 1);
    paymentRequest.Claim_End_Date__c = Date.newInstance(2018, 1, 1);
    paymentRequest.Invoice__c = invoice.Id;
    insert paymentRequest;

    return coopCampaign;
  }

  private static User createUsersAndReturnOne() {
    createUser('Brand User');
    createUser('Finance User');
    createUser('Dealer Business Manager');
    return createUser('Acquisition User');
  }

  private static User createUser(String profileName) {
    String uniqueUserName = 'standarduser' + DateTime.now().getTime() + '@testorg.com';
    Profile profile = [SELECT Id FROM Profile WHERE Name=:profileName];
    UserRole userRole = [SELECT Id FROM UserRole WHERE Name=:profileName];
    User user = new User(
      Alias = 'standt',
      Email = '[email protected]',
      EmailEncodingKey = 'UTF-8',
      LastName = 'Testing',
      LanguageLocaleKey = 'en_US',
      LocaleSidKey = 'en_US',
      ProfileId = profile.Id,
      TimeZoneSidKey = 'America/Los_Angeles',
       UserName = uniqueUserName,
      UserRoleId = userRole.Id
    );
    insert user;
    return user;
  }
}
12
  • Should I do this in production or Sandbox?
    – sumchans
    Commented Feb 1, 2021 at 16:21
  • 1
    Update in Sandbox
    – Prathyusha
    Commented Feb 1, 2021 at 16:22
  • Maybe I am thinking wrong, I see that you added one more insert statement to the createCoopCampaign method, actually with the new deployment we are trying to remove the second month from the condition. The requested ID condition as of now is if(today.month ==1) {}.
    – sumchans
    Commented Feb 1, 2021 at 16:36
  • 1
    Check the correct value for Media_Type__c field Is it Radio ?? for both objects coopCampaign and creative objects. replace Radio with correct one
    – Prathyusha
    Commented Feb 1, 2021 at 16:53
  • 1
    Yes create outbound changeset and deploy to prod
    – Prathyusha
    Commented Feb 1, 2021 at 17:37

You must log in to answer this question.

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