function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shubham Nandwana 2Shubham Nandwana 2 

superbadge: Advanced Apex Specialist Error

Hello,
I am solving challenge 4 of superbadge:Advance Apex Specialist and I am recieving the following error.
Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0 with id 01t7F0000026oieQAA; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

In my code I am creating a product and passing all the required fields apart from Id. I have checked using System.debug method and observed that products list do no contain any Id before insert(which is obvious though).

I am pasting my function here, any help is appreciated .
Regards,
Shubham Nandwana

CODE:(PS: TODO were already provided)

 public static List<Product2> ConstructProducts(Integer cnt){
        List<Product2> pros=new List<Product2>();
               
        Integer picklistSize=Constants.PRODUCT_FAMILY.size();
        System.debug(picklistSize+''+Constants.PRODUCT_FAMILY[0].getValue());
        Integer j=0;
        for(Integer i=0;i<cnt;i++){
            Product2 p=new Product2();
            p.Name='ProductConstruction'+i;
            p.IsActive=true;
            p.Initial_Inventory__c =10;
            p.Family=Constants.PRODUCT_FAMILY[j].getValue();
            if(j==picklistSize-1){
                j=0;
            }else{
            j++;
            }
            pros.add(p);
        }
        System.debug('before insert '+pros);
        insert pros;
        System.debug('after insert '+pros);
        return pros;
        //ToDo: Ensure this method returns a list, of size cnt, of uniquely named Product2 records
        //  with all the required fields populated
        //  and IsActive = true
        //  an Initial Inventory set to 10
        //  and iterating through the product family picklist values throughout the list.
    }
 

Attiq Ur-RehmanAttiq Ur-Rehman
Task details say:
Next, go back to the orderTests class and ensure its test methods don’t (and can’t) use live data. Create a new method named SetupTestData that is will be used to generate test data for all the unit tests in OrderTests.

So don't insert the data in ConstructXXX methods. insert the values in InsertTestData method.