I am facing an issue with test class for Apex Superbadge. Here is my trigger helper class -

public class MaintenanceRequestHelper {
private static List<Case> closedCases = new List<Case>();
private static List<Case> newlyCreatedCases = new List<Case>();
private static List<Work_Part__c> newlyCreatedWorkParts = new List<Work_Part__c>();
private static List<Work_Part__c> allWorkParts = new List<Work_Part__c>();

public static void updateAfterUpdateActivity(List<Case> newCases, Map<Id, Case> oldCases){
    for(Case singleCase : newCases){

       // do stuff ............. 


private static void createFollowUpMaintenanceRequest(){
    for(Case singleCase : closedCases){
       // ....

    for(Case singleCase : closedCases){

        // .....


    //Insert all the newly created case in the system
        insert newlyCreatedCases;

 private static void updateRelatedWorkOrders(List<Case> cases){

    for(Case singleCase : cases){
       // do stuff ......    

    if(allWorkParts != null){
        for(Work_Part__c singleWorkPart : allWorkParts){

        // do stuff ....


    if(allWorkParts != null && allWorkParts.size() > 0){
        update allWorkParts;

private static Decimal findShortestMaintCycle(List<Work_Part__c> workParts){
    Decimal shortestValue;
    for(Work_Part__c singleWorkPart : workParts){

       // do stuff .......
    return shortestValue;

private static Map<Id, List<Work_Part__c>> createMaintWorkPartMap(Set<ID> caseIds){

    for(Work_Part__c singleWorkPart : allWorkParts){

         // do stuff ..........


    return maintWorkPartMap;


In the test class, if I prepare 200 test data everything is working fine, but the moment I change the test data to 201, it is failing with the error message -

System.DmlException: Update failed. First exception on row 200 with id 50036000003SjdZAAS; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, MaintenanceRequest: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0 with id 50036000003SjdaAAC; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Class.MaintenanceRequestHelper.createFollowUpMaintenanceRequest: line 61, column 1 Class.MaintenanceRequestHelper.updateAfterUpdateActivity: line 18, column 1 Trigger.MaintenanceRequest: line 3, column 1: []

If you have any idea, please let me know. Thanks in advance.


The collection newlyCreatedCases is static in the class MaintenanceRequestHelper. That means it will exist for the entire execution context and across all the trigger invocations.

Each trigger invocation will work with a maximum of 200 records. So up to the first 200 records you won't notice any difference. At 201 records the trigger will be called twice. Once for the first 200 records and then again for the remaining record. In that second invocation the newlyCreatedCases List will still have the 200 Cases from the first trigger pass. These will have Id's, and can't be inserted.

I.e. unless you specifically empty the contents of the set or create a new instance then it will still have the same contents with each trigger invocation of up to 200 records.

Try dropping the static modifier. There isn't any need to it to be static. The same probably goes for the other class level variables you have defined.

Maybe take it a step further and considering reducing the scope of all your variables. Is there any need for newlyCreatedCases to be defined at the class level? It could be moved into the method where it is used.

| improve this answer | |
  • Hi Daniel, Yes you are correct. The problem was due to the static variable. I made the variable local and also moved another static variable to member variable. It is working fine now and I am able to complete the step now in Trailhead. Thanks a lot for your help. – Sudipta Deb Jul 28 '16 at 3:19

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.