0

I'm re-writing unit tests for the Apex Specialist superbadge (yes, re-writing, I successfully completed the superbadge a couple of days ago, and found a few bugs while playing around today, decided to try and fix them), all was fine and dandy, tests were passing, asserts were correct, until I started fixing bugs, which introduced more bugs.

First I was updating a record with the update function, then that started throwing the aforementioned error. Changed update to Database.upsert(), that worked for a while, then started failing. Then I changed Database.upsert() to just upsert, that worked for a single test run, then started failing. Then I carried out the test data setup from the test method to it's own method, annotated with @testSetup. Worked fine until an hour ago, now getting the same error.

Note that everything is working fine in the online org, I can create/edit/delete any and all of the records and the trigger gets called when it's needed and everything executes according to the superbadge requirements with absolutely no problem. I'm at a complete loss.

Here's my test class, took a lot of things out of it for now. Please don't hesitate to ask for additional information, if necessary. Full error message posted below the class.

@isTest
public class TestMaintenaceRequest {

    @testSetup
    static void setTestData(){
        Vehicle__c veh = new Vehicle__c(Name = 'Test Vehicle');
        Product2 eq = new Product2(Name = 'Generator', Maintenance_Cycle__c = 230);
        insert veh;
        insert eq;
        Case cs = new Case(Status = 'New', 
                           Origin = 'Web', 
                           Subject = 'Test Case', 
                           Type = 'Repair', 
                           Vehicle__c = veh.Id, 
                           Equipment__c = eq.Id, 
                           Date_Reported__c = Date.today());
        insert cs;
        Work_Part__c wp = new Work_Part__c(Maintenance_Request__c = cs.Id,
                                           Equipment__r = eq);
        insert wp;
    }

    @isTest
    static void testMaintenanceTriggerPositive(){
        Case cs = [SELECT Id, Subject, Type, Status, Origin,
                   Vehicle__c, Equipment__c, Date_Reported__c,
                   Vehicle__r.Id, Vehicle__r.Name, Equipment__r.Id 
                   FROM Case WHERE Status = 'New'];
        System.debug(String.valueOf(cs));
        Test.startTest();
        cs.Status = 'Closed';
        Database.UpsertResult result = Database.upsert(cs, true);
        Test.stopTest();
    }

}
20:52:17:195 EXCEPTION_THROWN [26]|System.DmlException: Upsert failed. First exception on row 0 with id 5003X00001nKPfMQAW; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, MaintenanceRequest: execution of AfterUpdate
  • Are you sure that the query in the testMaintenanceTriggerPositive() is returning a single record only since a query retrieves one or more records based on the criteria. Also it is a good idea to instantitate the cs variable to a new instance with a new keyword – Jigar Shah Nov 29 '19 at 5:15
0

Looks like there is an after update trigger written on the MaintenanceRequest object within your org where you are trying to execute this code. The trigger code is failing and causing the update to fail. If you deactivate that trigger and try again, this update within the testMaintenanceTriggerPositive() should work fine.

Also, have you verified if Closed is the correct value that is being assigned to the cs.Status picklist. You may have a restricted set of values in the picklist and you could be possibly trying to update the incorrect text value, causing the update to fail resulting in a DmlException

  • The after update trigger is required in the logic for new Maintenance Request generation after a Request has been closed. The picklist values are restricted, but I've checked and I'm assigning them correctly. And yes, the query is returning a single object, I checked with System.debug(), and I'm pretty sure I'd be getting an error of Query has more than one record for assignment to variable or something along those lines, if there would be more than one record returned. I changed the name of the variable to a unique one, but that didn't change anything. – JaPet Nov 29 '19 at 13:14
  • I see that you are using an upsert. Can you change it to update instead? – Jigar Shah Nov 29 '19 at 14:10
  • Yeah, but nothing changes, I still get an error, System.DmlException: Update failed. First exception on row 0 with id 5003X00001nKcVxQAK; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, MaintenanceRequest: execution of AfterUpdate – JaPet Nov 29 '19 at 17:43
  • Can you share the code for the after update trigger on MaintenanceRequest here to see what's going on there? – Jigar Shah Nov 29 '19 at 17:44
  • The trigger or the trigger handler? Or both? – JaPet Nov 29 '19 at 18:03
0

I found the issue. It was because of the way I was creating the Product2 eq object. I was setting only Name and Maintenance_Cycle__c when creating it, but turns out that there's another required field, Replacement_Part__c. Missing the field failed the validation, which is why the system couldn't create a number of objects after that, one of which caused the DmlException.

Ambiguous error messages are ambiguous.

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.