• Attiq Ur-Rehman
  • NEWBIE
  • 5 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

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.
    }