You need to sign in to do that
Don't have an account?
jsacpt24
Test Class for Attachment erroring out for ID
I am trying to run a test for my code that is working for UI testing. I am getting an error of "System.DmlException: Insert failed. First exception on row 0 with id 5000m000005R0B9AAK; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]" for a stack trace of "Class.attachController.submit: line 27, column 1
Class.attachController_Test.test1: line 30, column 1"
When I looked at this I am not seeing anything that would cause for it to error out for the id. Am I possibly missing something?
VF:
Class.attachController_Test.test1: line 30, column 1"
When I looked at this I am not seeing anything that would cause for it to error out for the id. Am I possibly missing something?
VF:
<apex:page Standardcontroller="Case" extensions="attachController" showheader="false" > <html lang="en"> <head> <link rel="icon" type="image/png" href="{!$Resource.siteLogo}" /> <link href="https://use.fontawesome.com/releases/v5.4.2/css/all.css" rel="stylesheet" media="screen"/> <!-- Bootstrap --> <link href="https://cdn.rawgit.com/creativetimofficial/now-ui-kit/2e8e665f/assets/css/bootstrap.min.css" rel="stylesheet" media="screen"/> <link href="https://cdn.rawgit.com/creativetimofficial/now-ui-kit/2e8e665f/assets/css/now-ui-kit.min.css?v1.2.0" media="screen"/> <title>Submit a Case</title> <style> p, li, .description{ font-weight: 400; line-height: 1.8rem; } .paddingTop{padding-top: 2rem;} } .background-black{background: #2196f3;} .background-black:hover, .background-black:focus{background: #2386d3 !important;} section .section-inner h2{color: #fff;} .round { border-radius: 100px; margin: 0; color: #9a9a9a } .longBox{height: 100px;} @media screen and (max-width: 500px){ .brand .h1-seo{ font-size: 2.2rem; } } </style> </head> <body> <div class="container"> <apex:form styleclass="form-signin"><br/><br/><br/> <br/><h2 class="form-signin-heading">Create a Ticket</h2><br/><br/> <h5 class="form-signin-heading">Email</h5><br/> <apex:inputfield styleClass="inputGroupSelect01 custom-select form-control" value="{!case.Email__c}" required="true"/> <br/><h5 class="form-signin-heading">Subject</h5><br/> <apex:inputfield styleClass="inputGroupSelect01 custom-select form-control" value="{!case.Subject}" required="true"/> <br/><h5 class="form-signin-heading">What are you experiencing?</h5><br/> <apex:inputfield styleClass="inputGroupSelect01 custom-select form-control" value="{!case.What_are_you_experiencing__c}" required="true"/> <br/><h5 class="form-signin-heading">Where is the location of your issue?</h5><br/> <apex:inputfield styleClass="inputGroupSelect01 custom-select form-control" value="{!case.Location_of_issue__c}" required="true"/> <br/><h5 class="form-signin-heading">Campaign or Source?</h5><br/> <apex:inputfield styleClass="inputGroupSelect01 custom-select form-control" value="{!case.Source_Type__c}" required="false"/><br/> <br/><h5 class="form-signin-heading">Campaign/Source ID</h5><br/> <apex:inputfield styleClass="inputGroupSelect01 custom-select form-control" value="{!case.Campaign_Source_ID__c}" required="false"/><br/> <br/><h5 class="form-signin-heading">Description</h5><br/> <apex:inputfield styleClass="inputGroupSelect01 custom-select form-control longBox" value="{!case.Description}" required="true"/><br/> <apex:commandButton action="{!submit}" value="Submit" styleClass="btn btn-primary btn-block btn-lg background-black btn-round round"/><br/> <!-- <button class="btn btn-primary btn-block btn-lg background-black btn-round round " action="{!submit}">Submit</button><br/><br/><br/> --> <apex:pageBlock > <apex:pageBlockSection columns="1" > <div id="upload" class="upload"> <h6> If you have more than 1 file to upload please zip file to include all files. </h6> <apex:inputFile title="Choose File" value="{!filebody}" filename="{!filename}" styleClass="btn btn-primary btn-block btn-lg background-black btn-round round"/> </div> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </div> <!-- /container --> <!-- Bootstrap core JavaScript ================================================== --> <!-- Placed at the end of the document so the pages load faster --> </body> </html> </apex:page>Apex:
public class attachController { public case objcase{get;set;} public Attachment myAttachment{get;set;} public string fileName{get;set;} public Blob fileBody{get;set;} public attachController(Apexpages.standardcontroller controller) { objcase = (Case)controller.getRecord(); myAttachment = new Attachment(); } public PageReference submit(){ if (fileName != null){ objcase.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Product Case').getRecordTypeId(); insert objcase; System.debug('@@@@@fileBody'+fileBody); myAttachment = new Attachment(); Integer i=0; myAttachment .clear(); myAttachment.Body = fileBody; myAttachment.Name = fileName ; myAttachment.ParentId = objcase.id; insert myAttachment; } else { objcase.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Product Case').getRecordTypeId(); insert objcase; } pagereference pr = Page.thankYouForSubmittingYourProductCase; pr.setRedirect(true); return pr; } }Test:
@isTest public class attachController_Test { static testMethod void test1() { Case cs = new Case( Email__c = 'Test@test.com', 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' ); insert cs; Attachment attach=new Attachment(); attach.Name='Unit Test Attachment'; Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); attach.body=bodyBlob; attach.parentId=cs.id; insert attach; List<Attachment> attachments=[select id, name from Attachment where parent.id=:cs.id]; System.assertEquals(1, attachments.size()); ApexPages.StandardController stdCtr = new ApexPages.StandardController(cs); attachController ctr = new attachController(stdCtr); ctr.objcase = cs; ctr.myAttachment = attach; ctr.submit(); } }
I am still getting the same error.
try this
@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 = 'Test@test.com',
Subject = 'Phone',RecordTypeId = recId ,
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');
insert caseObj ;
Blob bodyBlob=Blob.valueOf('Attachment Body');
Attachment attachObj = new Attachment(
Name='Unit Test Attachment',
body=bodyBlob,
parentId=caseObj.id);
insert attach;
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();
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
I am still getting the same error.
Error: System.DmlException: Insert failed. First exception on row 0 with id 5000m000005R452AAC; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Stack Trace: Class.attachController.submit: line 16, column 1
Class.attachController_Test.test1: line 32, column 1