2

I am at a loss for this one and could really use some help on this. I have a vf page and apex extension that is used with a site. Everything works when I test this via the UI but for some reason I am getting an error

System.DmlException: Insert failed. 
First exception on row 0 with id 5000m000005mJxSAAU; first error: 
INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] 

when I run my test and I am not sure what is wrong with this. Does anyone see something in my apex or test class that I wrote incorrectly?

Apex:

public class attachController
{
public case objcase{get;set;}
public Attachment myAttachment{get;set;}
public string fileName{get;set;}
public Blob fileBody{get;set;}

string recId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Product Case').getRecordTypeId();

public attachController(Apexpages.standardcontroller controller)
{
    objcase = (Case)controller.getRecord();
    myAttachment = new Attachment();
}
public PageReference submit(){
    if (fileName != null){
        objcase.RecordTypeId = recId;
        insert objcase;

        myAttachment = new Attachment();
        Integer i=0;
        myAttachment .clear();
        myAttachment.Body = fileBody;
        myAttachment.Name = fileName ;
        myAttachment.ParentId = objcase.id;            
        insert myAttachment;
    } else {
        objcase.RecordTypeId = recId;
        insert objcase;

    }             
    pagereference pr = Page.thankYouForSubmittingYourProductCase;                          
    pr.setRedirect(true);
    return pr;
}
}

Test:

@isTest
public class attachController_Test {

    @isTest
    static void test1(){

    //Id recId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Product Case').getRecordTypeId();

    Case caseObj = new Case( Email__c = '[email protected]',
                            Subject = 'Phone',
                            What_are_you_experiencing__c = 'I have a feature request',
                            Location_of_issue__c = 'Campaigns',
                            Source_Type__c = 'Source',
                            Campaign_Source_ID__c = '123456',
                            Description = 'test from Mr. Testerson');


    Blob bodyBlob=Blob.valueOf('Attachment Body');
    Attachment attachObj = new Attachment(
        Name='Unit Test Attachment',
        body=bodyBlob,
        parentId=caseObj.id);      


    ApexPages.StandardController stdCtr = new ApexPages.StandardController(caseObj );
    attachController attachControllerObj = new attachController(stdCtr);
    attachControllerObj .fileName = 'your file name';
    attachControllerObj .fileBody = Blob.valueOf('your file name');
    attachControllerObj .submit();  
}

static void test2(){

    Case caseObj1 = new Case(Email__c = '[email protected]',
                             Subject = 'Phone',
                             What_are_you_experiencing__c = 'I have a feature request',
                             Location_of_issue__c = 'Campaigns',
                             Source_Type__c = 'Source',
                             Campaign_Source_ID__c = '123456',
                             Description = 'test from Mr. Testerson');


    ApexPages.StandardController stdCtr = new ApexPages.StandardController(caseObj1 );
    attachController attachControllerObj = new attachController(stdCtr);
    attachControllerObj.submit();  
}
}

2 Answers 2

0

I do not know how you call the page in the UI.

For the test, you create a case and feed it into the VF controller. So the VF page has a recordId of an existing case.

In your apex class you then do:

    if (fileName != null){
        objcase.RecordTypeId = recId;
        insert objcase;

Which means you are re-inserting an existing case. Hence the error. upsert might work for your requirements.

2
  • That worked. I guess I didn't think about trying to do it as an upsert as it is just a vf form to insert cases
    – SoleHack
    Commented Mar 28, 2019 at 22:35
  • I am getting an error when I try to deploy this "System.NullPointerException: Attempt to de-reference a null object Stack Trace: Class.attachController_Test.test1: line 11, column 1" From what it looks like it is causing an error. Any thoughts on why this might be?
    – SoleHack
    Commented Mar 29, 2019 at 0:09
0

Since your code presumes an insert, you wouldn't insert the case in your unit test. You can pass in an unsaved record to the standard controller:

static void test2(){

    Case caseObj = new Case(Email__c = '[email protected]',
                            Subject = 'Phone',
                            What_are_you_experiencing__c = 'I have a feature request',
                            Location_of_issue__c = 'Campaigns',
                            Source_Type__c = 'Source',
                            Campaign_Source_ID__c = '123456',
                            Description = 'test from Mr. Testerson');

    // Do not insert here...  //
    //insert caseObj ;


    ApexPages.StandardController stdCtr = new ApexPages.StandardController(caseObj );
    attachController attachControllerObj = new attachController(stdCtr);
    attachControllerObj.submit();  
}
3
  • I am currently getting 'System.NullPointerException: Attempt to de-reference a null object Stack Trace: Class.attachController.<init>: line 8, column 1 Class.attachController_Test.test1: line 26, column 1' Line 8 on the class is 'string recId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Product Case').getRecordTypeId();' and then like 26 on the test class is 'attachController attachControllerObj = new attachController(stdCtr);'
    – SoleHack
    Commented Mar 29, 2019 at 4:42
  • The error looks like it is erroring out when I set the variable for recId which is to just set the id for the record type. I am not sure why it would be erroring out on this.
    – SoleHack
    Commented Mar 29, 2019 at 4:42
  • @JoshuaAnderson Sounds like you might have the wrong name for the record type, or you didn't deploy it as part of your deployment.
    – sfdcfox
    Commented Mar 29, 2019 at 7:39

You must log in to answer this question.

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